冒泡 选择 插入 归并 快排 堆排 希尔

来源:互联网 发布:淘宝限时打折要钱吗 编辑:程序博客网 时间:2024/06/05 22:30
#include<iostream>#include<algorithm>using namespace std;int a[]={7,2,1,9,6,5,4,3,8,6};//o(n logn)void bobble(){int i,j,temp=0;for(i =0; i <10;i++){for(j =1; j<10-i;j++){if(a[j-1]>a[j]){   temp = a[j];   a[j] = a[j-1];   a[j-1]=temp;}}}for(i =0; i <10 ; i++){cout<<a[i]<<endl;}} void choseSort(){int i,j,min=0,k=0;for(i = 0; i<10; i++){min = a[i];k= i;for(j =i; j<10; j++){if(min > a[j]){min = a[j];k = j;}}if(i!= k){           a[k] = a[i];           a[i] = min;}}for(i =0; i <10 ; i++){cout<<a[i]<<endl;}}void insetSort(){ int i,j,k;for(i = 1; i < 10; i++){k = a[i];for(j=i-1;j>=0; j--){if(k<a[j]){a[j+1] = a[j];}else{break;}}a[j+1] = k;}for(i = 0; i <10; i++){cout<<a[i]<<endl;}}//o(N*logN)/*   归并排序 快速排序 堆排序 希尔排序 */void MemeryArray(int a[],int n, int b[],int m,int c[]){int i,j,k;i = j = k = 0;while(i < n && j <m){if(a[i] < b[j])c[k++] = a[i++];elsec[k++] = b[j++];}while(i < n)c[k++] = a[i++];while(j < m)c[k++] = b[j++];} //通过 先递归的分解数列,再合并数列就完成了归并排序void mergearray(int a[],int first,int mid,int last,int temp[]) {int i = first,j=mid+1;int m = mid, n = last;int k =0;while(i <= m && j<= n){if(a[i] < a[j]){temp[k++] = a[i++];}else{temp[k++] = a[j++];}}while(i <=m){temp[k++] = a[i++];}while(j <= n){temp[k++] = a[j++];}for(i =0; i< k;i++){a[first+i] = temp[i];}}void mergesort(int a[], int first, int last,int temp[]){if(first < last){int mid = (first+last)/2;mergesort(a,first,mid,temp);mergesort(a,mid+1,last,temp);mergearray(a,first,mid,last,temp);}}bool MergeSort(int a[],int n){int *p = new int[n];if(p == NULL)return false;mergesort(a,0,n-1,p);delete[] p;return true;}//快速排序int quciksort(int low, int hight){int mid = a[low];while(low < hight){    while(low < hight &&mid<a[hight])hight--;a[low] = a[hight];while(low < hight && mid>=a[low])low++;a[hight] = a[low];}a[low] = mid;return low;}void QuickSort(int low,int hight){if(low < hight){int mid = quciksort(low,hight);QuickSort(low,mid-1);QuickSort(mid+1,hight);}}//调整堆 void HeapAdjust(int* a,int i, int size){int lchild = 2*i; // i的 左孩子节点序号int rchild = 2*i+1; // i的右孩子节点序号int max = i;        //临时变量if(i <= size/2){    if(lchild <= size && a[lchild]> a[max]){    max = lchild;}if(rchild <= size && a[rchild]>a[max]){max = rchild;}if(max !=i){swap(a[i],a[max]);HeapAdjust(a,max,size);//避免调整之后以max为父节点的子树不是堆 }    }} void BuildHeap(int *a,int size){int i;for(i= size/2; i >= 1; i--){   //非叶子节点最大序号值为size/2 HeapAdjust(a,i,size);}}void HeapSort(int *a,int size) //堆排序{   int i;   BuildHeap(a,size);   for(i = size; i>=1; i--){   swap(a[1],a[i]);   HeapAdjust(a,1,i-1);   }} void shellSort(int list[],int length){int gap = length /2;while( 1<= gap){//把距离为gap的元素编为一个组,扫描所有组for(int i =gap; i < length; i++){int j =0;int temp = list[i];//对距离为gap的元素组进行排序for(j = i-gap; j>= 0&& temp<list[j]; j = j-gap) {list[j+gap] = list[j];}list[j+gap] = temp;} gap = gap/2;//减小增量 }}int main(){//bobble();//choseSort();//  insetSort();    //MergeSort(a,10);    //QuickSort(0,9);    //HeapSort(a,10);    //shellSort(a,10);    for(int i = 1; i <10 ;i++)cout<<a[i]<<endl;    return 0;}

0 0