博客园 首页 联系 订阅 管理

插入排序: 算法和字面意思是一样的,对于当前元素a[now],在a[1]到a[now]的位置中找一个位置给a[now].因为a[1]到a[now-1]的数据是有序的,所以在插入a[now]后,序列仍是有序的。算法时间复杂度为 o(Σ(i) )=o(n*n)  (1<i<=n),这是最坏情况下的时间复杂度; 空间复杂度为o(1); 实现很简单,当表中数据很少时,这会是一个不错的选择。代码:

View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 #define maxSize 200008
 8 
 9 void inPutData(int myArray[], int n){
10     for ( int i=1; i<=n; ++i )
11        scanf ("%d", myArray+i);
12 }
13 
14 void insert(int data, int a[], int pos ) {
15      while( data < a[pos] ){
16          a[pos+1] = a[pos--];
17      }
18      a[pos+1] = data;
19 }
20 
21 void insertSort(int a[], int n){
22     for ( int i=2; i<=n; ++i ) {
23         int tmp = a[i];
24         insert(tmp, a, i-1);
25     }
26 }
27 
28 void outPutData(int myArray[], int n) {
29      for ( int i=1; i<=n; ++i ) {
30           if(i-1) printf(" ");
31           printf("%d", myArray[i]);
32      }
33      puts("");
34 }
35 
36 int main(){
37     int n;
38     int myArray[maxSize];
39     while( ~scanf("%d", &n) ) {
40          inPutData(myArray, n);
41          insertSort(myArray, n);
42          outPutData(myArray, n);
43     }
44 };

 

冒泡排序: 所谓的冒泡就是每次循环把最大的或最小的元素沉到底部去,即放到数组靠后的位置去。

共有n-1次循环,每次循环只需在[1,n-i]的区间里比较就可以了。时间复杂度为o(n*n),空间复杂度为o(1);

代码:

View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 #define maxSize 200008
 8 
 9 template < typename T>
10 void Swap( T& a, T& b){
11       T tmp = a;  a = b; b = tmp;
12 }
13 
14 void inPutData(int myArray[], int n){
15     for ( int i=1; i<=n; ++i )
16        scanf ("%d", myArray+i);
17 }
18 
19 void bubbleSort(int a[], int n){
20     for ( int i=1; i<n; ++i ) {
21         for ( int j=1; j<=n-i; ++j )
22           if(a[j] > a[j+1] ) Swap(a[j], a[j+1]);
23     }
24 }
25 
26 void outPutData(int myArray[], int n) {
27      for ( int i=1; i<=n; ++i ) {
28           if(i-1) printf(" ");
29           printf("%d", myArray[i]);
30      }
31      puts("");
32 }
33 
34 int main(){
35     int n;
36     int myArray[maxSize];
37     while( ~scanf("%d", &n) ) {
38          inPutData(myArray, n);
39          bubbleSort(myArray, n);
40          outPutData(myArray, n);
41     }
42 };

 

选择排序: 每次循环找出最小的或最大放在当前位置,循环n-1次就可以了。时间复杂度为: o(n*n), 空间复杂度为: o(1);  代码如下:

View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 #define maxSize 200008
 8 
 9 template < typename T>
10 void Swap( T& a, T& b){
11       T tmp = a;  a = b; b = tmp;
12 }
13 
14 void inPutData(int myArray[], int n){
15     for ( int i=1; i<=n; ++i )
16        scanf ("%d", myArray+i);
17 }
18 
19 void selectionSort(int a[], int n){
20     for ( int i=1; i<n; ++i ) {
21         int pos = i;
22         for ( int j=i+1; j<=n; ++j )
23           if(a[j] < a[pos] ) pos = j;
24         if(pos ^ i) Swap(a[i], a[pos]);
25     }
26 }
27 
28 void outPutData(int myArray[], int n) {
29      for ( int i=1; i<=n; ++i ) {
30           if(i-1) printf(" ");
31           printf("%d", myArray[i]);
32      }
33      puts("");
34 }
35 
36 int main(){
37     int n;
38     int myArray[maxSize];
39     while( ~scanf("%d", &n) ) {
40          inPutData(myArray, n);
41          selectionSort(myArray, n);
42          outPutData(myArray, n);
43     }
44 };