• C++ 排序 projectCpp_排序

    #include<iostream> #include<stdlib.h> #include<iomanip> using namespace std; void ssort(float *, int ); int main(void) { int i, seed=6523, n=20; srand(seed); float list[20], *pf; for(i=0; i<n; i++) { //生成n 个随机数并输出 //cout<<seed<<endl; list[i] = rand( ); cout<<setw(8)<<list[i]<<" "; if ((i+1)%8==0) cout<<endl; } cout<<endl; pf=&list[0]; ssort (pf, n); //对n 个数排序 i=0; while ( i<n ) { //输出排序的n 个数 cout<<setw(8)<<list[i]<<" "; i++; if (i % 8 == 0) cout<<endl; } cout<<endl; } void swap1(float *a, float *b){ //交换两个变量的值 float temp; temp = *a; *a = *b; *b = temp; } void ssort (float *a, int m){ int i, j, index; float elem; for (i=0;i<m-1;i++) { elem = *(a+i); index = i; for (j=i+1;j<m;j++) if (*(a+j)>elem){ elem = *(a+j); index = j; } swap1(a+i, a+index); } }

    0
    101
    920B
    2015-11-21
    16
关注 私信
上传资源赚积分or赚钱