The idea of quick sorting is divided into two parts of the data to be sorted into separate, part of the data is smaller than the other part of the data, and then according to this method of the two parts of the data are quickly sorted, the entire sequencing process can be recursive, in order to achieve the entire data into an ordered sequence.
The code is as follows:
basic structure of all sorting algorithms
typedef struct
{
int data[max_size + 1]; data
int length; length
}sqlist;
Exchange two data
void Swap (sqlist *list,int i,int j)
{
if (!list) return;
int temp = list->data[i];;
list->Data[i] = list->data[j];
list->Data[j] = temp;
}
void printfsqlist (sqlist *list)
{
if (list) {
for (int i =0; i< list->length; i++) {
printf("%d",list->data[i]);
}
}
}
swaps the records of a list sub-table, determines the pivot, and returns its position
int Partition (sqlist *list,int low ,int. )
{
int pivotkey;
int m = low + (high-low)/2;
if (list->data[low]>list->data[high]) {
Swap(list, low, high); swap left and right end, smaller at the beginning
}
if (list->data[m]>list->data[high]) {
Swap(list, high, m); swap the middle and right end to make the middle smaller
}
if (list->data[m]>list->data[low]) {
Swap(list, high, m); swaps the middle and left side, leaving a smaller
}
PivotKey = list->data[low];
while (low
while (Lowdata[high]>=pivotkey) {
high--;
}
Swap(list, low, high); Exchange, will be smaller than the pivot value to low ;
while (Lowdata[low]<=pivotkey) {
low++;
}
Swap(list, low, high);
}
return low ;
}
do a sort of list->data[low...high] . Low : minimum subscript, high: maximum subscript
void QuickSort (sqlist *list,int low ,Int. High )
{
int pivot; pivot
if (low
Pivot = Partition(list,low,high);
QuickSort(list, low, pivot-1);
QuickSort(list, pivot+1, high);
}
}
In the main method, add
{
int array[] = {7,6,9,ten,5,1,8};
sqlist liststruct;
printf("source data:");
sqlist *plist = &listStruct;
for (int i =0 ; i<7; i++) {
plist->Data[i] = array[i];
printf("%d",plist->data[i]);
}
plist->Length = 7;
printf("\ r \ n");
QuickSort(plist, 0, 6);
printf("final data:");
printfsqlist(plist);
}
On this basis can also be optimized for fast algorithms, there is time to engage in.
Fast sorting algorithm