Algorithm serial (2) - Comparison of rapid sorting and insertion sorting

xiaoxiao2021-03-05  25

Quick Sort Basic Thought: Select A as an element, such as t = a (s), then rearrange other elements, so that all the elements in T (1: N) are less than or equal to T, All elements after T are greater than or equal to T.

// Language: C // Purpose: Compare the time complexity of two sort algorithms // Original code: // INSERTIONSORTITINT * INSERTIONSORT (INT * a, INT N) {Int J, ITEM, I; For (J = 2; J <= n; j ) {item = a [j]; i = j-1;

While (item

// QuickSortint Quickpass (int R [], int download, up; // initialize flag down = low; Up = high; r [0] = r [low]; // put benchmark record INTO R [0] While (Down = R [0])) // scan from right to left up -; if (Down

} R [Down] = R [0]; Return Down;} // One Time of Sort

INT * Quicksort (int R [], int low, int high {int mid; if (low

} // quicksort

#include #include #include // main functionvoid main () {clock_t start, end; float elapsed1; // time of quicksort float elapsed2; // time of infinsertionsort const Int n = 30001; const Int m = 30000; int 1 INT W;

Cout << "| ------ Rapid Sort and Insert Sort Algorithm ----- | << Endl; Cout <<" | --------- Data Size: 30000- ---------- | << Endl; cout << "| --- Power by zhanjianTao (028054115) --- |" << endl; cout << "------- -------------------------------- "<< endl; cout <<" running ... "<< endl;

While (w) {// INSERTIONSORT START = Clock (); // Start TimeInt a [M]; for (i = 0; i

INSERTIONSORT (A, M);

End = clock (); // finish time

ELAPSED2 = ((float) end-start) / clocks_per_sec;

// Insertionsort

// QuickSort start = clock (); // start time int R [n]; for (i = 1; i <= n; i ) R [i] = rand ();

Quicksort (R, 1, N);

End = clock (); // Time Finish

ELAPSED1 = (FLOAT) End-start / clocks_per_sec; // QuickSort Cout << "Select <3> Verify INSERTIONSORT's correctness" << Endl; cout << "Select <2> Verify QUICKSORT correctness" << Endl; cout << "Select <1> Compare Algorithm Operation Time" << Endl; Cout << "Select <0> Exit" << Endl; CIN >> W; Switch (w) {

Case 3: for (i = 0; i


New Post(0)