three ways to get a quick line
1, the earliest method
(1) Idea: Give a key value (default number in the first position of the array), starting from the beginning to find a number greater than key, the end of the array to find less than the number of keys, find the next two positions on the exchange of the number
(2) Drawing comprehension:
(3) Code:
int Partition2 (Int*arr, int left, int. right)
{
ITN start=left;
int key = Arr[left];
while (left < right)
{
while (left < Right&&arr[left] <= key)
left++;
while (Left<right&&arr[right]>=key)
right--;
if (left < right)
swap (Arr[left], arr[right]);
}
For example {5,6},key is 5, do not judge the exchange, the array is unordered
if (Arr[left]<key)//Attention point, remember to determine the
swap (Arr[left], Arr[start]);
return left;
}
2. Digging Pit method
(1) Idea: Give a key value (default number in the first position of the array), and dig a hole in that position. A number is missing from this position, and then the right side of the array is traversed, looking for a number that is smaller than key, and digging up the number to pits the previous pit. Then, from the front of the array, look for a number larger than key, fill the hole. Until the left and right meet, and finally put the key on the pit
(2) Drawing comprehension:
(3) Code:
int Partition (Int*arr, int start, int end)
{
int key = Arr[start];
while (Start < end)
{
while (Start < End&&key <= Arr[end])//Note to write =
end--;
if (Start < end)
arr[start]= Arr[end];
while (Start < End&&key >= Arr[start])
start++;
if (Start < end)
Arr[end] = Arr[start];
}
Arr[start] = key;
return start;
}
3, before and after the pointer method
(1) Train of thought: set the front and back pointers, the previous pointer is followed by + +, after the pointer only when the number is less than the key point of + +. When the after pointer + +, and its + + value is not equal to the previous pointer, then the number of two positions on the exchange. When the pointer is to the last position, the value of the key is swapped to the back position of the previous pointer.
(2) Drawing comprehension:
(3) Code:
int partion (int*arr,int start, int end)
{
int key = Arr[end];
int ps = start-1;
int pf = start;
while (PF < end)
{
if (ARR[PF] < key&& (++ps! = PF))
swap (ARR[PF], arr[ps]);
++PF;
}
Swap (Arr[end], arr[++ps]);
return PS;
}
two ways to realize the fast line
1. Recursion
void Qsort (Int*arr, int start, int end)
{
if (Start < end)
{
int index = Partition3 (arr, start, end);
qsort (arr, start, index-1);
Qsort (arr, index + 1, end);
}
}
2. Circulation
void Qsort1 (Int*arr, int start, int end)
{
stack<int>coll;
Coll.push (end);
Coll.push (start);
while (!coll.empty ())
{
int left = Coll.top ();
Coll.pop ();
int right = Coll.top ();
Coll.pop ();
int index = Partition3 (arr, left, right);
if (left < index-1)
{
coll.push (index-1);
Coll.push (left);
}
if (index + 1 < right)
{
coll.push (right);
Coll.push (index + 1);}}
Three, the time complexity of the fast row
The best scenario for a quick row: The value of the key as shown above is taken to the middle value, the least recursive number
The worst scenario: The key value is taken to the boundary value (maximum or minimum), the number of recursion is N, the time complexity is O (N) Four, the optimization of the fast row
1, three take the method: for the key to optimize the location of the value, it is possible to take the key to the boundary value, resulting in the efficiency of the fast row lower. The specific implementation is to judge the value of the last three points, take the middle value as key.
int Midpos (Int*arr, int left, int.)
{
int mid = left + (right-left)/2;
if (Arr[left] > Arr[right])
{
if (Arr[mid] > Arr[left])
return left;
else if (Arr[mid] < arr[right])
return right;
else
return mid;
}
else
{
if (Arr[mid]>arr[right])
return right;
else if (Arr[mid] < Arr[left])
return left;
else
return mid;
}
return-1;
}
2, if the length of a period of less than 13 of the data to sort, the use of direct insertion method to replace the fast row, can effectively reduce the recursion depth.