编写一个C++快速排序的程序
- 提问者网友:咪咪
- 2021-07-20 10:18
- 五星知识达人网友:千杯敬自由
- 2021-07-20 11:27
你说的应该是数组排序了?
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
void swap(int *a,int *b){
int t;
t=*a;*a=*b;*b=t;
}
void main(){
int a[10][10],i,j;
srand(1);
for(i=0;i<10;i++)
for(j=0;j<10;j++)
a[i][j]=rand()%100;
for(i=0;i<9;i++){
for(j=i+1;j<10;j++){
if(a[i]>a[j])swap(&a[i],&a[j]);
}
}
for(i=0;i<10;i++)
for(j=0;j<10;j++)
printf("%d,",a[i][j]);
getch();
}
- 1楼网友:狂恋
- 2021-07-20 12:59
class Sort { public: void setnumber(); void putnumber(); void quicksort(int p,int r);
private: int num[N]; int partition(int p,int r); };
int main() { char ch; Sort test; do { cout<<"QUICK SORT\n====================\n"; test.setnumber(); cout<<"QUICK SORT\n====================\n"; cout<<"Before sort:\n"; test.putnumber(); cout<<"begin sort:\n"; test.quicksort(0,N-1); test.putnumber(); cout<<"SUCCESS!\n"; cout<<"====================\nPress Y to continue and N to quit:\n"; cin>>ch; }while(ch=='y'||ch=='Y'); return 0; } void Sort::setnumber() { int i; cout<<"Please input "<<N<<" integers:\n"; for(i=0;i<N;i++) { cin>>num[i]; } } void Sort::putnumber() { int i; for(i=0;i<N;i++) { cout<<num[i]<<" "; } cout<<endl; } void Sort::quicksort(int p,int r) { int q; if(p<r) { q=partition(p,r); //q=partition_descending(p,r); quicksort(p,q); quicksort(q+1,r); }
} int Sort::partition(int p,int r) { int temp,i,j,x; while(1) { j=r; i=p; x=num[p]; while(num[j]>x)j--; while(num[i]<x)i++; if(i<j) { temp=num[j]; num[j]=num[i]; num[i]=temp; } else { return j; } } }