Fast sequencing of algorithm research

Source: Internet
Author: User

Quick Sort (Quicksort) is an improvement to the bubbling sort. by C. A. R. Hoare was introduced in 1962. Its basic idea is: by a trip to sort the data to be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then the two parts of the data are quickly sorted by this method, the entire sorting process can be recursive, so as to achieve the entire data into an ordered sequence.
The above is from Baidu Encyclopedia.
Worst time complexity:
O (n two times);
The best time complexity:
O (n);
The basic idea of quick sorting
1. Decomposition:
In D[I...J...N] 's data, find a benchmark point d[j], divide the data into about two parts,
Left equals d[i...j-1]
Right equals D[J+1...N]
2. Solving
The recursive call division function solves the two data division of the left and right interval.
3. Merge the left and right two sorted data
Visible fast sorting algorithm time complexity is equal to the time spent dividing.
Let's talk about the key second part of the solution

Partitioning steps
Data D[low...high];
① set two variable i,j, pointing to the low and high of the data, respectively. Take any of these data as Datum pivot.
② move J from right to left to find data that is smaller than pivot, and then assign the data that J points to i,d[i]=d[j]
Move I from left to right, look for data larger than pivot, and then assign the data I pointed to J when I find it. D[j]=d[i];
And so on constantly exchanging direction, know i=j time to stop. This time I's position is where the datum pivot is located.

Let's go through this process through the data.
Quotations are not well described in words. So I use strokes in the book to come out.
I can't write well. Do not spray. ^.^


Code written by PHP

123456789101112131415161718192021222324 Functionquick_sort (& $array, $x, $y) {     $i = $x;     $j = $y;      $key = $array [$x];//datum     while ($i! = $j) {         //$j--Find a value smaller than the benchmark $key         while ($i < $j && $array [$ J] >= $key) {             $j--;         }        //  if ($i < $j)           $array [$i] = $array [$j];         //$i + + finds values larger than the benchmark $key         while ($i < $j && $array [$i] < = $key) {             $i ++;         }        //if ($i < $j)           $array [$j] = $array [$i];    }     $array [$i] = $key;     if ($x < $i-1) data         quick_sort of  //left interval ($ Array, $x, $i-1),     if ($y > $i + 1)//right-interval data          Quick_sort ($array, $i +1, $y);}

  http://bbs.dzwww.com/forum.php?mod=viewthread&tid=53241034

http://bbs.dzwww.com/forum.php?mod=viewthread&tid=53241055

http://bbs.dzwww.com/forum.php?mod=viewthread&tid=53241072

Fast sequencing of algorithm research

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.