Quick Sort Method

Source: Internet
Author: User

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. void swap (int *x,int *y)//exchange function
    4. {
    5. int temp;
    6. temp = *x;
    7. *x = *y;
    8. *y = temp;
    9. }
    10. int Choose_pivot (int I,int j)//take two number mean function
    11. {
    12. Return ((i+j)/2);
    13. }
    14. void quicksort (int list[],int m,int n)//quick sort function, m,n are left-bounded right-bound such as 0 and 9 in 10-digit order
    15. {
    16. int key,i,j,k;
    17. If (m < N)
    18. {
    19. K = Choose_pivot (m,n); The value of the middle position is obtained
    20. Swap (&list[m],&list[k]);
    21. Key = list[m];
    22. i = m+1;
    23. j = n;
    24. While (i <= j)
    25. {
    26. While ((i <= n) && (list[i] <= key))
    27. i++;
    28. While ((j >= m) && (list[j] > Key)
    29. j--;
    30. If (i < J)
    31. Swap (&list[i],&list[j]);
    32. }
    33. Swap the position of two elements
    34. Swap (&list[m],&list[j]);
    35. Recursively sort smaller data series
    36. Quicksort (list,m,j-1);
    37. Quicksort (list,j+1,n);
    38. }
    39. }
    40. void Printlist (int List[],int n)
    41. {
    42. int i;
    43. For (i=0;i<n;i++)
    44. printf ("%d\t", list[i]);
    45. }
    46. void Main ()
    47. {
    48. const int max_elements = 10;
    49. int list[max_elements];
    50. int i = 0;

    51. Generate random numbers of fill sequences
    52. For (i = 0; I < max_elements; i++) {
    53. list[i] = Rand ();
    54. }
    55. printf ("sequence before sorting: \ n");
    56. Printlist (list,max_elements);

    57. Sort the list using quicksort
    58. Quicksort (list,0,max_elements-1);
    59. Print the result
    60. printf ("sequence after sorting using the quick sort algorithm: \ n");
    61. Printlist (list,max_elements);
    62. }
  1. This procedure is not difficult, it should be very good to understand, I take the process roughly, first of all, you have an array and three pointers, the first pointer is called the P pointer, before the end of the process it is firmly pointed to the first number, the second pointer and the third pointer, respectively, the lo pointer and the Hi pointer, Points to the leftmost value and the rightmost value, respectively. The LO pointer and the hi pointer from both sides of the same time to the middle, in the process of approaching the value of the P pointer, if the value of the LO pointer is smaller than the value of the P pointer, lo++, also small + +, and then small + +, until a value greater than the P pointer, then the sight shift to the Hi pointer, if The value of the Hi pointer is larger than the value of the P pointer, hi--, or Larger----and then large again--until a value less than the P pointer is Encountered. The value of the LO pointer and the value of the Hi pointer are then Swapped. Continue this process until two pointers meet, then the value of the P pointer and the value of the meeting to make a swap, and then return the P pointer to a new Position.

Quick Sort Method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.