Test for several sorting algorithms

xiaoxiao2021-03-06  59

#include #include #include using namespace std;

Class Sort {private: int * base; int data_type; bool is_display; void Heapadjust (int, int); // Pile Adjust Void Partition (INT, INT); // Quick Sort Array Segment PUBLIC :

Sort (int size, int kind, char display); // assigns array memory ~ sort (); // Release allocated memory void show (); // Displays the value BOOL COPY (); // Memory copy Void Display (); // Last output Display Void Runtime {Cout << "Time" << (long double) runtime / clocks_per_sec << endl;} // calculate the runtime void bubblesort (); // Bubble Sort Void Quicksort (); // Quick Sort Void Heapsort (); // Stack Sort Void SelectionSort (); // Select Sort Void INSERTSORT (); // Insert Sort Voidsort (); // Hill Sort Void RadixSort (); // Benquary of the base};

Sort :: sort (int size = 100, int Kind = 0, char display = 'n') {INT i; length = size; data_type = kind; if (Display == 'y' || display == 'y' IS_DISPLAY = True; Else is_display = false;

// Based on the Base array allocation memory and the random value base = (int *) malloc (intend * size); if (! Base) {CERR << "The System Malloc Memory Error!" << endl; exit 1);} Switch // base array assignment type selection (0 - random value 1 - Positive sequence 2 - reverse order) {case 0: i = clock (); SRAND (i); for (i = 0; i

/ * Display Arr array value * / void sort :: show () {INT i; if (is_display) {system ("pause"); for (i = 0; i

}

/ * Array copy * / bool sort :: copy () {INT i;

For (i = 0; i

/ * Bubbling Sort * / Void Sort :: bubblesort () {INT I, J, T; For (i = 0; i arr [j]) {t = arr [i]; arr [i] = arr [= t;} / * rapid sort * /

Void Sort :: Partition (int low, int high) {INT I, J, T; if (Low t) j -; IF (i

Void Sort :: Heapadjust (int K, int N) {INT I, J; int Tmp; i = k; j = 2 * i 1; TMP = arr [i]; while (j

Void Sort :: Heapsort () {INT K, DES; long Tmp; int n = length;

For (k = n / 2-1; k> = 0; k -) / * buildtle * / sort :: Heapadjust (k, n);

For (des-) / * sort * / {TMP = arr [0]; arr [0] = arr [des]; arr [des] = tmp; sort :: Heapadjust (0, des);}}

/ * Select Sort * /

Void sort :: select () {INT I, J, T, MINS = 0; for (i = 0; i

/ * Insert Sort * / Void Sort :: INSERTSORT () {INT I, J, T; For (i = length -1; i> 0; I -) / / Determine the minimum value in the ArR array, and put it in Arr [0] Sentinel bit IF (Arr [i]

/ * Hill sort * / void sort :: shellsort () {INT i, J, h, v; for (h = 1; h <= length / 9; h = 3 * h 1); // Find OK Maximum incremental value for (; h> 0; h / = 3) for (i = h; i = h && v

For (i = 0; i <10; i ) c [i] = (int *) malloc (intend * n);

For (i = 0; i

M = 0; for (D = 0; D <10; D ) {for (E = 0; E

For (i = 0; i <10; i ) free (c [i]);}

Void Sort :: Display () {int start_time, end_time;

Cout << "--------- Quick Sort ----------" << Endl; if (data_type == 0) {sort :: copy (); start_time = clock () Sort :: quicksort (); end_time = clock (); sort :: runtime (end_time - start_time); sort :: show ();} Else Cerr << "When the inner resection, rapid sorting may cause the stack overflow Therefore, sole mask test "<< Endl << endl;

COUT << "--------- Pile Sort ------------" << Endl; Sort :: Copy (); start_time = clock (); sort :: heapsort ); END_TIME = clock (); sort :: runtime (end_time - start_time); sort :: show ();

Cout << "--------- Hill Sort ----------" << Endl; Sort :: Copy (); start_time = clock (); sort :: shellsort () End_time = clock (); sort :: runtime (end_time - start_time); sort :: show (); cout << "--------- Base Sort ----------" << endl; sort :: copy (); start_time = clock (); sort :: radixsort (); end_time = clock (); sort :: runtime (end_time - start_time); sort :: show ();

COUT << "--------- Insert Sort ----------" << endl; sort :: copy (); start_time = clock (); sort :: insertsort (); END_TIME = Clock (); sort :: runtime (end_time - start_time); sort :: show ();

COUT << "---------- Select Sort -----------" << Endl; Sort :: Copy (); start_time = clock (); sort :: selectionsort ); END_TIME = clock (); sort :: runtime (end_time - start_time); sort :: show ();

COUT << "----------- Bubbling ---------" << endl; sort :: copy (); start_time = clock (); sort :: bubblesort ); END_TIME = clock (); sort :: runtime (end_time - start_time); sort :: show ();

}

Void main () {int size, kind; bool ct; char sz = 'n', ch; do {cout << "input test test data number: (n> 0) << Endl; do {cin> > size;} while (size <1 && (cout << "data number should be greater than 1, please re-enter:" << Endl));

Cout << "Enter the test type value: (0-- random number 1 - Positive sequence 2 - reverse order)" << Endl; do {cin >> Kind;} While ((Kind> 2) || (Kind> 2) || <0) && (cout << "Type value range (0-2), please re-enter:" << Endl));

转载请注明原文地址:https://www.9cbs.com/read-91040.html

New Post(0)