Data Structure Experiment 4 (Implementation and Performance Analysis of sorting algorithms)
The options for sorting, insert sorting, Bubble sorting, quick sorting, improved quick sorting, and two-way Merge Sorting are implemented.
A random function is used to generate a random number of 100, sort the number, record the sorting start time and End Time, and calculate the consumed time to compare the algorithm.
Implementation Code:
# Include "iostream" # include "cstdio" # include "cstring" # include "algorithm" # include "queue" # include "stack" # include "cmath" # include "utility" # include "map" # include "set" # include "vector" # include "list" # include "string" # include "cstdlib" # include "ctime" using namespace std; typedef long ll; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f; const int MAXN = 1005; template
Void SelectSort (t a [], int n) {int small; for (int I = 0; I <n; ++ I) {small = I; for (int j = I + 1; j <n; ++ j) if (A [j] <A [small]) small = j; swap (A [I], A [small]) ;}} template
Void InsertSort (t a [], int n) {for (int I = 1; I <n; ++ I) {int j = I; T tmp = A [j]; while (j> 0 & tmp <A [j-1]) {A [j] = A [j-1]; j --;} A [j] = tmp;} template
Void BubbleSort (t a [], int n) {int I = n-1, j, last; while (I> 0) {last = 0; for (int j = 0; j <I; ++ j) if (A [j + 1] <A [j]) {swap (A [j], A [j + 1]); last = j;} I = last ;}} template
Void QSort (t a [], int left, int right) {int I, j; if (left <right) {I = left, j = right + 1; do {do I ++; while (A [I] <A [left]); do j --; while (A [j]> A [left]); if (I <j) swap (A [I], A [j]);} while (I <j); swap (A [left], A [j]); QSort (A, left, j-1); QSort (A, j + 1, right) ;}} template
Void QuickSort (t a [], int n) {QSort (A, 0, n-1);} template
Void MagicQSort (t a [], int left, int right) {int I, j; if (right> = 9) {if (left <right) {I = left, j = right + 1; do {do I ++; while (A [I] <A [left]); do j --; while (A [j]> A [left]); if (I <j) swap (A [I], A [j]);} while (I <j ); swap (A [left], A [j]); MagicQSort (A, left, j-1); MagicQSort (A, j + 1, right );}} else {InsertSort (A, right-left + 1); return ;}} template
Void MagicQuickSort (t a [], int n) {MagicQSort (A, 0, n-1);} template
Void Merge (t a [], int i1, int j1, int i2, int j2) {T * tmp = new T [j2-i1 + 1]; int I = i1, j = i2, k = 0; while (I <= j1 & j <= j2) {if (A [I] <= A [j]) tmp [k ++] = A [I ++]; else tmp [k ++] = A [j ++];} while (I <= j1) tmp [k ++] = A [I ++]; while (j <= j2) tmp [k ++] = A [j ++]; for (int I = 0; I <k; ++ I) A [i1 ++] = tmp [I]; delete [] tmp;} template
Void MergeSort (t a [], int n) {int i1, j1, i2, j2, size = 1; while (size <1) {i2 = i1 + size; j1 = i2-1; if (i2 + size-1> n-1) j2 = n-1; else j2 = i2 + size-1; Merge (A, i1, j1, i2, j2); i1 = j2 + 1;} size * = 2;} int main (int argc, char const * argv []) {clock_t start, finish; srand (time (NULL); int n = 100, I; int * a = new int [n]; int * B = new int [n]; int * c = new int [n]; int * d = new int [n]; int * e = new int [n]; int * f = new int [n]; cout <"the sequence to be sorted is:" <endl; for (int I = 0; I <n; ++ I) {a [I] = rand () % 91; cout <a [I] <""; B [I] = a [I]; c [I] = a [I]; d [I] = a [I]; e [I] = a [I]; f [I] = a [I] ;}cout <endl; start = clock (); SelectSort (a, n); cout <"the sequence after simple selection and sorting is:" <endl; for (I = 0; I <n; ++ I) cout <a [I] <""; cout <endl; finish = clock (); cout <"Start Time: "<start <" <"End Time:" <finish <"<" Duration: "<(double) (finish-start)/CLOCKS_PER_SEC <endl; start = clock (); InsertSort (B, n); cout <"the sequence after direct insertion and sorting is: "<endl; for (I = 0; I <n; ++ I) cout <B [I] <" "; cout <endl; finish = clock (); cout <"start Time:" <start <"<" End Time: "<finish <" <"Duration:" <(double) (finish-start)/CLOCKS_PER_SEC <endl; start = clock (); bubbleSort (c, n); cout <"sequence after Bubble Sorting:" <endl; for (I = 0; I <n; ++ I) cout <c [I] <"; cout <endl; finish = clock (); cout <" Start Time: "<start <" <"End Time:" <finish <"<" Duration: "<(double) (finish-start)/CLOCKS_PER_SEC <endl; start = clock (); QuickSort (d, n); cout <"the sequence after fast sorting is: "<endl; for (I = 0; I <n; I ++) cout <d [I] <" "; cout <endl; finish = clock (); cout <"start Time:" <start <"<" End Time: "<finish <" <"Duration:" <(double) (finish-start)/CLOCKS_PER_SEC <endl; start = clock (); mergeSort (e, n); cout <"after the two-way merged sorting sequence:" <endl; for (I = 0; I <n; I ++) cout <e [I] <"; cout <endl; finish = clock (); cout <" Start Time: "<start <" <"End Time:" <finish <"<" Duration: "<(double) (finish-start)/CLOCKS_PER_SEC <endl; start = clock (); MagicQuickSort (f, n); cout <"the Improved Fast sorting sequence is: "<endl; for (I = 0; I <n; ++ I) cout <f [I] <" "; cout <endl; finish = clock (); cout <"start Time:" <start <"<" End Time: "<finish <" <"Duration:" <(double) (finish-start)/CLOCKS_PER_SEC <endl; return 0 ;}