#include <iostream>using namespace std; void swap(int *p,int i,int j){ int temp=p[i]; p[i]=p[j]; p[j]=temp;}//插入排序void insertSort(int *A,int n){ for(int i=0;i<n;i++) for(int j=1;j<=i;j++) if(A[i]<A[j]) swap(A,i,j);}//稍改进的冒泡排序算法。加标志tag.void ebuSort(int *A,int n){ int tag=1; for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) if(A[i]<A[j]) { swap(A,i,j); tag=0; } if(tag==1) break; }}//选择排序void selectSort(int *A,int n){ int temp; for(int i=0;i<n;i++) { temp=i; for(int j=i+1;j<n;j++) if(A[temp]>A[j]) temp=j; swap(A,i,temp); }}void print(int *A,int n){ cout<<"print all number in sort: "; for(int i=0;i<n;i++) cout<<A[i]<<" "; cout<<endl;}int main(){ int n; cout<<"Enter the sum of number:"<<endl; cin>>n; int *A=new int[n]; cout<<"Enter the number:"<<endl; for(int i=0;i<n;i++) cin>>A[i]; insertSort(A,n); print(A,n); ebuSort(A,n); print(A,n); selectSort(A,n); print(A,n); delete []A; return 0;}

评论