Data Structure Experiment 4 (Implementation and performance analysis of sorting algorithm)

Source: Internet
Author: User

Implements the selection sort, insert sort, bubble sort, quick sort, improved quick sort, and two-way merge sort.

Random functions randomly generate 100 numbers, make various sorts, record sort start time and end time, calculate the time consumed to compare the algorithm's advantage.

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 long ll;const int MOD = 1e9 + 7;const int INF = 0x3f3f3f3f;const int MAXN = 1005;template <class t>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 <class t>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 <class t>void bubblesort (T a[], int n) {int i = n-1, J, last;while (i > 0) {last = 0;for (int j = 0; J &L T I ++J) if (a[j + 1] < A[j]) {swap (A[j], a[j + 1]); last = j;} i = Last;}} Template <class t>void QSort (T a[], int left, inT right) {int I, j;if (left < right) {i = left, J = right + 1;do {does i++; while (A[i] < A[left]);d o 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 <class t>void QuickSort (T a[], int n) {QSort (A, 0, n-1);} Template <class t>void magicqsort (T a[], int left, int. right) {int I, j;if (right >= 9) {if (left < right) {i = L EFT, j = right + 1;do {does i++, while (A[i] < A[left]);d o 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 <class t>void magicquicksort (T a[], int n) {magicqsort (A, 0, n-1);} Template <class t>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];d ele Te []tmp;} Template <class t>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 = 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 << "to sort sequence 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 << "After a simple selection of sorted sequence:" << Endl; for (i = 0; i < n; ++i) cout << a[i] << ""; cout << endl;finish = Clock (); CoUT << "Start time:" << start << "<<" End Time: "<< finish <<" "<<" duration: "<& Lt (double) (finish-start)/clocks_per_sec << Endl;start = Clock (); Insertsort (b, n); cout << "The sequence after direct insert sort is:" << en Dl;for (i = 0; i < n; ++i) cout << b[i] << ""; cout << endl;finish = Clock (); cout << "Start time:" &lt ;< start << "<<" End Time: "<< finish <<" "<<" duration: "<< (double) (Finish-star T)/clocks_per_sec << Endl;start = Clock (); Bubblesort (c, N); cout << "bubble sorted sequence is:" << 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 <& Lt Endl;start = Clock (); QuickSort (d, N); cout << "After a quick sorted sequence:" << endl;for (i = 0; I < n; i++) cout << d[i] << ""; cout << endl;finish = Clock () cout << "Start time:" << start << "<<" End Time: "&lt ;< finish << "<<" duration: "<< (Double) (finish-start)/clocks_per_sec << Endl;start = Cloc K (); MergeSort (e, N); cout << "The sequence after two merges is:" << 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 <& Lt Endl;start = Clock (); Magicquicksort (f, N); cout << "After the improved fast sorted 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 <& Lt Endl;return 0;}


Data Structure Experiment 4 (Implementation and performance analysis of sorting algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.