Quick sort is to find a number at random, and then arrange the remaining elements on the left and right according to their size relationship. First, put the found element at the position with subscript 0.

At this time, I put the first element at 0 (called the core number), and then I have to traverse from the right, so that the elements less than 8 can be placed on the left, and then the right can be vacated, so that the elements greater than 8 on the left can be placed on the right. In addition, the subscript is moved only when the element position exchange is not involved ， To find the elements that need to be moved.

Only when L=R can we find the only suitable position, otherwise we have to cycle the previous steps.

Then compare them. Because 11 and 13 are greater than 8, they will not move, but r --, when 4 < 8, move them left to 1, R (R=4), then 9 > 8, and move them right to position 4, L++(L=2). This is a cycle. Because l < R, the previous operation needs to be carried out. At this time, position 4 is set to 9 > 8, so move R. when 7 < 8 at R=3, they complete the exchange with the previous position 2,

At this moment, 7 < 8 in position 2 moves L, at this time l equals R, and then 8 is placed in this position. Therefore, the table is divided into two parts:

This is just to divide it into two parts. According to this idea, operate the left and right parts at once until there is only one element in each part, and then end the operation. Recursion can be used.

Also, after a comparison, remember to return the position greater than and less than the core number, so as to pass the appropriate area later

The specific codes are as follows:

int Quick_order(SqL* L, int low, int high) { if (low < high)//Judge whether there is only one element and judge the flag of the end of the comparison { int pos = pivot(&L,low,high);//Acceptance cut-off point Quick_order(L, low, pos - 1);//Left of dividing point Quick_order(L, pos + 1, high);//Right of dividing point } return OK; } int pivot(SQL* L, int left, int right) { (*L)->arr->data = ((*L)->arr + left)->data;//Put it in the sentry position while (left < right) { while (left<right && ((*L)->arr + right)->data>=(*L)->arr->data) { --right;//Move subscript } ((*L)->arr + left)->data = ((*L)->arr + right)->data;//Less than the number of cores, move forward while (left < right && ((*L)->arr + left)->data <= (*L)->arr->data) { ++left;//Move subscript } ((*L)->arr + right)->data = ((*L)->arr + left)->data;//Greater than the number of cores, move back } ((*L)->arr + left)->data = (*L)->arr->data;//Put in place return left; }

Remember the end condition. When there is only one position left, it is the sign of recursion and the end of the loop

The source code is as follows:

#include <stdio.h> #include <stdlib.h> #define SIZE 10 #define OK 1 #define ERROR 0 #define ElemType int #define OVERFLOW -1 typedef struct { ElemType data; }Record; typedef struct { Record* arr; int length; int real_len; }SqL,*SQL; int SqL_init(SqL* L) { L->arr = (Record*)malloc(sizeof(Record) * SIZE); if (!L->arr) { return ERROR; } L->length = SIZE; L->real_len = 1;//record the begining of the location return OK; } int SqL_insert(SqL* L) { int i = 0; int num = 0; if (num + L->real_len > L->length) { return OVERFLOW; } printf("number>"); scanf("%d", &num); for (i=1; i <= num; i++) { printf("value>"); scanf("%d", &(L->arr + i)->data); L->real_len++; } return OK; } int traverse(SqL L) { int i = 0; if (L.real_len == 1) { return ERROR; } for (i = 1; i < L.real_len; i++) { printf("%4d", (L.arr + i)->data); } return OK; } int Quick_order(SqL* L, int low, int high) { if (low < high)//Judge whether there is only one element and judge the flag of the end of the comparison { int pos = pivot(&L,low,high); Quick_order(L, low, pos - 1); Quick_order(L, pos + 1, high); } return OK; } int pivot(SQL* L, int left, int right) { (*L)->arr->data = ((*L)->arr + left)->data;//Put it in the sentry position while (left < right) { while (left<right && ((*L)->arr + right)->data>=(*L)->arr->data) { --right;//Move subscript } ((*L)->arr + left)->data = ((*L)->arr + right)->data;//Less than the number of cores, move forward while (left < right && ((*L)->arr + left)->data <= (*L)->arr->data) { ++left;//Move subscript } ((*L)->arr + right)->data = ((*L)->arr + left)->data;//Greater than the number of cores, move back } ((*L)->arr + left)->data = (*L)->arr->data;//Put in place return left; } int main() { SqL L; SqL_init(&L); SqL_insert(&L); Quick_order(&L, 1, L.real_len - 1); traverse(L); return 0; }

design sketch:

If you have any mistakes, you are welcome to point them out. Thank you very much