Recursive Algorithm Learning Series 3 (quick sorting)

Source: Internet
Author: User

Returns the first two partsAlgorithmLearning:

1) recursive algorithm Learning Series 1 (divide and conquer strategy)
2) recursive algorithm Learning Series 2 (Merge Sorting)

In the previous study, we introduced an application of Recursive Algorithms in sorting: Merge Sorting. Another sort algorithm uses recursion, which is fast sorting, fast sorting is also an algorithm that uses the divide-and-conquer policy. a.r was invented to divide a data table into smaller and smaller child tables using a series of recursive calls based on the value of the central element. In each call, after multiple exchanges, the final position of the central element is finally found. Unlike the merge algorithm, quick sorting is local sorting, while merging sorting requires copying elements in a temporary vector. Next we will sort the following vectors to understand and deepen the steps of the quick sorting algorithm:

V ={ 800,150,300,650,550,500,400,350,450,400,900 };

The sorting process of the data table by using the quick sorting algorithm is as follows: The index range of vector V is: [first, last) = [0th ), the index of the central point is mid = (0 + 10)/2 = 5, and the value of the central point is V [5] = 500.

The purpose of the First Division of the quick sorting algorithm is to divide vector V into two subtables, sublist1 and sublist2, based on the value of V [5]. The values in sublist1 are smaller than V [5], the value in sublist2 is greater than V [5]. We call sublist1 the left sub-table, and sublist2 the right sub-table, and determine the final position of V [5: the following are the steps that need to be taken to achieve this goal:

1) First, exchange the central element with the element at the starting position.

2) scan the Left and Right sub-tables respectively. the start position of the left sub-Table scan is first + 1, and the right sub-table starts from last-1. The left sub-table is scanned from left to right, and the right sub-table is scanned from right to left. Until the scanning position of the left sub-table is greater than or equal to the scanning position of the right sub-table.

In step 1, obtain the following data table

500 150 300 650 550 800 400 350 450, the scan position of the left sub-table is at Index 1, and that of the right sub-table is at index 9. First, scan the left sub-table, scan the right sub-table until the data value is found to be greater than the center value of 500, and then scan the right sub-table until the data value is found to be less than the center value of 500 and the scan position of the right sub-table (scandown) it must be smaller than the starting position of the left sub-table to prevent data overflow. After finding it, swap the elements in the scanning position between the left and right sub-tables, as shown in the figure below: In the switching V [3] (650> 500) and V [8] (450 <500), continue to scan the Left and Right sub-tables, () until the condition scanup> = scandown, then, the position of scandown is the final position of the central element 500, switching V [0] and V [scandown) = V [5]. The final result data set of the first classification level is: 400,150,300,450,350,500,800,550,650,900, in this case, the left sub-table is 400,150,300,450,350, And the right sub-table is 800,550,650,900.

The next partition level is used to process the child tables generated at the previous level. The left and right child tables are processed according to the same processing method. The index position of the left sub-table is ), the index position of the right sub-table is [6, 10). The final result of processing the left sub-table (400,150,300,450,350) is as follows: 150,300,400,450,350 the final processing result of the Right sub-table is as follows: 550,650,800,900 in the processing result, 300 and 650 are the center values respectively. Their current location is the final location.

In the next processing, the sub-tables left in the previous step are always processed. When the number of sub-tables is <= 1, no sub-tables need to be processed, when the sub-table has two elements, compare the size and then swap the positions of the two elements.

The sub-tables with more than two elements are the same as the above processing steps. We compile a function for the above processing process.

Private int sort tindex (INT [] V, int first, int last), then the quick sorting algorithm is a recursive call to this function.

 

  /**/ ///   <Summary>
/// Switch location
///   </Summary>
///   <Param name = "v"> </param>
///   <Param name = "index1"> </param>
///   <Param name = "index2"> </param>
Private   Void Swrap ( Int [] V, Int Index1, Int Index2)
{
IntTemp=V [index1];
V [index1]=V [index2];
V [index2]=Temp;
}
/**/ ///   <Summary>
/// Divides the index {first, last) in vector V into two left sub-tables and right sub-tables.
///   </Summary>
///   <Param name = "v"> Vector v </Param>
///   <Param name = "first"> Start position </Param>
///   <Param name = "last"> End position </Param>
Private   Int Pivotindex ( Int [] V, Int First, Int Last)
{
If (Last = First)
{
ReturnLast;
}
If (Last - First =   1 )
{
ReturnFirst;
}
Int Mid = (First + Last) /   2 ;
Int Midval = V [Mid];
// Swap V [first] and V [Mid]
Swrap (v, first, mid );
Int Scana = First +   1 ;
Int Scanb = Last -   1 ;
For (;;)
{

While (Scana <= Scanb && V [scana] < Midval)
{
Scana++;
}
While (Scanb > First && Midval <= V [scanb])
{
Scanb--;
}
If (Scana > = Scanb)
{
Break;
}
Swrap (v, scana, scanb );
Scana ++ ;
Scanb -- ;
}
Swrap (v, first, scanb );
Return Scanb;

}
Public   Void Sort ( Int [] V, Int First, Int Last)
{
If (Last - First <=   1 )
{
Return;
}
If (Last - First =   2 )
{
// Subtables with two elements
If (V [first] > V [last -   1 ])
{
Swrap (v, first, last- 1);
}
Return ;
}
Else
{
IntPivotindex=Using tindex (v, first, last );
Sort (v, first, tindex );
Sort (v, tindex+ 1, Last );
}
}  

Quick sorting because each division can find the final position of the central value element, and the left value is smaller than the central value, and the right side is greater than the central value, the average time complexity of the merge algorithm is O (nlog2n );

The time complexity of any sort algorithm based on comparison cannot be smaller than this number unless the comparison method is not used for sorting.

AlgorithmProgram:/Files/jillzhang/quicksort.rar

Algorithm learning for the first two parts:

1) recursive algorithm Learning Series 1 (divide and conquer strategy)
2) recursive algorithm Learning Series 2 (Merge Sorting)

-------------------------------------------------------
People are old, their heads are not easy to use, and occasionally use algorithms to train their brains, which can prevent premature aging. Haha
Jillzhang jillzhang@126.com

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.