Comparison of merging and fast sorting algorithms for processing data of the same size

Source: Internet
Author: User

The time complexity of the two algorithms is statistically measured by arranging 500 arrays of 100, 200, 300, 400, 600, 700, 800, 900, 1000, 10, respectively.

Quick Sort Program:

#include <iostream>using namespace std;int circle;static int num[11]={0,0,0,0,0,0,0,0,0,0,0};void vector_ Initial (int *array,int n), void vector_print (int *array,int n), void fast_sort (int *array,int p,int R), int Separate_2part ( int *array,int p,int R), void count (), int main () {int A[1000];int n;//cout<< "Number of inputs:";//cin>>n;for (circle=1 ; circle<=10;circle++) {vector_initial (a,circle); Fast_sort (a,0,100*circle-1); Vector_print (a,100*circle);} for (int i=1;i<=10;i++) cout<<i*100<< "data, perform step:" <<num[i]<<endl;return 0;} void vector_initial (int *array,int n) {int i=0;while (i<n*100) {Array[i]=rand ()%1000;i++;}} void Vector_print (int *array,int n) {for (int i=0;i<n;i++) cout<<array[i]<< "";cout<<endl<< Endl;} void Fast_sort (int *array,int p,int r) {int q;if (p<r) {Q=separate_2part (array,p,r); Fast_sort (array,p,q-1); fast_ Sort (array,q+1,r);}} int separate_2part (int *array,int p,int r) {int N1,n2,x;n1=p;n2=r;x=array[p];while (1) {while (array[++n1]< x) {count ();} while (array[n2]>x) {count (); n2--;} if (n1>=n2) Break;swap (array[n1],array[n2]);} for (int i=p+1;i<=n2;i++) {array[i-1]=array[i];} Array[n2]=x;return N2;} void count () {switch (circle) {case 1:num[1]++;break;case 2:num[2]++; break;case 3:num[3]++; Break;case 4:num[4]++; ; case 5:num[5]++; Break;case 6:num[6]++; Break;case 7:num[7]++; Break;case 8:num[8]++; Break;case 9:num[9]++; Break;case 10:num[10]++; Break;default:break;}}

Merge tests:

#include <iostream> #include <time.h>using namespace std;int a[1000];int circle;static int num[11]={ 0,0,0,0,0,0,0,0,0,0,0};void vector_initial (int n), void print_vector (int circle), void Merge_sort (int p,int r); void Merge_combine (int p,int q,int r); int main () {for (circle=1;circle<=10;circle++) {vector_initial (circle); Merge_sort (0,circle*100-1);p rint_vector (circle*100);} for (int i=1;i<=10;i++) cout<<i*100<< "data, perform step:" <<num[i]<<endl;return 0;} void vector_initial (int n) {int i=0;while (i<n*100) {A[i]=rand ()%1000;i++;}} void Print_vector (int n) {for (int i=0;i<n;i++) {cout<<a[i]<< "";//if (19==i%20)//cout<<endl;} Cout<<endl<<endl<<endl;} void Merge_sort (int p,int R)//merge sort {int q;if (p<r) {q= (p+r)/2; Merge_sort (P,Q); Merge_sort (Q+1,R); Merge_combine (P,q,r);}} void Merge_combine (int p,int q,int r) {int L[1000];int r[1000];int n1=q-p+1;int n2=r-q;for (int i=1;i<=n1;i++) L[i]=a[p +i-1];for (int j=1;j<=n2;j++) R[J]=A[Q+J]; l[n1+1]=100000; r[n2+1]=100000;i=1;j=1;for (int k=p;k<=r;k++) {switch (circle) {case 1:num[1]++;break;case 2:num[2]++; break;case 3: num[3]++; Break;case 4:num[4]++; Break;case 5:num[5]++; Break;case 6:num[6]++; Break;case 7:num[7]++; Break;case 8:num[8]++; Break;case 9:num[9]++; Break;case 10:num[10]++; Break;default:break;} if (L[i]<=r[j]) {a[k]=l[i];i++;} else{a[k]=r[j];j++;}}}

Comparison of test results:

Merge algorithm

Quick Sort algorithm:

Comparison of merging and fast sorting algorithms for processing data of the same size

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.