|
1楼 { int i,j; char ch; ch=arr[startPos]; i=startPos; j=endPos; while(i<j) { while(arr[j]>=ch && i<j)--j; arr[i]=arr[j]; while(arr[i]<=ch && i<j)++i; arr[j]=arr[i]; } arr[i]=ch; if(i-1>startPos) quickSort(arr,startPos,i-1); if(endPos>i+1) quickSort(arr,i+1,endPos); } void main() { char ch[]="qwertyuiopasdfghjklzxcvbnm"; quickSort(ch,0,25); printf("\n%s\n",ch); } |
|
|
|
| 222.33.200.* |
3楼 |
|
|
| 218.69.134.* |
4楼 |
|
|
| 222.50.13.* |
5楼 #include<string.h> #include<stdlib.h> #include<stdio.h> struct student{ int xuehao; char name[20]; int chengji; }; typedef struct student stu; sortbyxuehao(const void *,const void *); sortbyname(const void *,const void *); sortbychengji(const void *,const void *); void main() { int i; stu a[3]; int choice; int (*p)(const void * ,const void *); printf("please input record:\n"); printf("xuehao name chengji:\n"); for(i=0;i<3;i++) { scanf("%d",&a[i].xuehao); scanf("%s",a[i].name); scanf("%d",&a[i].chengji); } printf("choice_1: sorted xuehao:\n"); printf("choice_2: sorted name\n"); printf("choice_3: sorted chengji\n"); scanf("%d",&choice); while(choice!=0) { if(choice==1) p=sortbyxuehao; if(choice==2) p=sortbyname; if(choice==3) p=sortbychengji; qsort(a,3,sizeof(stu),p); if(choice==1) for(i=0;i<3;i++) printf("\n%d\t%s\t%d",a[i].xuehao,a[i].name,a[i].chengji); if(choice==2) for(i=0;i<3;i++) printf("\n%s\t%d\t%d",a[i].name,a[i].xuehao,a[i].chengji); if(choice==3) for(i=0;i<3;i++) printf("\n%d\t%s\t%d",a[i].chengji,a[i].name,a[i].xuehao); printf("\n"); scanf("%d",&choice); } } sortbyxuehao(const void *p,const void *q) { stu *x,*y; x=(stu*)p; y=(stu*)q; return (-((*x).xuehao-(*y).xuehao)); } sortbyname(const void *p,const void *q) { stu *x,*y; x=(stu*)p; y=(stu*)q; return strcmp((*x).name,(*y).name); } sortbychengji(const void *p,const void *q) { stu *x,*y; x=(stu*)p; y=(stu*)q; return ((*x).chengji-(*y).chengji); } |
|
|
| 61.191.140.* |
6楼 “蜗牛”差不多... 楼主是对的,和书上几乎一样,只不过看不懂!!! |
|
|
| 220.170.23.* |
7楼 |
|
|
| 221.13.11.* |
8楼 |
|
|
| 221.13.11.* |
9楼 |
|
|
| 218.3.211.* |
10楼 |
|
|
| 210.29.140.* |
11楼 |
|
|
|
12楼 main() { char sz[]="qwertyuiopasdfghjklzxcvbnm"; quickSort(sz,0,25); printf("\n%s\n",sz); getch(); } void quickSort(char* arr,int startPos, int endPos) { int i,j,a; char ch; j=endPos; for(i=startPos;j>i;--j) { for(a=i;a<j;++a) { if( arr[a]>arr[a+1]) { ch=arr[a+1]; arr[a+1]=arr[a]; arr[a]=ch; } } } } |
|
|
|
| 61.141.158.* |
13楼 void QuickSort(int array[], int low, int high) { int i, j, temp; i = low; j = high; temp = array[i]; while (i < j) { while (i<j && array[j]>temp) --j; array[i] = array[j]; while (i<j && array[i]<=temp) ++i; array[j] = array[i]; array[i] = temp; QuickSort(array, low, i-1); QuickSort(array, i+1, high); } } |
|
|
| 61.141.158.* |
14楼 void QuickSort(int array[], int low, int high) { int i, j, temp; i = low; j = high; temp = array[i]; if (i >=j) return; while (i < j) { while (i<j && array[j]>temp) --j; array[i] = array[j]; while (i<j && array[i]<=temp) ++i; array[j] = array[i]; } array[i] = temp; QuickSort(array, low, i-1); QuickSort(array, i+1, high); } |
|
|
| 219.236.31.* |
15楼 |
|
|
| 218.58.249.* |
17楼 |
|
|
| 221.237.177.* |
18楼 别得了好处还骂楼主垃圾. |
|
|
| 210.22.29.* |
19楼 |
|
|
| 61.28.52.* |
20楼 |
|
|
|
21楼 哈,解释的好幼稚。 ch=arr[startPos]; //挑出一颗作为基准的白菜 i=startPos; //开始的白菜 j=endPos; //结束的白菜 while(i<j) //白菜没挑完 { while(arr[j]>=ch && i<j)--j; //这棵比标准白菜大,跳过,直到遇到小的 arr[i]=arr[j]; //把这棵小的放到前面 while(arr[i]<=ch && i<j)++i; //跳过比标准小的,直到遇 到大白菜 arr[j]=arr[i]; //把大白菜扔到刚才遇见小白菜的地方 etc. |
|
|
|
| 210.185.254.* |
22楼 |
|
|
| 58.44.131.* |
23楼 { k=i; For(j=i+1;j<10;j++) If(MyVar[j]>MyVar[i]) k=j; If(k!=i) {t=MyVar[i];MyVar[i]=MyVar[k];MyVar[k]=t}; } |
|
|
| 222.95.174.* |
24楼 for(i=0;i<10-1;i++) { k=i; for(j=i+1;j<10;j++) if(MyVar[j]>MyVar[k/*不是i*/]) k=j; if(k!=i) {t=MyVar[i];MyVar[i]=MyVar[k];MyVar[k]=t}; } |
|
|
| 218.9.166.* |
25楼 快速排序是很有效率也很常用的算法,复杂度在n*log2n到n*n之间. |
|
|
| 218.75.144.* |
26楼 |
|
|
| 222.246.52.* |
27楼 |
|
|
| 222.39.27.* |
28楼 |
|
|
| 222.39.27.* |
29楼 |
|
|
|
30楼 程序如下 #include<stdio.h> #include<stdlib.h> void retArray(int*,int); void insertsort (int x[],int n); void bubblesort(int x[],int n); void selectsort(int x[],int n ); void fastsort(int *x,int l,int r); int main() { int r[100]; int n=0; int i=0; char choice; printf("how many numbers do you want to sort?(numbers<=99)!\n"); scanf("%d",&n); retArray(r,n); printf("OUT:\n"); for(i=0;i<n;i++) { printf("%d\n",r[i]); } while(true) { printf("which kind do you want to sort numbers?insert,bubblesort,select,fast or quit?,I/B/S/F/Q\n"); scanf("%c",&choice); getchar(); if(choice=='I'||choice=='i') { insertsort (r,n); for(i=0;i<n;i++) { printf("%d\n",r[i]); } } if(choice=='B'||choice=='b') { bubblesort(r,n); for(i=0;i<n;i++) { printf("%d\n",r[i]); } } if(choice=='S'||choice=='s') { selectsort(r,n); for(i=0;i<n;i++) { printf("%d\n",r[i]); } } if(choice=='F'||choice=='f') { fastsort(r,r[0],r[n-1]); for(i=0;i<n;i++) { printf("%d\n",r[i]); } } if(choice=='Q'||choice=='q') { printf("you can safely close the program now!"); getchar(); return 0; break; } } //while }//main() void retArray(int *a,int n) { int i; srand(100); for(i=0;i<n;i++) { a[i]=rand(); } fflush(stdin); return; } //OK! void insertsort (int *x, int n) { int a; int i,j; for (i=0;i<n-1;i++) { a=x[i+1]; j=i; while(j>-1&& a<x[j]) { x[j+1]=x[j]; j--; } x[j+1]=a; } } //OK! void bubblesort(int *x,int n) { int i,j,flag; int swap; for(i=0;i<n-1;i++) { flag = 1; for(j=0;j<n-1;j++) if(x[j]>x[j+1]) { flag = 1; swap = x[j]; x[j] = x[j+1]; x[j+1] = swap; } if(flag==0)return; } } //OK! void selectsort (int *x,int n) { int i,j,small; int temp; for (i=1;i<n-1;i++) { small=i; for (j=i+1;j<n;j++) if (x[j]<x[small]) { small=j; } if (small!=i) { temp = x[i]; x[i] = x[small]; x[small] = temp; } } }//ok! void fastsort(int *x,int l,int r) { int i,j; int temp; i=l;j=r; temp = x[l]; while(i<j) { while ( i<j && temp<=x[j]) j--; if(i<j) { x[i]=x[j]; i++; } while (i<j && temp>=x[i]) i++; if(i<j) { x[j]=x[i]; j--; } } x[i]=temp; if(l<i)fastsort(x,l,i-1); if(i<r)fastsort(x,j+1,r); } |
|
|
|
