使用的教材是电子工业出版社出版的《Data Structures and Algorithm Analysis in C++ 》(《数据结构与算法分析(C++)》(第三版)),作者是【美】Clifford A Shaffer,译者是张铭、刘晓丹等。所以,有些代码与书上给出来的几近相同,如有侵权,请联系本人([email protected]),我会删除文章的。
实验内容是几种主要排序算法的实现,包括:插入排序,选择排序,冒泡排序,Shell排序,归并排序,快速排序,堆排序,基数排序。
--------Sort.h--------------------
#ifndef SORT #define SORT template<typename E> void asign(E A[], int n) //Set elements of B with the rand value { cout<<"\nInput Data is :"<<endl; for(int i=0;i<n;i++) A[i] = rand()%100; print(A,n); } template<typename E> void print(E A[],int n) //print the elements of A { for(int i=0;i<n;i++) { cout<<A[i]<<" "; } cout<<endl; } template<typename E > void swap(E A[],int i,int j) { E temp = A[i]; A[i] = A[j]; A[j] = temp; } template<typename E > void insertionSort(E A[] , int n) { for(int i=1;i<n;i++) { for(int j=i;j>0;j--) { if(A[j]<A[j-1]) swap(A,j,j-1); } } } template<typename E> void bubSort(E A[],int n) { for(int i=0;i<n-1;i++) for(int j=n-1;j>i;j--) { if(A[j]<A[j-1]) swap(A,j,j-1); } } template<typename E> void selectionSort(E A[],int n) { int index; for(int i=0;i<n-1;i++) { index = i; for(int j=i+1;j<n;j++) { if(A[index]>A[j]) index = j; } swap(A,i,index); } } template<typename E> void insertion2(E A[],int n,int curr) { for(int i=curr;i<n;i+=curr) for(int j=i;j>=curr&&A[j]<A[j-curr];j-=curr) swap(A,j,j-curr); } template<typename E> void shellSort(E A[],int n) { for(int i=n/2;i>2;i/=2) for(int j=0;j<i;j++) insertion2(&A[j],n-j,i); insertion2(A,n,1); } template<typename E> void mergeSort(E A[],E temp[],int left,int right) { if(left==right) return; int i,j,k, mid = (left+right)/2; mergeSort(A,temp,left,mid); mergeSort(A,temp,mid+1,right); for(i=mid;i>=left;i--) temp[i]=A[i]; for(j=1;j<=right-mid;j++) temp[j+mid]=A[right+1-j]; for(i=left,j=right,k=left;k<=right;k++) if(temp[i]>temp[j]) A[k] = temp[j--]; else A[k] = temp[i++]; } template<typename E> int findPivot(E A[],int i,int j) { return (i+j)/2; } template<typename E> int partition(E A[],int l,int r,int z ) { do { while(A[++l]<z) ; while(l<r&&A[--r]>z) ; swap(A,l,r); }while(l<r); return l; } template<typename E> void qsort(E A[],int i,int j) { if(j<=i) return; int pivot = findPivot(A,i,j); swap(A,pivot,j); int k = partition(A,i-1,j,A[j]); swap(A,k,j); qsort(A,i,k-1); qsort(A,k+1,j); } template<typename E> void radix(E A[],E B[],int n,int k,int r,int cnt[]) { int j; for(int i=0,rtoi=1;i<k;i++,rtoi*=r) { for(j=0;j<r;j++) cnt[j]=0; for(j=0;j<n;j++) cnt[(A[j]/rtoi)%r]++; for(j=1;j<r;j++) cnt[j] = cnt[j]+cnt[j-1]; for(j=n-1;j>=0;j--) B[--cnt[(A[j]/rtoi)%r]] = A[j]; for(j=0;j<n;j++) A[j] = B[j]; } } template<typename E> void heapSort(E A[], int n) { E temp; Heap<int> H(A, n, n); for(int i=0; i<n; i++) temp = H.removeFirst(); } #endif
--------Sort_heap.h---------
template<typename E> class Heap { private: E* heap; int maxSize; int size; int leftChild(int pos) const { return 2*pos+1; } int rightChild(int pos) const { return 2*pos+2; } void siftdown(int pos) { while(!isLeaf(pos)) { int j = leftChild(pos); int rc = rightChild(pos); if(rc<size && heap[j]<heap[rc]) j = rc; if(heap[j]<heap[pos]) return; swap(heap, j, pos); pos = j; } } public: Heap(E* h, int num, int max) { heap = h; size = num; maxSize = max; buildHeap(); } bool isLeaf(int pos) const { return (pos >=size/2) && (pos < size); } void buildHeap() { for(int i=size/2-1; i>=0 ;i--) { siftdown(i); } } E removeFirst() { swap(heap, 0, --size); if(size != 0) siftdown(0); return heap[size]; } };
每次排序前生成16个随机数作为输入
--------Main.cpp-----------
#include<iostream> #include<ctime> #include<cstdlib> #include"Sort.h" #include"Sort_heap.h" using namespace std; int main() { srand(int(time(0))); int len = 16; int b[16]; asign(b,len); cout<<"Implement of Insertion Sort :"<<endl; insertionSort(b,len); print(b,len); asign(b,len); cout<<"Implement of Bubble Sort : "<<endl; bubSort(b,len); print(b,len); asign(b,len); cout<<"Implement of Selection Sort : "<<endl; selectionSort(b,len); print(b,len); asign(b,len); cout<<"Implement of Shell Sort : "<<endl; shellSort(b,len); print(b,len); asign(b,len); cout<<"Implement of Merge Sort : "<<endl; int temp[16]; mergeSort(b,temp,0,len-1); print(b,len); asign(b,len); cout<<"Implement of Qsort : "<<endl; qsort(b,0,len-1); print(b,len); asign(b,len); cout<<"Implement of Radix Sort ; "<<endl; int* temp1 = new int[len]; radix(b,temp,len,2,10,temp1); print(b,len); delete[] temp1; temp1 = NULL; asign(b, len); cout<<"Imlement of Heap Sort : "<<endl; heapSort(b,len); print(b,len); system("PAUSE"); }