c++简单实现:(连续数组)插入排序,选择排序,希尔排序,快速排序,二分查找
#include <iostream> using namespace std; void swap(int &i,int &j); void InsertionSort(int *l,int size); void SelectionSort(int *l,int size); void ShellSort(int *l,int size); void sort_interval(int start,int increment,int *l,int size); void QuickSort(int *l,int size); void recQuickSort(int *l,int low,int high); int Partition(int *l,int low,int high); int BinarySearch(int *l,int size,int value); int main(int argc,char **argv) { int list[]={10,11,20,18,2,4,6,1,8,3,7,19,15,100}; for(int i=0;i<14;i++) cout<<list[i]<<" "; cout<<endl; InsertionSort(list,14); SelectionSort(list,14); ShellSort(list,14); QuickSort(list,14); int a,s; cin>>a; s = BinarySearch(list,14,a); cout<<a<<":"<<s<<endl; getchar(); return 0; } void swap(int &i,int &j) { int temp = i; i = j; j = temp; } //insert sort void InsertionSort(int *l,int size) { cout<<"insertion sort begin"<<endl; int fir,place; int current; for(fir=1;fir<size;fir++) { if(l[fir]<l[fir-1]) { current = l[fir]; for(place = fir;place>0;place--) { l[place]=l[place-1]; if(l[place-1]<=current) break; } l[place]=current; } } for(int i=0;i<size;i++) cout<<l[i]<<" "; cout<<endl; cout<<"insertion sort end"<<endl; } //Selection Sort void SelectionSort(int *l,int size) { cout<<"selection sort begin"<<endl; int max_index; for(int i=0;i<size;i++) { max_index=i; for(int j=i+1;j<size;j++) { if(l[j]>l[i]) { max_index = j; } } swap(i,max_index); } for(int i=0;i<size;i++) cout<<l[i]<<" "; cout<<endl; cout<<"insertion sort end"<<endl; } // int BinarySearch(int *l,int size,int value) { //ensure num in l from small to large int low=0; int high=size-1; while(low<=high) { int middle = (low + high)/2; if(l[middle]==value) return middle; else if(l[middle]>value) high = middle - 1; else low = middle + 1; } return -1; } //shell sort void ShellSort(int *l,int size) { cout<<"shell sort begin"<<endl; int increment=size; int start; do{ increment = increment/3+1; for(start=0;start<increment;start++) sort_interval(start,increment,l,size); }while(increment>1); for(int i=0;i<size;i++) cout<<l[i]<<" "; cout<<endl; cout<<"shell sort end"<<endl; } void sort_interval(int start,int increment,int *l,int size) { int first,place; int current; for(first=start+increment;first<size;first+=increment) { if(l[first]<l[first-increment]) { place = first; current = l[first]; for(;place>0;place-=increment) { l[place]=l[place-increment]; if(l[place-increment]<=current) break; } l[place]=current; } } for(int i=0;i<size;i++) cout<<l[i]<<" "; cout<<endl; } //quick sort void QuickSort(int *l,int size) { recQuickSort(l,0,size-1); for(int i=0;i<size;i++) cout<<l[i]<<" "; cout<<endl; } void recQuickSort(int *l,int low,int high) { int pivot_pos; if(low<high) { pivot_pos = Partition(l,low,high); recQuickSort(l,low,pivot_pos-1); recQuickSort(l,pivot_pos+1,high); } } int Partition(int *l,int low,int high) { //pivot get middle value //to advance,can be random swap(l[low],l[(low+high)/2]); int lastsmall = low; int pivot = l[low]; for(int i=low+1;i<=high;i++) { if(l[i]<pivot) { lastsmall++;//ensure pivot at first pos, ++ first swap(l[i],l[lastsmall]); } } swap(l[low],l[lastsmall]); return lastsmall;//divide list with pivot }
参考教材:数据结构与程序设计(c++语言描述)http://book.douban.com/subject/1215873/
数据结构与程序设计(c语言)【中文版】