标签:输入 stream 冒泡 style log tmp hid close ack
针对排序算法,网上有比较好的讲解:程序员内功:八大排序算法;这里主要是学习过程中的重新记录,记录典型的排序算法实现模板;
冒泡排序:简单交换,从左端开始从大到小开始冒泡;每次冒泡后,最大值归位;
#include <iostream> #include <cstdio> #include <vector> using namespace std; vector<int> inarray; int main(){ freopen("in.txt", "r", stdin); // 重定向到输入 int i = 0; int tmp; // 方法1 while (cin >> tmp) { inarray.push_back(tmp); // cout << inarray[i] << endl; i++; } int num = i; cout << "输入数据:" << endl; for(i = 0; i < num; i++){ cout << inarray[i] << " "; } cout << endl << endl; cout << "排序过程:" << endl; for(i=1; i < num; i++){ for (int j = 0; j < num-i;j++){ if(inarray[j] >= inarray[j+1]){ int tmp = inarray[j]; inarray[j] = inarray[j+1]; inarray[j+1] = tmp; } } for(int k = 0; k < num; k++){ cout << inarray[k] << " "; } cout << endl; } for(i = 0; i < num; i++){ cout << inarray[i] << " "; } cout << endl; return 0; }
输入数据: 9 8 7 6 5 4 3 2 1 0 排序过程: 8 7 6 5 4 3 2 1 0 9 7 6 5 4 3 2 1 0 8 9 6 5 4 3 2 1 0 7 8 9 5 4 3 2 1 0 6 7 8 9 4 3 2 1 0 5 6 7 8 9 3 2 1 0 4 5 6 7 8 9 2 1 0 3 4 5 6 7 8 9 1 0 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
快速排序:
保持更新,转载请注明出处;更多内容请关注cnblogs.com/xuyaowen;
标签:输入 stream 冒泡 style log tmp hid close ack
原文地址:https://www.cnblogs.com/xuyaowen/p/sort-algos.html