排序
来源:互联网 发布:二次元手机ar软件 编辑:程序博客网 时间:2024/05/25 01:34
#include<bits/stdc++.h>using namespace std;int a[1000];//冒泡排序 void BubbleSort(int a[],int n){for(int i=0;i<n;i++){for(int j=i+1;j<n;j++){if(a[i]>a[j]){ int temp=a[i]; a[i]=a[j]; a[j]=temp;}}}}//插入排序void InsertSort(int a[],int n){int i,j,k;for(i=1;i<n;i++){for( j=i-1;j>=0;j--)if(a[j]<a[i])break;if(j!=i-1){int temp=a[i];for( k=i-1;k>j;k--) a[k+1]=a[k]; a[k+1]=temp;}}}//希尔排序void ShellSort(int a[], int n){int i, j, gap;for(gap=n/2;gap>0;gap/=2)for (i = 0; i < gap; i++){for(j=i+gap;j<n;j+=gap) if (a[j] < a[j - gap]){int temp = a[j];int k = j - gap;while(k>=0&&a[k]>temp){a[k + gap] = a[k]; k -= gap; }a[k + gap] = temp; }}} //选择排序void SelectSort(int a[], int n){int i, j, nMinIndex;for (i=0;i<n;i++){nMinIndex=i; for (j=i+1;j<n;j++)if (a[j]<a[nMinIndex])nMinIndex=j; int temp=a[i]; a[i]=a[nMinIndex]; a[nMinIndex]=temp;}}//快速排序void QuickSort(int left,int right){ int i,j,t,temp; if(left>right) return; temp=a[left]; i=left; j=right; while(i!=j) { while(a[j]>=temp && i<j) j--; while(a[i]<=temp && i<j) i++; if(i<j){ t=a[i]; a[i]=a[j]; a[j]=t; } } a[left]=a[i]; a[i]=temp; QuickSort(left,i-1); QuickSort(i+1,right);} int main(){cout<<"生成100个随机数"<<endl; srand(time(0)); //为rand()函数提供时间种子保证每次运行程序rand的结果不同 for(int i=0;i<100;i++){a[i]=rand()%200; //随机生成100个0-200内的数 } cout<<"生成的100个随机数为"<<endl; for(int i=0;i<100;i++){ //打印生成的100个数 if((i+1)%10==0&&i) cout<<a[i]<<endl; else cout<<a[i]<<' '; } //排序 //BubbleSort(a,100); //InsertSort(a,100); //ShellSort(a,100);//SelectSort(a,100);QuickSort(0,99); //从a[0]到a[4]; cout<<"排序生成的100个随机数"<<endl; for(int i=0;i<100;i++){ if((i+1)%10==0&&i) cout<<a[i]<<endl; else cout<<a[i]<<' '; }return 0;}``` c++#include<bits/stdc++.h>using namespace std;int a[1000];//冒泡排序 void BubbleSort(int a[],int n){ for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if(a[i]>a[j]){ int temp=a[i]; a[i]=a[j]; a[j]=temp; } } }}//插入排序void InsertSort(int a[],int n){ int i,j,k; for(i=1;i<n;i++){ for( j=i-1;j>=0;j--) if(a[j]<a[i]) break; if(j!=i-1){ int temp=a[i]; for( k=i-1;k>j;k--) a[k+1]=a[k]; a[k+1]=temp; } }}//希尔排序void ShellSort(int a[], int n){ int i, j, gap; for(gap=n/2;gap>0;gap/=2) for (i = 0; i < gap; i++){ for(j=i+gap;j<n;j+=gap) if (a[j] < a[j - gap]){ int temp = a[j]; int k = j - gap; while(k>=0&&a[k]>temp){ a[k + gap] = a[k]; k -= gap; } a[k + gap] = temp; } }} //选择排序void SelectSort(int a[], int n){ int i, j, nMinIndex; for (i=0;i<n;i++){ nMinIndex=i; for (j=i+1;j<n;j++) if (a[j]<a[nMinIndex]) nMinIndex=j; int temp=a[i]; a[i]=a[nMinIndex]; a[nMinIndex]=temp; }}//快速排序void QuickSort(int left,int right){ int i,j,t,temp; if(left>right) return; temp=a[left]; i=left; j=right; while(i!=j) { while(a[j]>=temp && i<j) j--; while(a[i]<=temp && i<j) i++; if(i<j){ t=a[i]; a[i]=a[j]; a[j]=t; } } a[left]=a[i]; a[i]=temp; QuickSort(left,i-1); QuickSort(i+1,right);} int main(){ cout<<"生成100个随机数"<<endl; srand(time(0)); //为rand()函数提供时间种子保证每次运行程序rand的结果不同 for(int i=0;i<100;i++){ a[i]=rand()%200; //随机生成100个0-200内的数 } cout<<"生成的100个随机数为"<<endl; for(int i=0;i<100;i++){ //打印生成的100个数 if((i+1)%10==0&&i) cout<<a[i]<<endl; else cout<<a[i]<<' '; } //排序 //BubbleSort(a,100); //InsertSort(a,100); //ShellSort(a,100); //SelectSort(a,100); QuickSort(0,99); //从a[0]到a[4]; cout<<"排序生成的100个随机数"<<endl; for(int i=0;i<100;i++){ if((i+1)%10==0&&i) cout<<a[i]<<endl; else cout<<a[i]<<' '; } return 0;}
阅读全文
0 0