Several commonly used sorting algorithms: such as direct insertion, folding sequencing, Hill sort, bubble sorting, rapid sorting, selection, stack sorting, main reference, data structure (C language) "# define maxSize 100Template CSORTARTARTARTHIC {public:
static struct _tagSqList {T r [MAXSIZE]; int length;}; private: typedef _tagSqList SqList; public: // direct insertion sort static void InsertSort (SqList & l); // binary insertion sort static void BInsertSort (SqList & l); / / Hill Sort Static Void Shellsort (SQList & l, INT DLTA [], INT N); // Bubble Sort Static Void Bubblesort (SQList & L); // Quick Sort Static Void Quicksort (SQList & L); // Select Sort Static Void selectSort (SQLIST & L); // Heap Sort Static Void Headsort (SQLIST & L); // Adopt the sequence table Storage Representation Static Void Swapt (T & T1, T & T2); Private: static void shellinsert (Sqlist & l, int DK); Static void Qsort (Sqlist & l, int low, int road;
Template void csortarithmethic :: INSERTSORT (SQList & l) {t ttemp; int j = 0; for (int i = 1; i = 0 && Ttemp <= LR [J]; --J) {LR [J 1] = LR [J]; } Lr [j 1] = tTEMP;}}} template void csortarithMethic :: binsertsort (Sqlist & l) {t ttemp; int low, high; int mid; int = 0; for (int i = 1; i <= L.LENGTH; i ) {TTEMP = LR [I]; Low = 0; high = i - 1; while (low = high 1; --j) LR [J 1] = LR [ j]; lr [high 1] = tTEMP;}} template void csortarithMethic :: shellinsert (SQList & l, int DK) {INT i = 0; int J = 0; t temp; for i = DK; i 0 && (Temp void csortarithmethic :: shellsort (Sqlist & l, int dlta [], int N) {for (int i = 0; i void csortarithmethic :: bubblesort (Sqlist & l) {for (int i = L.Length; I> = 0; I -) {for INT J = 0; J void csortarithmethic :: swapt (t & t1, t & t2) {t temp = t2; t2 = t1; t1 = TEMP;
Template void csortarithmethic :: qsort (Sqlist & l, int low, int high {INT i, J; int middle; i = low; j = high; t temp = lr [(low high) / 2]; do {while ((LR [I] Temp) && (j> low)) J -; IF (i < = j) {swapt (lr [i], lr [j]); i ; j -;}} while (i <= j);
IF (Low i) QSORT (L, I, HIGH);
} template void csortarithmethic :: quicksort (sqlist & l) {Qsort (L, 0, L.Length-1);}
Template Void Csortarithmethic :: SelectSort (Sqlist & l) {Int SmallIndex; INT I, J; For (i = 0; i LR [J]) SmallIndex = J;} Swapt (LR [I], LR [SmallIndex]);}}} template void CsortarithMethic :: Headsort (Sqlist & l) {for (int i = (L.Length-1) / 2; I> = 0; I -) {T RC = LR [I]; for (int J = 2 * i; j <= L.LENGTH-1; j * = 2) {IF (j = LR [ j]) BREAK; LR [I] = LR [J]; I = J;} LR [i] = rc;} for (i = L.LENGTH-1; I> 0; I -) {swapt (LR [0], LR [I]); T R = LR [I]; for (int J = 2 * i; j <= L.LENGTH-1; j * = 2) {IF (j = lr [j]) Break; LR [i] = LR [J]; i = j;} lr [i] = r; } / ************************************************************************** ** Comprehensive comparison of various internal sorting methods, approximately results as the following method average time worst case Auxiliary storage simple sort O (N * n) O (N * n) o (1) Rapid Sort O (NLOGN) O (N * N) O (NLOGN) O (NLOGN) O (1) ******************************** ****************** // ********************** *********************************************************** ******************************** Test ************** *********************************************************** ******************************************* / INT _TMAIN (int Arg, _tchar * argv []) {cout < <"/ nsortarithmetic ------------------------------" << Endl;
Csortarithmethic :: _ tagsqlist list;
For (int i = 0; i :: quicksort (list);
For (i = 0; i