Java quick sorting

Source: Internet
Author: User

 

1. Simple Steps (numbers increase progressively from left to right)

1. Identify the principal element, which can be any element. Here we always select the last most principal element.

2. Search from left to right, and place smaller than the principal component to the left.

3. After the end is found, the principal component is exchanged with the next number on the left that is less than the principal component.

In this way, the left side of a principal component is smaller than the principal component, and the right side is greater than the principal component.


II. Implementation Code

 

[Java] 
  1. Public class QuickSort {
  2.  
  3. Public static void main (String [] args ){
  4. Int [] arr = {6, 9, 2, 5, 8, 6 };
  5.  
  6. QuickSort quickSortSample = new QuickSort ();
  7. QuickSortSample. quickSort (arr, 0, arr. length-1 );
  8.  
  9. For (int j = 0; j <arr. length; j ++ ){
  10. System. out. print (+ arr [j]);
  11. }
  12. }
  13.  
  14. Public void quickSort (int data [], int start, int end ){
  15. If (start <end ){
  16. Int partitionIndex = partition (data, start, end );
  17. QuickSort (data, start, partitionIndex-1 );
  18. QuickSort (data, partitionIndex + 1, end );
  19. }
  20. }
  21.  
  22. Public int partition (int data [], int start, int end ){
  23. Int key = data [end]; // use the last element. data [hi] is the primary element.
  24. Int I = start-1;
  25. For (int j = start; j <end; j ++ ){
  26. If (data [j] <= key ){
  27. I = I + 1;
  28. Swap (data, I, j );
  29. }
  30. }
  31. Swap (data, I + 1, end );
  32.  
  33. Return I + 1;
  34. }
  35.  
  36. Private void swap (int data [], int I, int j ){
  37. Int temp = data [I];
  38. Data [I] = data [j];
  39. Data [j] = temp;
  40. }
  41.  
  42. }

     

     

    Iii. Step-by-step demonstration

     

    6, 9, 2, 5, 8, 6

     


    Current point Condition of exchange Exchanged Array
    I = 0, j = 0 6 <= 6 6 9 2 5 8 6
    I = 1, j = 2 2 <= 6 6 2 9 5 8 6
    I = 2, j = 3 5 <= 6 6 2 5 9 8 6
    I = 3 I was six times smaller than I was. 6 2 5 6 8 9


    Current point Condition of exchange Exchanged Array
    I = 0, j = 1 2 <= 5 2 6 5 6 8 9
    I = 1 I was less than five 2 5 6 6 8 9


    Current point Condition of exchange Exchanged Array
    I = 4, j = 4 8 <= 9 2 5 6 6 8 9
    I = 5 I was always 9 small 2 5 6 6 8 9



    Iv. References

     

    WIKI-sorting algorithm http://zh.wikipedia.org/wiki/%E6%8E%92%E5%BA%8F%E7% AE %97%E6%B3%95
    A series of Classical Vernacular algorithms-Quick Sort-quick http://blog.csdn.net/morewindows/article/details/6684558
    Http://blog.csdn.net/v_JULY_v/article/details/6116297 of Quick Sort Algorithm
    Deep analysis and http://blog.csdn.net/v_july_v/article/details/6211155 of the continuous and fast Sorting Algorithm
    12 continued: http://blog.csdn.net/v_july_v/article/details/6262915 for all versions of the fast sort algorithm c/c ++

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.