Sorting algorithm of data structure--quick sort
Code a lot of places borrowed from http://my.csdn.net/MoreWindows his thoughts,
I think the author has written well and just added some of his own understanding and instructions on his basis
If it comes to copyright issues, please contact my email and I will delete it as soon as possible
Hill sort want to close the link:
Wikipedia: https://zh.wikipedia.org/wiki/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F#C.E8.AA.9E.E8.A8.80
Baidu Encyclopedia: http://baike.baidu.com/view/19016.htm
Reference Blog: http://blog.csdn.net/morewindows/article/details/6684558
Fast sequencing provides good performance for large amounts of data,
The basic idea of a quick sort is to scan a right-to-left array first, find the number of key values that are not greater than the current definition, swap with it, then scan from left to right, find the first number that is not less than the current key value, swap them, and divide the data into two parts, with the previous part smaller than the keyword. The latter part is larger than the keyword, and then the front and back sections continue with the previously described action, which requires recursive invocation of itself.
The source code is as follows:
void Quick_sort (int array[], int arraylen)
{
Sort (array, 0, arrayLen-1);
}
void Sort (int array[], int low, int height)
{
if (Low < height)
{
int retpivotkey = partition (array, low, height);
Sort (array, low, retPivotkey-1);
Sort (Array, Retpivotkey + 1, height);
}
}
int partition (int array[], int right, int left)
{
Move all elements that are smaller than the keyword to the left of the keyword and move to the right of the keyword larger than the key
int pivotkey = Array[right];
while (right < left)
{
while (right < left && Array[left] >= pivotkey)
-left;
if (right < left)
{
array[right++] = Array[left];
}
while (right < left && Array[right] < PivotKey)
+ + right;
if (right < left)
{
Array[left--]=array[right];
}
}
Insert a keyword into the location you want to close
Array[right] = PivotKey;
Returns the location of the keyword to facilitate the next loop
return right;
}
The source code is written so that it is consistent with the previous sorting algorithm, and I have seen a combination of
void Quick_sort (int array[], int low, int height)
{
Cache low and height to provide bounds for the implementation of the following recursion
int right = low and left = height;
if (Low < height)
{
int pivotkey = Array[right];
while (right < left)
{
while (right < left && Array[left] >= pivotkey)
--left;
if (right < left)
{
array[right++] = Array[left];
}
while (right < left && Array[right] < PivotKey)
++right;
if (right < left)
{
array[left--] = Array[right];
}
}
Insert a keyword into the location you want to close
Array[right] = PivotKey;
Quick_sort (array, low, right-1);
Quick_sort (Array, right + 1, height);
}
}
I also saw an iterative version of this version from Wikipedia, when we looked at the data.
Wikipedia link: https://zh.wikipedia.org/wiki/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F#C.E8.AA.9E.E8.A8.80
The source code is as follows:
typedef struct _RANGE {
int start, end;
} Range;
Range new_range (int s, int e) {
Range R;
R.start = s;
R.end = e;
return R;
}
void swap (int *x, int *y) {
int t = *x;
*x = *y;
*y = t;
}
void Quick_sort2 (int arr[], const int len) {
if (len <= 0)
Return Error preventing Len from being negative
R[] Analog stack, p is the number of elements in the stack, r[p++] is push, r[--p] is pop and gets the element
Range *r= (range *) malloc (sizeof (range) *len);//Because the VS does not support C99 so, can only be dynamically opened up, support for the C99 may use Range R[len];
if (r = = NULL)
Return
int p = 0;
Stacking of simulation stacks
r[p++] = New_range (0, len-1);
while (p)
{
Stack out of the simulation stack
Range range = R[--p];
Ends the current loop when start is greater than end
if (Range.Start >= range.end)
Continue
int mid = Arr[range.end];
int left = Range.Start, right = range.end-1;
while (left < right)
{
Find the first element not less than mid
while (Arr[left] < mid && left < right)
left++;
Find the first element less than mid
while (Arr[right] >= mid && left < right)
right--;
Swap (&arr[left], &arr[right]);
}
Avoid cross-border array elements
if (Arr[left] >= arr[range.end])
{
Swap (&arr[left], &arr[range.end]);
}
Else
{
If the left pointer is not out of bounds
left++;
}
Put the left and right two-part range into a stack
r[p++] = New_range (Range.Start, left-1);
r[p++] = New_range (left + 1, range.end);
}
Free (R);
}
Quick Sort Summary