快速排序算法

 1 #include<stdio.h>

 2 

 3 void quicksort(int *array,int left,int right)

 4 {

 5     if(left<right)

 6     {

 7         int temp=array[left];

 8         int low=left;

 9         int high=right;

10         while(low<high)

11         {

12             while(low<high&&array[high]>temp)

13             {

14                 high--;

15             }

16 

17             array[low]=array[high];

18 

19             while(low<high&&array[low]<temp)

20             {

21                 low++;

22             }

23 

24             array[high]=array[low];

25         }

26         array[low]=temp;

27         quicksort(array,left,low-1);

28         quicksort(array,low+1,right);

29     }

30 }

31 

32 

33 int main()

34 {

35     int array[10]={0,2,1,4,3,5,8,7,6,9};

36     int i,left=0,right=9;

37 

38     printf("The before Order:\n");

39     for(i=0;i<10;i++)

40         printf("%d ",array[i]);

41 

42         printf("\n");

43 

44     quicksort(array,left,right);

45 

46     printf("The after Order:\n");

47     for(i=0;i<10;i++)

48         printf("%d ",array[i]);

49     return 0;

50 }

 


运行结果图:


 

你可能感兴趣的:(快速排序)