最近兩天在為找工作複習準備。特此總結了資料結構中內部排序一章中提到的三大排序演算法。做了簡單實現。
// Sort.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <stdio.h>void bubble(int list[], int n){int i,j,temp;for (i=0;i<n;i++){for (j=0;j<n-i-1;j++){if (list[j]>list[j+1]){temp = list[j];list[j] = list[j+1];list[j+1] = temp;}}}}//*************************************************//希爾排序//*************************************************void ShellInsert(int list[], int dk, int len){int i, j;int temp;for(i=dk; i<len;++i){if (list[i]<list[i-dk]){temp=list[i];for (j=i-dk;j>=0&&(temp<list[j]);j-=dk){list[j+dk]=list[j];}list[j+dk] = temp;}}}void ShellSort(int list[], int len){int dk=len;while (dk>0){dk=dk/2;ShellInsert(list,dk,len);}}//**********************************************************//快速排序//**********************************************************int Partition(int list[], int low, int high){int pivotkey;pivotkey = list[low];while (low<high){while ((low<high) && (list[high]>=pivotkey)){high--;}list[low] = list[high];while((low<high) && (list[low]<=pivotkey)){low++;}list[high] = list[low];}list[low] = pivotkey;return low;}void QSort(int list[], int low, int high){int pivotloc;if (low<high){pivotloc = Partition(list, low, high);QSort(list,low,pivotloc-1);QSort(list,pivotloc+1,high);}}void QuickSort(int list[], int len){QSort(list,0,len-1);}int main(int argc, char* argv[]){int list[]={49,38,65,97,76,13,27,49,55,04};// bubble(list,10);/*ShellSort(list,10);*/QuickSort(list, 10);for (int i=0; i<10; i++){printf("%d ", list[i]);}printf("\n");return 0;}