选择排序,冒泡排序,归并排序,快速排序,堆排序等等

来源:互联网 发布:客单价方面的优化 编辑:程序博客网 时间:2024/06/08 13:28
#include<iostream>
using namespace std;
void BubbleSort1(int a[],int size) //冒泡排序
{assert(a);int i = 0;int j = 0;for( i=0; i<size; i++ ){for( j=1; j<size-i; j++ ){if( a[j-1] > a[j] )swap(a[j-1],a[j]);}}}void BubbleSort2(int a[],int size) //冒泡排序2,设置一个标志位,如果有一躺{                                  //比较都没有交换,说明就是有序的了assert(a);int k = 0;int i = 1;bool flag = true;while( flag ){flag = false;for( i; i<k; i++ ){if( a[i-1] > a[i] ){swap( a[i-1],a[i] );flag = true;}}k--;}}void BubbleSort3(int a[],int size)//设置一个记录,记录第一次比较结束的地方{                                 //下一次比较的时候,比较到第一次记录的那个地方就可以了assert(a);int i = 0;int flag = size;//flag是记录上一次比较结束的那个地方while( flag > 0){int k = flag;flag = 0;for( i=1; i<k; i++ ){if( a[i-1] > a[i]){swap( a[i-1],a[i] );flag = i;}}}}void InsertSort(int a[],int size)//插入排序{assert(a);int i = 0;for(i=1; i<size; i++ ){int temp = a[i];int k = i-1;while( k>=0 && temp>a[k] ){a[k+1] = a[k];k--;}a[k+1] = temp;}} void ShellSort(int a[],int size)//希尔排序{assert(a);for(int gap=size/2; gap>0; gap=gap/2 )//gap是步长,当gap>=0的时候整个数组就有顺序了{int i = 0;for( i=0; i<gap; i++)//控制次数的{                 // /1/ 9 8 5 2     /4/ 6 3 9 7     /7/ 8 9 2 0int j = 0;for( j=i+gap; j<size; j=j+gap )//插入排序{int temp = a[j];int k = j - gap;while( k>=0 && a[k]>temp )}a[k+gap] = a[k];k = k - gap;}a[k+gap] = temp;} }void FastSort(int a[],int left,int right) //快速排序{if ( left >= right )return ;int begin = left;int end = right;int key = a[left];while( begin < end ){while( begin<end && a[end]>key )//end是来找到比key小的数{end--;}if( begin < end ){a[begin] = a[end];begin++;}while( begin<end && a[begin]<key )//begin是找到一个比key大的数{begin++;}if( begin < end ){a[end] = a[begin];end--;}}a[begin] = key;FastSort(a,begin+1,right);FastSort(a,left,begin-1);}void SelectSort(int a[],int size)//选择排序{assert(a);int i = 0;int left = 0;int right = size - 1;while( left < right ){int min = left;int max = right;for( i=min; i<max; i++){if( a[i] < a[min] ){swap(a[min],a[i]);}if (a[i] > a[max] )}swap(a[max],a[i]);left++;right--;}}void AdjustDown(int a[],int root,int size){int  left = root * 2 + 1;int right = left + 1;int key = left;while( left <size ){if( right<size && a[left]>a[right] ){key = right;}if(a[key]<a[root] ){swap( a[root],a[key] );root = key;left = root * 2 + 1;right = left + 1;key = left;}else{break;}}}void HeapSort(int a[],int size )//堆排序{assert(a);int begin = size / 2 - 1;int i = 0;for( begin; begin>=0; begin-- ){AdjustDown(a,begin,size);}int end = size - 1;while(end){swap( a[0],a[end] );AdjustDown(a,0,end );end--;}}#include<iostream>using namespace std;void mergearray(int a[],int first,int mid,int last,int temp[]){int i = first;int j = mid + 1;int m = mid;int n = last;int k =  0;while( i<=m && j<=n ){if( a[i] <= a[j] )temp[k++] = a[i++];elsetemp[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 main(){int a[] = {9,5,5,10,2,3,8,4,10,8,7};int size = sizeof(a) / sizeof(a[0]);//BubbleSort1(a,size);//BubbleSort1(a,size);//BubbleSort3(a,size);//ShellSort(a,size);//FastSort(a,0,size-1);//SelectSort(a,size);//InsertSort(a,size);//HeapSort(a,size );MergeSort(a,n);int z = 0;for( z; z<size; z++ ){cout<<a[z]<<" ";}cout<<endl;return 0;}


1 0
原创粉丝点击