Quick Sorting Algorithm quicksort (2)

Source: Internet
Author: User
1. Description

This quick sortingAlgorithmIs an improvement of the previous quick sorting algorithm quicksort.

Only the int partition (INT arry [], int start, int end) method is modified.

2. Ideas

After careful observation, we can find that swap (Arry, start, end) is required in the preceding partition method ), However, some steps in this step can be omitted. 

In the (<-end) process, when the first element is smaller than or equal to the value of begin, you only need to assign a value once to arry [start] = arry [end]; Because the current arry [start] is the value of marker;

In the (START->) process, if you encounter the first element greater than or equal to 'struct', you only need to assign a value once to arry [end] = arry [start];Because the current arry [end] value is the value of begin.

In the whole process, either start or end points to the unit stored by the consumer.,Therefore, in the whole process, the consumer does not need to be saved through swap each time, but only needs to find the consumer tkey at the end, Make arry [unique tkey] = unique. 

The specific modification is as follows:CodeInstance.

3. Improved quick sorting quicksort View code

# Include <iostream>
# Include <stdlib. h>
# Include <time. h>
Using Namespace STD;
# Define Maxnum100
/*
Improved int partition (INT arry [], int start, int end)
After careful observation, we can find that swap (Arry, start, end) is required in the preceding partition method ),
However, some steps in this step can be omitted.
In the (<-end) process, when the first element is smaller than or equal to the value of begin, you only need to assign a value once to arry [start] = arry [end];
Because the current arry [start] is the value of marker
In the (START->) process, if you encounter the first element greater than or equal to 'struct', you only need to assign a value once to arry [end] = arry [start];
Because the current arry [end] value is the value of begin,
In the whole process, either start or end points to the unit stored by the consumer.
Therefore, in the whole process, the consumer does not need to be saved through swap each time, but only needs to find the consumer tkey at the end,
Make arry [unique tkey] = unique.
*/
Int Partition ( Int Arry [], Int Start, Int End)
{
// Cout <"axis array" <Endl;
// For (INT I = start; I <= end; I ++)
// Cout <arry [I] <"";
// Cout <Endl;
Int Vertex = arry [start];
While (Start <End)
{
While (Arry [end]> begin & start <End)
End --;
Arry [start] = arry [end];
While (Arry [start] <begin & start <End)
Start ++;
Arry [end] = arry [start];
}
Arry [start] = begin;
/* Cout <"output axis position:" <start <Endl; */
Return Start;
}
Void Qsort ( Int Arry [], Int Start,Int End)
{
If (Start <End)
{
Int Required tkey = partition (Arry, start, end );
Qsort (Arry, start, Volume tkey- 1 );
Qsort (Arry, javastkey + 1 , End );
}
}
Void Quicksort ( Int Arry [], Int Len)
{
Qsort (Arry, 1 , Len );
}
Void Main ()
{
Int N;
Int Arry [maxnum] = { 0 };
Srand (Time (null ));
Cout < " Input random array length N: " <Endl;
Cin> N;
Cout < " Generation length: " <N < " Random array of less than 100 " <Endl;
Cout < " Before sorting: " <Endl;
For ( Int I = 1 ; I <= N; I ++)
{
Arry [I] = rand () % 100 ;
Cout <arry [I] < " " ;
}
Cout <Endl;
Quicksort (Arry, N );
Cout < " After sorting: " <Endl;
For ( Int I = 1 ; I <= N; I ++)
{
Cout <arry [I] < " " ;
}
Cout <Endl;
System ( " Pause " );
}
/*
Input random array length N:
10
Generate a random array of less than 100 of the length of 10
Before sorting:
91 67 99 37 40 70 49 71 43 42
After sorting:
37 40 42 43 49 67 70 71 91 99
Press any key to continue...
*/
PS: 2012-5-4

Today, I found that I used to think that I was wrong about the fast sorting, so I didn't consider the case where the number to be sorted is equal. If there is the same number in the sorted array, an endless loop will occur, there is no result. The modification is to modify the partition function. The modification is as follows:

View code

Int Partition ( Int Arry [], Int Start, Int  End ){  //  Cout <"axis array" <Endl;  //  For (INT I = start; I <= end; I ++)  //  Cout <arry [I] <"";  //  Cout <Endl;   Int Signature =Arry [start];  While (Start < End ){  While (Arry [end]> = begin & start < End) End -- ; Arry [start] = Arry [end];  While (Arry [start] <= begin & start < End) Start ++ ; Arry [end] = Arry [start];} arry [start] =Break;  /*  Cout <"output axis position:" <start <Endl;  */      Return  Start ;} 

The only modification is to change the original arry [end]> begin to arry [end]> = begin, arry [start] <begin to arry [start] <= begin

Quick exercise (PS: 2012-10-2) View code

# Include <iostream> # Include <Stdlib. h> Using   Namespace  STD;  Int Partition (Int Arry [], Int Start, Int  End ){  Int Signature = Arry [start];  While (Start < End ){  While (Start <End & arry [end]> = begin) //  Find the first element smaller than the limit. If arry [end]> = Limit, filter it out. End -- ; Arry [start] =Arry [end];  While (Start <End & arry [start] <= begin) //  Locate the first element greater than limit. Start ++ ; Arry [end] = Arry [start];} arry [start] = Break;  Return Start; //  In this case, start = end, so the return start is the same as the return end.  }  Void Quicksort ( Int Arry [], Int Start, Int  End ){  If (Start> = End)  Return  ;  Int Required tkey = Partition (Arry, start, end); quicksort (Arry, start, iterator tkey - 1  ); Quicksort (Arry, javastkey + 1  , End );}  Void Printarry ( Int Arry [], Int  Len ){  For ( Int I = 0 ; I <Len; I ++ ) Cout <Arry [I] < "   "  ; Cout < Endl ;}  Int  Main (){  Int Arry [] = {1 , 2 , 3 , 2 , 2 , 2 , 5 , 4 , 2  };  Int Len = Sizeof (Arry )/ Sizeof ( Int );//  Evaluate the array Length Quicksort (Arry, 0 , Len- 1 ); //  Fast sorting Printarry (Arry, Len ); //  Print Array System ( "  Pause  "  );  Return   0  ;} 

 

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.