快速排序

 1 //
 2 //  main.c
 3 //  快速排序
 4 //
 5 //  Created by 丁小未 on 13-7-16.
 6 //  Copyright (c) 2013年 dingxiaowei. All rights reserved.
 7 //
 8 
 9 #include <stdio.h>
10 
11 int Qsort(int start,int length,int a[])//start排序的起始,length是要排序序列长度
12 {
13     int x = a[start];
14     int i,j;
15     i = start;  //起始位置
16     j = length -1;  //长度
17     while(i < j)
18     {
19         if(x < a[j])
20             j--;
21         else if(x > a[j])
22         {
23             a[i] = a[j];
24             a[j] = x;
25             i++;
26         }
27         else if(x < a[i])
28         {
29             a[j] = a[i];
30             a[i] = x;
31             j--;
32         }
33         else
34             i++;
35     }
36     if(start < length-1)
37     {
38         Qsort(start,i,a);
39         Qsort(i+1,length,a);
40     }
41 }
42 
43 int main(int argc, const char * argv[])
44 {
45     int N;
46     printf("请输入要排序的个数N:");
47     scanf("%d",&N);
48     printf("请输入你要排序的%d个数:
",N);
49     int array[30];
50     for (int i=0; i<N; i++) {
51         scanf("%d",&array[i]);
52     }
53     printf("您输入的数组是:
");
54     for (int i=0; i<N; i++) {
55         printf("%d ",array[i]);
56     }
57     //快速排序
58     Qsort(0, N, array);
59 
60     printf("
快速排序后的数组是:
");
61     for (int i=0; i<N; i++) {
62         printf("%d ",array[i]);
63     }
64     return 0;
65 }
感谢来访,共同学习!
原文地址:https://www.cnblogs.com/dingxiaowei/p/3197546.html