1、排序
排序算法分为交换类排序,插入类排序,选择类排序,归并类排序
交换类排序分为冒泡排序,快速排序(后面的408笔记有详细的)
2、冒泡排序
简单来说,每次都会找到剩余元素最小的元素,并将它放到前面。(演示代码是找到最大元素放到最后,主要看算法怎么写,没什么太大区别。)
冒泡排序的基本思想是:从后往前(或从前往后)两两比较相邻元素的值,(若A[j-1]>A[j]),则交换它们,直到序列比较完。我们称它为第一趟冒泡,结果是将最小的元素交换到待排序列的第一个位置。
这么说可能不直观,直接看代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| void BubbleSort(ElemType *A,int n) { int i,j; bool flag; for(i=0;i<n-1;i++) { flag= false; for(j=n-1;j>i;j--) { if(A[j-1]>A[j]) { swap(A[j-1],A[j]); flag= true; } } if(false==flag) { return; } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> typedef int ElemType; typedef struct{ ElemType *elem; int TableLen; }SSTable;
void ST_Init(SSTable &ST,int len) { ST.TableLen=len; ST.elem=(ElemType *)malloc(sizeof(ElemType)*ST.TableLen); int i; srand(time(NULL)); for(i=0;i<ST.TableLen;i++) { ST.elem[i]=rand()%100; } }
void ST_print(SSTable ST) { for(int i=0;i<ST.TableLen;i++) { printf("%3d",ST.elem[i]); } printf("\n"); }
void swap(int &a,int &b) { int tmp; tmp=a; a=b; b=tmp; }
void BubbleSort(ElemType *A,int n) { int i,j; bool flag; for(i=0;i<n-1;i++) { flag= false; for(j=n-1;j>i;j--) { if(A[j-1]>A[j]) { swap(A[j-1],A[j]); flag= true; } } if(false==flag) { return; } } }
int main() { SSTable ST; ST_Init(ST,10);
ST_print(ST); BubbleSort(ST.elem, 10); ST_print(ST); return 0; }
|
3、快速排序
分治思想。设定一个界定值,遍历所有需要排序元素,将小于该界定值和大于该界定值的放在其两侧,每次结束都能将该界定值放到其最终正确的位置。
一开始可能有点难理解,多看两遍就懂了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| int partition(ElemType *A,int low,int high) { ElemType pivot=A[low]; while(low<high) { while(low<high&&A[high]>=pivot) high--; A[low]=A[high]; while(low<high&&A[low]<=pivot) low++; A[high]=A[low]; } A[low]=pivot; return low; }
void QuickSort(ElemType *A,int low,int high) { if(low<high) { int pivot_pos=partition(A,low,high); QuickSort(A,low,pivot_pos-1); QuickSort(A,pivot_pos+1,high); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> typedef int ElemType; typedef struct{ ElemType *elem; int TableLen; }SSTable;
void ST_Init(SSTable &ST,int len) { ST.TableLen=len; ST.elem=(ElemType *)malloc(sizeof(ElemType)*ST.TableLen); int i; srand(time(NULL)); for(i=0;i<ST.TableLen;i++) { ST.elem[i]=rand()%100; } }
void ST_print(SSTable ST) { for(int i=0;i<ST.TableLen;i++) { printf("%3d",ST.elem[i]); } printf("\n"); }
int partition(ElemType *A,int low,int high) { ElemType pivot=A[low]; while(low<high) { while(low<high&&A[high]>=pivot) high--; A[low]=A[high]; while(low<high&&A[low]<=pivot) low++; A[high]=A[low]; } A[low]=pivot; return low; }
void QuickSort(ElemType *A,int low,int high) { if(low<high) { int pivot_pos=partition(A,low,high); QuickSort(A,low,pivot_pos-1); QuickSort(A,pivot_pos+1,high); } } int main() { SSTable ST; ST_Init(ST,10);
ST_print(ST); QuickSort(ST.elem,0,9); ST_print(ST); return 0; }
|
5、插入排序
不难,前面序列有序,后面序列无序,把后面序列依次插入前面序列,整个序列就有序了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| void InsertSort(ElemType *A,int n) { int i,j,insertVal; for(i=1;i<n;i++) { insertVal=A[i]; for(j=i-1;j>=0&&A[j]>insertVal;j--) { A[j+1]=A[j]; } A[j+1]=insertVal; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include <stdio.h> #include <stdlib.h> #include <time.h> typedef int ElemType; typedef struct{ ElemType *elem; int TableLen; }SSTable;
void ST_Init(SSTable &ST,int len) { ST.TableLen=len; ST.elem=(ElemType *)malloc(sizeof(ElemType)*ST.TableLen); int i; srand(time(NULL)); for(i=0;i<ST.TableLen;i++) { ST.elem[i]=rand()%100; } } void ST_print(SSTable ST) { for(int i=0;i<ST.TableLen;i++) { printf("%3d",ST.elem[i]); } printf("\n"); }
void InsertSort(ElemType *A,int n) { int i,j,insertVal; for(i=1;i<n;i++) { insertVal=A[i]; for(j=i-1;j>=0&&A[j]>insertVal;j--) { A[j+1]=A[j]; } A[j+1]=insertVal; }
} int main() { SSTable ST; ST_Init(ST,10); ST_print(ST); InsertSort(ST.elem,10); ST_print(ST); return 0; }
|