Quick sorting is an improvement of bubble sorting. Its basic idea is: split the data to be sorted into two independent parts by one sort, and all the data in one part is smaller than all the data in the other part, then, the data is sorted by time. The entire sorting process can be recursive or non-recursive to convert the entire data into an ordered sequence.
Quick sorting is an improvement of bubble sorting. Its basic idea is: split the data to be sorted into two independent parts by one sort, and all the data in one part is smaller than all the data in the other part, then, the data is sorted by time. The entire sorting process can be recursive or non-recursive to convert the entire data into an ordered sequence.
The recursive implementation of quick sorting is as follows:
# Include "stdafx. h "# define LIST_INIT_SIZE 100 // initial size of the sequence table # define LISTINCREMENT 10 // incremental typedef int ElemType of the sequence table; // ordered table element type typedef struct // ordered table structure {ElemType * elem; int length; int listsize;} SqList; // initialize the ordered table int InitList_Sq (SqList & L) {L. elem = (ElemType *) malloc (LIST_INIT_SIZE * sizeof (ElemType); if (! L. elem) return-1; L. length = 0; L. listsize = LIST_INIT_SIZE; return 0 ;}// create an ordered table int CreatList_Sq (SqList & L) {InitList_Sq (L); int I = 1; while (scanf ("% d ", & L. elem [I])! = EOF) {I ++;} L. length = I-1; return 0;} // A quick sorting int Partition (SqList & L, int low, int high) {L. elem [0] = L. elem [low]; int foreign TKEY; foreign TKEY = L. elem [low]; int temp; while (low
Non-recursive algorithm for fast sorting:
# Include
# Include
Using namespace std; template
Int partition (T a [], int low, int high) {T v = a [low]; while (low
= V) high --; a [low] = a [high]; while (low
Void QuickSort (T a [], int p, int q) {stack
St; int j; do {while (p
This article is available at http://www.nowamagic.net/librarys/veda/detail/1194.