Sorting algorithm-(1) Quick sort

Source: Internet
Author: User

Brief description

Previous data structure and algorithm courses to learn the algorithm are almost forgotten, so still want to tidy up the knowledge point review, good memory than bad pen.

I'll start with one. Introduce these algorithms and implementations, and then summarize them at a later time. First, start with the quick sort.

The algorithm idea is simple to describe:

A quick sort is an essential improvement to the bubbling sort. Its basic idea is that the length of the sequencing sequence can be drastically reduced after a scan. In a bubbling sort, a scan can only ensure that the number of maximum values is moved to the correct position, while the length of the sequence to be sorted may be reduced by only 1. Quick sort by a scan, you can make sure that the number of the left is smaller and the number on the right is larger than it. It then uses the same method to manipulate the left and right sides of the number until there is only one element to the left of the datum point.

Fast sequencing is not stable. Optimal condition algorithm time complexity O (nlog2n), Worst O (n2)

Algorithm implementation
 Public classsort{ Public Static voidMain (string[] args) {int[] arr = {4,3,6,1,5,2}; Kssort (arr,0,arr.length-1);      System.out.println (arrays.tostring (arr)); }      Private Static voidKssort (int[] A,intLowintHigh ) {          if(low<High ) {              intMID =Getmid (A,low,high); Kssort (A,low,mid-1); Kssort (A,mid+1, high); }       }       Private Static intGetmid (int[] A,intLowintHigh ) {             intBase =A[low];  while(low!=High ) {                     while(lowbase) { High--;                   } swap (A,low,high);  while(lowbase) { Low++;            } swap (A,low,high); }            returnA[low]; }      Private Static voidSwapint[] A,intLowintHigh ) {            inttemp =A[low]; A[low]=A[high]; A[high]=temp; }  }  

Okay, look at the results.

Sorting algorithm-(1) Quick sort

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.