Various sorting algorithms compare source code

xiaoxiao2021-03-06  19

// Testsort.cpp: Defines the entry point of the console application. //

#include "stdafx.h" #include int Cp [2500]; int rplist [2500] = {1097, 0, 747, 1395, 24, 2433, 1208, 1050, 1106, 378, 450, ..., 1146 }; int C = 0; void test (int [= 0) (int [= 0; i

Printf ("% s, count =% D Time =% D", S, C, Clock () - T); for (int i = 0; i <25; i ) {IF (i% 25 == 0) Printf ("/ n"); Printf ("% D", A [i]);

} Printf ("/ n");

Void shellsort (int A [], int N) {int GAP, I, J; For (GAP = N / 2; GAP> 0; GAP = GAP / 2) for (i = gap; i = 0 && a [j]> a [j gap]; j = j-gap) {// swap (a [j], a [j gap]); int Temp = A [j]; a [j] = a [j GAP]; A [J GAP] = Temp; C ;}} void bubblesort (int A [】, int N) // bubbling order {INT i , J, Temp; FOR (i = 0; I = i 1; j -) {IF (A [J]

Void SunXysort (int B [], INT D) {INT P = 0; for (int i = 0; i b [j]) {p = b [j]; b [j] = b [i]; b [i] = p; c ;}} void quicksort (int A [】, int L, int Hi) {// lo is the thing {hi is the Upper index // of the region of array a That is to be sorted int = LO, J = Hi, H; int x = a [(LO HI) /2];

// Partition do {while (a [i] x) {j--;} // C ;} if (i <= j) {H = a [i]; a [i] = a [j]; a [j] = h; i ; j -; c ;}} while (i <= j);

// Recursion IF (LO

Void Quicksort2 (Int a [], int N) {Quicksort (A, 0, N-1);} void sift (int key [], int L, int m) {INT I, J, X; i = L; J = 2 * i; x = key [i]; while (j <= m) {C ; IF (j

Void Heapsortb (int key [], int N) {INT I, W; // compct = shiftcount = 0; for (i = n / 2; i> = 1; I -) {sift (key, i, n ); c ;} for (i = n; i> = 2; i -) {C ; w = key [i]; // shiftcount ; key [i] = key [1]; // shiftcount ; key [ 1] = W; // ShiftCount ; SIFT (Key, I-1, 1);} return;} voidheapsort (int key [], int N) {INT I, J; int = n; // - 1 int = L = (n >> 1) 1; int RRA; for (;;) {if (l> 1) {RRA = Key [- l-001];} else {ra = key [IR-001]; Key [IR-001] = Key [1-001]; C ; IF (--ir == 1) {key [1-001] = RRA; C ; return;}} i = L; j = l << 1; While (j <= IR) {IF (j

INT_TMain (int Argc, _tchar * argv []) {Test ("Quicksort", Quicksort2, CP, 2500); TEST ("Insertsort", Insertsort, CP, 2500); TEST ("Shellsort", Shellsort, CP, 2500 TEST ("SelectSort", SelectSort, CP, 2500); TEST ("Bubblesort", Bubblesort, CP, 2500); TEST ("Sunxysort", Sunxysort, CP, 2500); Test ("Heapsort", Heapsort, CP , 2500); return 0;}


New Post(0)