Sort algorithm Four: Quick sort (Quicksort)

Source: Internet
Author: User

Quick Sort (Quicksort), because of its fast sort of name, although TA's average time complexity is O (nlgn), but from the subsequent simulation results, TA is faster than merge sorting and heap sorting.

The idea of division and treatment is also used in quick sorting.

(i) Algorithm implementation

1 protected voidQuicksortint[] Array,intFirstintLast ) {2 3         intPivot =Array[first];4         inti =First ;5         intj = last-1;6         BooleanSerachbig =true;7          while(I <j) {8             if(serachbig) {9                 if(Array[j] <pivot) {TenArray[i] =Array[j]; Onei++; ASerachbig =false; -}Else { -j--; the                 } -}Else { -                 if(Array[i] >pivot) { -ARRAY[J] =Array[i]; +j--; -Serachbig =true; +}Else { Ai++; at                 } -             } -         } -Array[i] =pivot; -  -         if(I-first > 1) { in quicksort (array, first, i); -         } to         if(Last-i > 2) { +Quicksort (Array, i + 1, last); -         } the}
Quicksort

1) The average time complexity of fast sequencing is also O (NLGN)

2) Quick Sort is in-situ sort

(ii) Simulation results

**************************************************
Number to Sort is:2500
Array to sort is:{378132,303655,213274,506865,348563,122685,857064,624884,376943,281167 ...}
Cost time of "QuickSort" is (milliseconds): 0
Sort result of "QuickSort": {93,862,991,1285,2737,2938,3119,3372,3933,4647 ...}
**************************************************
Number to Sort is:25000
Array to sort is:{737677,533972,6498,772664,516805,635063,278963,284929,577222,593745 ...}
Cost time of "QuickSort" is (milliseconds): 2
Sort result of "QuickSort": {1,247,270,375,386,428,431,515,588,623 ...}
**************************************************
Number to Sort is:250000
Array to sort is:{481818,650680,957183,733420,611440,739781,495686,560166,942993,492550 ...}
Cost time of "QuickSort" is (milliseconds): 23
Sort result of "QuickSort": {0,6,10,17,20,22,23,26,32,37 ...}

Related code:

1  PackageCom.cnblogs.riyueshiwang.sort;2 3 Importjava.util.Arrays;4 5  Public classQuickSortextendsAbstractsort {6 7 @Override8     protected voidSortint[] tosort) {9Quicksort (tosort, 0, tosort.length);Ten     } One  A     protected voidQuicksortint[] Array,intFirstintLast ) { -  -         intPivot =Array[first]; the         inti =First ; -         intj = last-1; -         BooleanSerachbig =true; -          while(I <j) { +             if(serachbig) { -                 if(Array[j] <pivot) { +Array[i] =Array[j]; Ai++; atSerachbig =false; -}Else { -j--; -                 } -}Else { -                 if(Array[i] >pivot) { inARRAY[J] =Array[i]; -j--; toSerachbig =true; +}Else { -i++; the                 } *             } $         }Panax NotoginsengArray[i] =pivot; -  the         if(I-first > 1) { + quicksort (array, first, i); A         } the         if(Last-i > 2) { +Quicksort (Array, i + 1, last); -         } $     } $  -      Public Static voidMain (string[] args) { -          for(intj = 0, n = 2500; J < 3; j + +, n = n * 10) { the System.out -. println ("**************************************************");WuyiSystem.out.println ("Number to Sort is:" +n); the             int[] array = Commonutils.getrandomintarray (n, 1000000); -System.out.print ("Array to sort is:"); Wu Commonutils.printintarray (array); -  About             int[] Array1 =arrays.copyof (array, n); $             NewQuickSort (). Sortandprint (array1); -         } -     } -  A}
Quicksort.java

Sort algorithm Four: Quick sort (Quicksort)

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.