Test __c++ for "C + +" various sort run times

Source: Internet
Author: User
Tags assert

What we often see is sorting a set of data, and there are many ways to sort it.

The common sorting methods are:

Bubble sort, insert sort, hill sort, quick sort, heap sort, select sort, merge sort, count sort, cardinal sort.

These common sorting methods, when sorting the same set of data, are slow in size, and their time complexity is different:


Next, we'll simply test the time it takes to run these sorting methods.

First, the function of calculating the time a program runs is the clock ' function, which is placed inside the time,h.

When we start executing the program, we define start, define a finish when we finish the program, and subtract two times, then we can get the time that the program runs.

Code implementation:

Test.h file

#pragma once   
#include <iostream>
using namespace std;
#include <time.h>
#include <assert.h>
#include <stack>
#define M 100/* Execution times * *
Define N 500/* Array size/

void menu ()//Menu function
void bubblesort (int *a, size_t N);//1, bubble sort
void insertsort (int *a, size_t n);//2, insert sort
void sheelsort (int *a, size_t n);//3, Hill sort
void selectsort (int *a, size_t n);//4, select Sort
void QuickSort (int *a, size_t left, size_t right);//5, quick sort algorithm
void heapsort (int *a, size_t n);//6, heap sort
void Merge Sort (int* A, size_t N);//7, merge sort
void Countsort (int *a, size_t n);//8, Count sort
void lsdsort (int *a, size_t n);//9, cardinal Sort

Test.cpp file

#include "test.h" void menu () {cout << "**********************************************\n";
	cout << "*****************1, bubble sort ******************\n";
	cout << "*****************2, insert sort ******************\n";
	cout << "*****************3, Hill sort ******************\n";
	cout << "*****************4, select Sort ******************\n";
	cout << "***************5, fast sorting algorithm ****************\n";
	cout << "*****************6, heap sort ********************\n";
	cout << "*****************7, merge sort ******************\n";
	cout << "*****************8, counting sort ******************\n";
	cout << "*****************9, Cardinal order ******************\n";
	cout << "*******************0, exit ********************\n";
	cout << "**********************************************\n";
cout << "Please select an algorithm for sorting:" << Endl;}
	1, bubble sort void bubblesort (int *a, size_t n) {int i, J;
	int tmp;
	for (i = 1; i < n; i++) {for (j = n-1; J >= i; j--) {if (a[j + 1] < A[j]) {			TMP = a[j + 1];
				A[j + 1] = A[j];
			A[J] = tmp;
	//2, insert sort void insertsort (int *a, size_t n) {int i;
	Int J;
		for (i = 2; I <= n; i++) {a[0] = A[i];
		j = i-1;
			while (A[0] < a[j]) {a[j + 1] = A[j];
		j--;
	} a[j + 1] = a[0];
	}//3, Hill sort void sheelsort (int *a, size_t N) {assert (a);  int gub = n/2;//First defines the increment as N/2 while (Gub > 0) {for (size_t i = gub; i < n; ++i)//Exchange from the first GUB data {int TMP =
			A[i];
			int end = I-gub;
				while (end >= 0 && tmp < A[end]) {a[end + Gub] = A[end];
			end = End-gub;
		} a[end + Gub] = tmp;
	} gub/= 2;
	}//4, select sort void selectsort (int *a, size_t N) {assert (a);
	int left = 0;
	int right = n-1;
		While [left < right] {size_t Minindex = left;
		size_t Maxindex = right;
			for (size_t i = left, I <= right; ++i) {if (A[i] < A[minindex]) {minindex = i;
			} if (A[i] > A[maxindex]) {maxindex = i; } swap (A[left], a[mInindex]);
		if (Maxindex = left) {maxindex = Minindex;
		} swap (A[right], a[maxindex]);
		++left;
	--right;
	}//5, quick sort algorithm int partsort (int *a, size_t left, size_t right) {int I, J;
	static int w = 0;
	int temp;
	i = left;
	j = right;
	temp = A[i];
			do {while ((a[j]>temp) && (i<j)) {j--;
		w++;
			} if (i<j) {a[i] = a[j];
			i++;
		w++;
			while (a[i]<= temp && (i<j)) {i++;
		w++;
			} if (i<j) {a[j] = A[i];
			j--;
		w++;
	} while (I!= j);
	A[i] = temp;
return i;
	} void QuickSort (int *a, size_t left, size_t right) {assert (a);
	if (left >= right) {return;
	int div = Partsort (A, left, right);
	QuickSort (A, left, Div-1);
QuickSort (A, div + 1, right);
	//6, heap sort void adjustdown (int *a, size_t n, int root) {size_t parent = root;
	size_t Child = parent * 2 + 1;
		while (Child < n) {if (child + 1 < n && a[child + 1] > A[child]) {++child; } if (A[child] > A[parent]) {swap (A[child], a[parent]);
			parent = child;;
		Child = parent * 2 + 1;
		} else {break;
	}} void Heapsort (int *a, size_t N) {assert (a);
	int parent = (n-2) >> 1;
	Build heap for (; parent >= 0;--parent) {Adjustdown (A, n, parent);
		for (int i = n-1 i >= 0;-i) {swap (a[0], a[i]);
	Adjustdown (A, I, 0);
	//7, merge sort void _mergesort (int* src, int* dst, int left, right of int) {if (left >= right) return;
	int mid = left + (right-left)/2;
	[Left,mid],[mid+1,right] _mergesort (SRC, DST, left, mid);

	_mergesort (SRC, DST, mid + 1, right); int begin1 = left;
	int begin2 = mid + 1;

	int index = 0; while (Begin1 <= mid && begin2 <= right) {if (src[begin1) < src[begin2]) dst[index++] = Src[begin1
		++];
	else dst[index++] = src[begin2++];
	while (Begin1 <= mid) dst[index++] = src[begin1++];

	while (Begin2 < right) dst[index++] = src[begin2++]; int i = 0;
	Int J = left; while (I < index) src[j++] = dst[i++];
	} void MergeSort (int* arr, size_t len) {assert (arr);
	ASSERT (len > 0);
	int *tmp = new Int[len];
_mergesort (arr, tmp, 0, len-1);
	//8, Count sort void countsort (int *a, size_t n) {int I, j, K;  int C[n + 1] = {0};    /* All elements of the C array for counting are initialized with 0*/for (i = 0; i<n; i++) c[a[i]]++;
	/* For example, R[i].key is 6 o'clock, C[6]++,c[r[i].key] is the number of r[i].key occurrences * * k = 0;
for (j = 0; J <= N; j +)/* Investigate each j*/for (i = 1 I <= c[j]; i++)/*j=r[j].key has appeared c[j], which is the result of the order/a[k++] = j;
	//9, Cardinal sort size_t getmaxdigit (int *a, size_t n) {size_t digit = 1;
	size_t base = 10;
			for (size_t i = 0, i < n; i++) {while (A[i] >= base) {base *= 10;
		digit++;
} return digit;
	} void Lsdsort (int *a, size_t n) {size_t maxdigit = Getmaxdigit (A, n);

	size_t base = 1;
	size_t *bucket = new Size_t[n];
		while ((maxdigit--) > 0) {size_t counts[10] = {0};
		size_t start[10] = {0};

		Start[0] = 0; for (size_t i = 0; i < n; i++) {size_t num = (A[i]/base)% 10;
		counts[num]++;
		for (size_t i = 1; i < i++) {Start[i] = Start[i-1] + counts[i-1];
			for (size_t i = 0; i < n; i++) {size_t num = (a[i]/base)% 10;
		bucket[start[num]++] = A[i];
		memcpy (A, bucket, sizeof (size_t) *n);
	Base *= 10; }
}

Main.cpp

#include "test.h"
void Main () 
{
	int a[n], I, J, p;
	Menu ();
	do{
		cin >> p;
		Double start, finish; /* Define start time and end time/start
		= (double) clock (); 
		for (j = 0; j<m; j + +) 
		{/* executes M/for
			(i = 0; i<n; * (A + i++) = rand ()% 10);
			H (p)
			{case
			1:bubblesort (A, N); 
				break;
			Case 2:insertsort (A, N);
				break;
			Case 3:sheelsort (A, N);
					break;
			Case 4:selectsort (A, N);
					break;
			Case 5:quicksort (A, 0, N-1);
					break;
			Case 6:heapsort (A, N);
					break;
			Case 7:mergesort (A, N);
				break;
			Case 8:countsort (A, N);
					break;
			Case 9:lsdsort (A, N);
			Case 0:break;
			Default:break
			}
		}
		finish = (double) clock ();
		printf ("%.4fms\n", (Finish-start));
	} while (P!= 0);
}

In this small test, a total of nine sorting methods were tested: bubble sort, insert sort, hill sort, select sort, quick sort, heap sort, merge sort, count sort, cardinal sort.

Next, let's take a look at the results of the test:


The results of the operation can be concluded:


By this simple little test, we can know that the fastest sort is the counting sort, then the cardinality sort, and the bubbling sort is the longest running method.

This is just a simple small test, there are imperfect places, look more advice.



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.