填坑之作。
那天因为在考试,就没有听到学长讲。后来听得似懂非懂的。
在《啊哈算法》里面看了一下看懂了。原理很简单,看看代码就懂了。
顺便玩了一下,比较 冒泡排序、快速排序、sort排序(qsort因为要指针知识,本人完全不懂) 的排序速度快慢
先随机生成10^7个数字……嗯,50多MB。调用rand()之前要srand(time(NULL));初始化。
1 #include <cstdio> 2 #include <iostream> 3 #include <cmath> 4 #include <cstring> 5 #include <queue> 6 #include <vector> 7 #include <algorithm> 8 #include <ctime> 9 #include <cstdlib> 10 11 #define rep(i,a,n) for(int i = a;i < n;i++) 12 #define per(i,a,n) for(int i = n-1;i >=a;i--) 13 #define pb push_back 14 #define VI vector<int> 15 #define QI queue<int> 16 17 typedef long long ll; 18 19 using namespace std; 20 21 const int MOD = 1e7+8; 22 const int N = 1e7+8; 23 24 int main(){ 25 freopen("data.in","w",stdout); 26 srand(time(NULL)); 27 printf("%d ",N); 28 29 rep(i,0,N){ 30 printf("%d ",rand() % MOD); 31 }puts(""); 32 return 0; 33 }
然后冒泡排序:
1 #include <cstdio> 2 #include <iostream> 3 #include <cmath> 4 #include <cstring> 5 #include <queue> 6 #include <vector> 7 8 #define rep(i,a,n) for(int i = a;i < n;i++) 9 #define per(i,a,n) for(int i = n-1;i >=a;i--) 10 #define pb push_back 11 #define VI vector<int> 12 #define QI queue<int> 13 14 typedef long long ll; 15 16 using namespace std; 17 18 const int N = 100000000; 19 int n; 20 int a[N] = {}; 21 22 void bubblesort(){ 23 rep(i,0,n-1){ 24 rep(j,0,n-1){ 25 if(a[j] > a[j+1]){ 26 int t = a[j];a[j] = a[j+1];a[j+1] = t; 27 } 28 } 29 } 30 } 31 32 int main(){ 33 freopen("data.in","r",stdin); 34 scanf("%d",&n); 35 rep(i,0,n){ 36 scanf("%d",&a[i]); 37 } 38 bubblesort(); 39 freopen("data_bubble.out","w",stdout); 40 rep(i,0,n){ 41 printf("%d ",a[i]); 42 }puts(""); 43 return 0; 44 }
接着快速排序:
1 #include <cstdio> 2 #include <iostream> 3 #include <cmath> 4 #include <cstring> 5 #include <queue> 6 #include <vector> 7 8 #define rep(i,a,n) for(int i = a;i < n;i++) 9 #define per(i,a,n) for(int i = n-1;i >=a;i--) 10 #define pb push_back 11 #define VI vector<int> 12 #define QI queue<int> 13 14 typedef long long ll; 15 16 using namespace std; 17 18 const int N = 100000000; 19 int a[N] = {}; 20 int n; 21 22 void quicksort(int l,int r){ 23 int flag = a[l]; 24 int i = l; 25 int j = r; 26 if(l > r ) return; 27 28 while(i != j){ 29 while(a[j] >= flag && i < j) j--; 30 31 while(a[i] <= flag && i < j) i++; 32 33 if(i < j){ 34 int t = a[i];a[i] = a[j];a[j] = t; 35 } 36 } 37 a[l] = a[i]; 38 a[i] = flag; 39 40 quicksort(l,i-1); 41 quicksort(i+1,r); 42 } 43 44 int main(){ 45 freopen("data.in","r",stdin); 46 scanf("%d",&n); 47 rep(i,0,n){ 48 scanf("%d",&a[i]); 49 } 50 quicksort(0,n-1); 51 freopen("data_quicksort.out","w",stdout); 52 rep(i,0,n){ 53 printf("%d ",a[i]); 54 }puts(""); 55 return 0; 56 }
最后,当当~sort排序(据同学说里面内置了三种函数:快速排序,冒泡排序,桶排序?然后哪个排的快选哪个):
1 #include <cstdio> 2 #include <iostream> 3 #include <cmath> 4 #include <cstring> 5 #include <queue> 6 #include <vector> 7 #include <algorithm> 8 9 #define rep(i,a,n) for(int i = a;i < n;i++) 10 #define per(i,a,n) for(int i = n-1;i >=a;i--) 11 #define pb push_back 12 #define VI vector<int> 13 #define QI queue<int> 14 15 typedef long long ll; 16 17 using namespace std; 18 19 const int N = 10000000; 20 int n; 21 int a[N] = {}; 22 23 bool cmp(int a,int b){ 24 return a < b; 25 } 26 27 28 int main(){ 29 freopen("data.in","r",stdin); 30 scanf("%d",&n); 31 rep(i,0,n){ 32 scanf("%d",&a[i]); 33 } 34 sort(a,a+n,cmp); 35 freopen("data_sort.out","w",stdout); 36 rep(i,0,n){ 37 printf("%d ",a[i]); 38 }puts(""); 39 return 0; 40 }
结果嘛~当然是很明显,看图:
第一张图是sort函数的,第二张图是快速排序,至于冒泡排序为什么没有图嘛~
我们可以算一下复杂度:
冒泡排序的复杂度是O(N^2) = 10^7 * 10^7 = 10^14。
计算机每秒的运算速度一般在10^6~10^9之间,当然也有比10^9的快的,比如我家的电脑^_^
但是这里就按照10^9/秒的速度算,至少27小时多才能算完。我等不了那么长时间T_T
嗯呐,就是这样~
原文:http://www.cnblogs.com/syuritsu/p/5161638.html