Eight sorting algorithm source code + time-consuming comparison (see good things turn down)

Source: Internet
Author: User

Comparison of the sorting time length of the eight sorting algorithms, the results of the test data 10000000 are as follows

Input test data length: 10000000
Data initialization ...
Data initialization complete!
Heap sequencing time: 8 seconds 499 milliseconds
Quick sort time: 22 seconds 35 milliseconds
Merge sort time: 34 seconds 473 milliseconds

Five other sorts I didn't wait for the result, and the reader can test it himself.

When testing, pay attention to memory consumption, so that the data is too large, memory is not enough, you can test a single algorithm to increase the number of testable data

#include <iostream>
#include <ctime>
#include <iomanip>

using namespace Std;

int NUM; Test data size
const int sortfunnum = 8;

Template <typename t> void Bublesort (T a[], int, int);
Template <typename t> void Selectsort (T a[], int, int);
Template <typename t> void Insertsort (T a[], int, int);
Template <typename t> void Ranksort (T a[], int, int);
Template <typename t> void Merge (T a[], int, int, int);
Template <typename t> void MergeSort (T a[], int, int);
Template <typename t> int Partition (T a[], int, int);
Template <typename t> void QuickSort (T a[], int, int);
Template <typename t> void Shellsort (T a[], int, int);
Template <typename t> void Heapsort (T a[], int, int);

void Test ()
{
cout << "Data initialization ..." << Endl;
int **testdata = new int *[sortfunnum];
for (int i = 0; i < sortfunnum; ++i)
Testdata[i] = new Int[num];
for (int k = 0; k < NUM; ++k)
{
TESTDATA[0][K] = rand ();
for (int j = 1; j < Sortfunnum; ++j)
TESTDATA[J][K] = testdata[0][k];
}
cout << "Data initialization complete!" << Endl;
clock_t begin, end;

begin = Clock ();
Heapsort (Testdata[6], 0, NUM-1);
end = Clock ();
cout << setw << "Heap sequencing Time:" << setw (4) << (end-begin)/clocks_per_sec << "SEC" << s ETW (4) <<
+ * Double ((end-begin)% clocks_per_sec)/clocks_per_sec << "msec" << Endl;

begin = Clock ();
QuickSort (Testdata[5], 0, NUM-1);
end = Clock ();
cout << setw << "Quick sort time:" << setw (4) << (end-begin)/clocks_per_sec << "seconds" << SETW (4) <<
+ * Double ((end-begin)% clocks_per_sec)/clocks_per_sec << "msec" << Endl;

begin = Clock ();
MergeSort (Testdata[4], 0, NUM-1);
end = Clock ();
cout << setw << "merge sort time:" << setw (4) << (end-begin)/clocks_per_sec << "seconds" << SETW (4) <<
+ * Double ((end-begin)% clocks_per_sec)/clocks_per_sec << "msec" << Endl;


begin = Clock ();
Insertsort (Testdata[2], 0, NUM-1);
end = Clock ();
cout << setw << Insert Sort time: << setw (4) << (end-begin)/clocks_per_sec << "seconds" << SETW (4) <<
+ * Double ((end-begin)% clocks_per_sec)/clocks_per_sec << "msec" << Endl;

begin = Clock ();
Shellsort (Testdata[7], 0, NUM-1);
end = Clock ();
cout << setw << "Hill sort Time:" << setw (4) << (end-begin)/clocks_per_sec << "seconds" << SETW (4) <<
+ * Double ((end-begin)% clocks_per_sec)/clocks_per_sec << "msec" << Endl;

begin = Clock ();
Selectsort (Testdata[1], 0, NUM-1);
end = Clock ();
cout <<setw << "Select sort time:" << setw (4) << (end-begin)/clocks_per_sec << "seconds" << s ETW (4) <<
+ * Double ((end-begin)% clocks_per_sec)/clocks_per_sec << "msec" << Endl;

begin = Clock ();
Ranksort (Testdata[3], 0, NUM-1);
end = Clock ();
cout << setw << count sort time: << setw (4) << (end-begin)/clocks_per_sec << "seconds" << SETW (4) <<
+ * Double ((end-begin)% clocks_per_sec)/clocks_per_sec << "msec" << Endl;

begin = Clock ();
Bublesort (Testdata[0], 0, NUM-1);
end = Clock ();
cout << setw << "bubble sort time:" << setw (4) << (end-begin)/clocks_per_sec << "seconds" << SETW (4) <<
+ * Double ((end-begin)% clocks_per_sec)/clocks_per_sec << "msec" << Endl;


for (int i = 0; i < sortfunnum; ++i)
Delete[] testdata[i];
Delete[] TestData;
}

int main ()
{
Srand (Time (0));
while (1)
{
cout << "Input test data length:";
Cin >> NUM;
Test ();
cout << "Test done!" << Endl << Endl;;
}

Cin.get ();
Cin.get ();
return 0;
}












/* Bubble Sort */
Template <typename t>
void Bublesort (T a[], int first, int last)
{
int has = 1;
for (int i = last; i > First && have; i.)
{
has = 0;
for (int j = First; J < i; ++j)
{
if (A[j] > a[j + 1])
{
Swap (A[j], a[j + 1]);
has = 1;
}

}
}
}

/* Select sort */
Template <typename t>
void Selectsort (T a[], int first, int last)
{
int m;
for (int i = last; i > first; i.)
{
m = i;
for (int j = First; J < i; ++j)
{
if (A[j] > A[m])
m = j;
}
Swap (A[m], a[i]);

}
}


/* Insert Sort */
Template <typename t>
void Insertsort (T a[], int first, int last)
{
Int J;
T T;
for (int i = first + 1, i <= last; ++i)
{
t = A[i];
j = i-1;
while (A[j] > t && J >= 0)
{
A[j + 1] = A[j];
--j;
}
A[j + 1] = t;
}
}

/* Count Sort */
Template <typename t>
void Ranksort (T a[], int first, int last)
{
int sum = Last-first + 1;
int *rank = new Int[sum];
T *b = new T[sum];
for (int i = 0; i < sum; ++i)
{
Rank[i] = 0;
B[i] = A[first + i];
}

for (int m = First, M <= last; ++m)
{
for (int n = first; n < m; ++n)
{
if (A[m] > A[n])
rank[m-first]++;
Else
rank[n-first]++;
}
}
for (int k = 0; k < sum; ++k)
{
A[first + rank[k]] = b[k];
}
Delete[] B;
Delete[] rank;
}

/* Merge, for merge sort */
Template <typename t>
void Merge (T a[], int first, int mid, int last)
{
if (Last-first < 1)
Return
T *b = new T[last-first + 1];
int m = First, n = Mid, bi =-1;
while (M < mid && n <= last)
{
if (A[m] > A[n])
B[++BI] = a[n++];
Else
B[++BI] = a[m++];
}
while (M < mid)
B[++BI] = a[m++];
while (n <= last)
B[++BI] = a[n++];
while (bi >= 0)
{
A[first + bi] = B[bi];
--bi;
}

Delete[] B;

}
/* Merge Sort */
Template <typename t>
void MergeSort (T a[], int first, int last)
{
if (Last-first + 1 >= 2)
{
int mid = (first + last)/2;
MergeSort (A, first, mid);
MergeSort (A, mid + 1, last);
Merge (A, first, Mid + 1, last);
}
}

/* Division, for quick Sorting */
Template <typename t>
int Partition (T a[], int first, int last)
{
Swap (A[rand ()% (Last-first + 1) + first], a[last]); Random Exchange
int m = Last;
int j = first-1;
for (int i = First, i < last; ++i)
{
if (A[i] < a[m])
{
++j;
Swap (A[j], a[i]);
}
}
Swap (A[m], a[j + 1]);
return j + 1;

}
/* Quick Sort */
Template <typename t>
void QuickSort (T a[], int first, int last)
{
if (Last-first + 1 >= 2)
{
int m = Partition (A, first, last);
QuickSort (A, first, m-1);
QuickSort (A, M + 1, last);
}
}

/* Hill Sort */
Template <typename t>
void Shellsort (T a[], int first, int last)
{
int sum = Last-first + 1;
T T;
Int J;
for (int d = SUM/2; d >= 1; d/= 2)
{
for (int i = d; i <= sum; i + = d)
{
t = a[first-1 + i];
j = i-d;
while (J >= 0 && a[first-1 + j] > t)
{
A[first-1 + j + d] = a[first-1 + j];
J-= D;
}
A[first-1 + j + d] = t;
}
}
}

/* Heap Sort */
Template <typename t>
void Heapsort (T a[], int first, int last)
{
int num = Last-first + 1;
int F, C;
T T;
for (f = num/2; f >= 1;--f)
{
c = f * 2;
t = a[first-1 + f];
while (c < num)
{
if (c + 1 <= num && A[first + c] > a[first-1 + c])
++c;
if (T > a[first-1 + c])
Break
A[first-1 + C/2] = a[first-1 + c];
C *= 2;
}
A[first-1 + C/2] = t;
}

while (num > 1)
{
Swap (A[first], A[first + num-1]);
c = 2;
t = A[first];

while (c < num)
{
if (c + 1 < num && A[first + c] > a[first-1 + c])
++c;
if (a[first-1 + C] < T)
Break
A[first-1 + C/2] = a[first-1 + c];
C *= 2;
}
A[first-1 + C/2] = t;
--num;
}
}

Eight sorting algorithm source code + time-consuming comparison (see good things turn down)

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.