最近準備面試,重溫數據結構,先給出各種排序算法,算法中註釋比較少面試
若想下載註釋比較全的源代碼上個人csdn下載我上學的時候寫的:算法
http://download.csdn.net/detail/jiezou007/1952628數據結構
與如今寫的有些區別。函數
如下代碼是此次寫的,算法中對於指針與引用的選擇。讀者本身能夠根據須要修改,冒泡算法是指針類型作函數變量,爲了方便其他算法是C++引用。主函數中將註釋更改後就能夠實現各種排序算法。ui
#include <stdio.h> #include <stdlib.h> #include <conio.h> #define MAXSIZE 100 typedef struct { int key; }RedType; typedef struct { RedType r[MAXSIZE+1]; int Length; }Sqlist; //插入排序 O(n^2) void InsertSort(Sqlist &L) { int i,j; for (i=2;i<L.Length;++i) { if (L.r[i].key<L.r[i-1].key) { L.r[0].key=L.r[i].key; //L.r[i].key=L.r[i-1].key; for (j=i-1;L.r[0].key<L.r[j].key;--j) L.r[j+1].key=L.r[j].key; L.r[j+1].key=L.r[0].key; } } } //折半插入排序 O(n^2) void BInsertSort(Sqlist &L) { int m,low,high,i; for (i=2;i<=L.Length;++i) L.r[0]=L.r[i]; low=1; high=i-1; while(low<=high) { m=(low+high)/2; if (L.r[0].key<L.r[m].key) high=m-1; else low=m+1; } for (int j=i-1;j>=high+1;--j) L.r[j+1]=L.r[j]; L.r[high+1]=L.r[0]; } //快速排序中的冒泡排序 O(n^2) void Bubble(Sqlist *L) { int n,i,k,j; n=L->Length; for (i=n;i>0;i--) { for (j=1;j<i;j++) { if (L->r[j].key<L->r[j+1].key)//從大到小 { k=L->r[j].key; L->r[j].key=L->r[j+1].key; L->r[j+1].key=k; } } } } int Partition(Sqlist &T,int low,int high) { int pivotkey; pivotkey=T.r[low].key; while(low<high) { while(low<high&&T.r[high].key>=pivotkey) --high; T.r[low].key=T.r[high].key; while(low<high&&T.r[low].key<=pivotkey) ++low; T.r[high].key=T.r[low].key; } T.r[low].key=pivotkey; return low; } void QSort(Sqlist &L,int low,int high) { int pivotloc; if (low<high) { pivotloc=Partition(L,low,high);//一趟排序 QSort(L,low,pivotloc-1); QSort(L,pivotloc+1,high); } } //快速排序遞歸形式 O(n*log2(n)) void QuickSort(Sqlist &L) { QSort(L,1,L.Length); } int SelectMinKey(Sqlist &L,int low) { int i,j,t; t=L.r[low].key; j=low; for (i=low+1;i<L.Length;++i) { if (L.r[low].key<t) { t=L.r[low].key; j=i; } } return j; } //簡單選擇排序 O(n^2) void SelectSort(Sqlist &L) { int i,t,j; for (i=1;i<L.Length;++i) { j=SelectMinKey(L,i); if (j!=i) { t=L.r[i].key; L.r[i].key=L.r[j].key; L.r[j].key=t; } } } //歸併排序 O(n*log2(n)) void Merge(RedType sr[],RedType tr[],int i,int m,int n) { //將有序的SR[i,...,m]和SR[m+1,...,n]歸併到有序的TR[i,..,n] int j,k,h; for (j=m+1,k=i;i<=m&&j<=n;k++) { if (sr[i].key<=sr[j].key) tr[k].key=sr[i++].key; else tr[k].key=sr[j++].key; } if (i<=m) { for (h=0;h<=m-i;h++) tr[k+h].key=sr[i+h].key; } if (j<=n) { for (h=0;h<=n-j;h++) { tr[k+h]=sr[j+h]; } } } void MergeSort(RedType sr[],RedType tr1[],int s,int t) { int m; RedType tr2[MAXSIZE+1]; if (s==t) tr1[s]=sr[s]; else { m=(s+t)/2; MergeSort(sr,tr2,s,m); MergeSort(sr,tr2,m+1,t); Merge(tr2,tr1,s,m,t); } } void main() { system("cls"); Sqlist T; int key,i=1; printf("請輸入數:\n"); scanf("%d",&key); while(key!=0) { T.r[i++].key=key; printf("請輸入數:\n"); scanf("%d",&key); T.Length=i-1; } //InsertSort(T); 插入排序,用引用 //BInsertSort(T);折半插入排序 //Bubble(&T);//冒泡排序,用指針試試 //QuickSort(T);//快速排序 //SelectSort(T);//簡單選擇排序 MergeSort(T.r,T.r,1,T.Length);//歸併排序 printf("查找結果:\n"); for (i=1;i<=T.Length;i++) { printf("%d ",T.r[i].key); } getch(); }