快速排序(未成功)

时间:2020-10-20 16:26:37   收藏:0   阅读:21
 1 #include<bits/stdc++.h>
 2 void quickSort(int a[],int first,int end)
 3 {
 4     if(first==end) return;
 5     int i = first,j = end,temp;
 6     while(i<j){
 7         while(i<j&&temp<=a[j]){
 8             j--;
 9         }
10         if(i<j){
11             a[i++] = a[j];
12         }
13         while(i<j&&temp>=a[i]){
14             i++;
15         }
16         if(i<j){
17             a[j--] = a[i];
18         }
19     }
20     a[i] = temp;
21     quickSort(a,first,i-1);
22     quickSort(a,i+1,end);
23 }
24 int main(){
25     int arr[100] = { 49, 38, 65, 97, 23, 22, 76, 1, 5, 8, 2, 5, -1, 22 };
26     int len = 0,i;
27     for(i=0;;i++){
28         if(arr[i]==0) break;
29         else{
30             len++;
31         }
32     }
33     printf("%d\n",len);
34     quickSort(arr,0,len-1);
35     for(i=0;i<len;i++){
36         printf("%d ",arr[i]);
37     }
38     return 0;
39 }

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!