# The measurement of the complexity of sorting algorithm

Posted by luanne on Sun, 14 Jun 2020 04:59:15 +0200

# 3, Algorithm Construction:

## 1. Select sort:

The number of len is len-1;

## 2. Bubble sort:

In each round of price comparison, the comparison between two pairs of len-1 and len-j will be carried out. After each round of price comparison, the maximum value will sink to the bottom;

## 3. Merge sort:

The sequence to be sorted is divided into two parts. The two parts are sorted by merging again, and then they are merged;

## 4. Quick sort:

Array takes the first number (default) as the base number. Put all numbers larger than this number on its right, and all numbers smaller than or equal to it on its left. Repeat the above steps for the left and right intervals until there is only one number in each interval;

## 5. Insert sort:

The first number of the array is recorded as an ordered sequence, followed by len-1 numbers to form a non sequence, and the numbers in the unordered sequence are inserted into the ordered sequence in turn.

# Algorithm implementation

/*
* time: 2020 June 11, 2006
* author: Xue Kaixuan
* description: Time consumed by sorting algorithm under different data scales
* compiler: codeblocks
*/
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include<Windows.h>
int begintime = 0;
int endtime = 0;
int arr1[100000] = { 0 };
void swap(int* a, int* b){
int c = 0;
c = *a;
*a = *b;
*b = c;
}
//Select sorting;
void select_sort(int arr[], int len){
int min = 0;
for (int i = 0; i < len - 1; i++){
min = i;
for (int j = i + 1; j < len; j++){
if (arr[min] > arr[j]){
min = j;
}
}
if (min != i){
swap(&arr[min], &arr[i]);
}
}
}
//Bubble sorting;
void maopao_sort(int arr[], int len){
for (int i = 0; i < len - 1; i++){
for (int j = 0; j < len - i; j++){
if (arr[j] > arr[j + 1]){
swap(&arr[j], &arr[j + 1]);
}
}
}
}
void merge(int arr[], int low, int mid, int high){
int i, k;
int *tmp = (int *)malloc((high - low + 1)*sizeof(int));
int left_low = low;
int left_high = mid;
int right_low = mid + 1;
int right_high = high;
for (k = 0; left_low <= left_high && right_low <= right_high; k++){
if (arr[left_low] <= arr[right_low]){
tmp[k] = arr[left_low++];
}
else{
tmp[k] = arr[right_low++];
}
}
if (left_low <= left_high){
for (i = left_low; i <= left_high; i++){
tmp[k++] = arr[i];
}
}
if (right_low <= right_high){
for (i = right_low; i <= right_high; i++){
tmp[k++] = arr[i];
}
}
for (i = 0; i < high - low + 1; i++){
arr[low + i] = tmp[i];
}
free(tmp);
}
//Merge sort;
void merge_sort(int arr[], unsigned int first, unsigned int last){
int mid = 0;
if (first < last){
mid = first/2 + last/2;
merge_sort(arr, first, mid);
merge_sort(arr, mid + 1, last);
merge(arr, first, mid, last);
}
}
//The function of quick sorting;
void quick_sort(int arr[], int begin, int end){
int i = begin;
int j = end;
int x = arr[begin];
if (begin < end){
while (begin < end){
while (begin<end && arr[end]>x){
end--;
}
if (begin < end){
arr[begin++] = arr[end];
}
while (begin < end && arr[begin] < x){
begin++;
}
if (begin < end){
arr[end--] = arr[begin];
}
}
arr[begin] = x;
quick_sort(arr, i, end - 1);//Use recursion to sort the left and right sides of the selected standard number;
quick_sort(arr, begin + 1, j);
}
}
//Insert sort;
void charu_sort(int arr[], int len){
int t = 0;
int j = 0;
for (int i = 1; i < len; i++){
t = arr[i];
for (j = i - 1; j >= 0 && t < arr[j]; j--){
arr[j + 1] = arr[j];
}
arr[j + 1] = t;
}
}
void select_time(int len){
int sum = 0;
int n = 0;
printf("20 Group sample(ms):>");
while (n < 20){
for (int i = 0; i < len; i++){
arr1[i] = rand();
}
begintime = clock();
select_sort(arr1,len);
endtime = clock();
sum = sum + endtime - begintime;
printf(" %d ", endtime - begintime);
if (n == 19){
printf("\n");
}
n++;
}
printf("Average time spent sorting:> %d \n", (sum / 20));
}
//Calculate the time for bubble sorting of 20 groups of samples
void maopao_time(int len){
int sum = 0;
int n = 0;
printf("20 Group sample(ms):>");
while (n < 20){
for (int i = 0; i < len; i++){
arr1[i] = rand();
}
begintime = clock();
maopao_sort(arr1, len);
endtime = clock();
sum = sum + endtime - begintime;
printf(" %d ", endtime - begintime);
if (n == 19){
printf("\n");
}
n++;
}
printf("Average time spent sorting:> %d \n", (sum / 20));
}
void merge_time(int len){
int sum = 0;
int n = 0;
printf("20 Group sample(ms):>");
while (n < 20){
for (int i = 0; i < len; i++){
arr1[i] = rand();
}
begintime = clock();
merge_sort(arr1, 0, len - 1);
endtime = clock();
sum = sum + endtime - begintime;
printf(" %d ", endtime - begintime);
if (n == 19){
printf("\n");
}
n++;
}
printf("Average time spent sorting:> %d \n", (sum / 20));
}
void quick_time(int len){
int sum = 0;
int n = 0;//Samples;
printf("20 Group sample(ms):>");
while (n < 20){
for (int i = 0; i < len; i++){
arr1[i] = rand();
}
begintime = clock();
quick_sort(arr1, 0, len - 1);
endtime = clock();
sum = sum + endtime - begintime;
printf(" %d ", endtime - begintime);
if (n == 19){
printf("\n");
}
n++;
}
printf("Average time spent sorting:> %d \n", (sum / 20));
}
//Calculate the time taken for insertion sorting of 20 groups of samples
void charu_time(int len){
int sum = 0;
int n = 0;
printf("20 Group sample(ms):>");
while (n < 20){
for (int i = 0; i < len; i++){
arr1[i] = rand();
}
begintime = clock();
charu_sort(arr1, len);
endtime = clock();
sum = sum + endtime - begintime;
printf(" %d ", endtime - begintime);
if (n == 19){
printf("\n");
}
n++;
}
printf("Average time spent sorting:> %d \n", (sum / 20));
}
int main(){
int input = 0;
int len = 10;
while (input<1||input>5){
printf("1.Select sort 2.Bubble sort 3.Merge sort 4.Quick sort 5.Insert sort\n");
printf("Please enter the algorithm you selected:");
scanf("%d", &input);
switch (input){
case 1:
while (len <= 100000){
printf("Array length is%d When:\n",len);
select_time(len);
if (len != 100000){
printf("----------------------------------------------------------------------------------\n");
}
len = len * 10;
}
break;
case 2:
while (len <= 100000){
printf("Array length is%d When:\n", len);
maopao_time(len);
if (len != 100000){
printf("----------------------------------------------------------------------------------\n");
}
len = len * 10;
}
break;
case 3:
while (len <= 100000){
printf("Array length is%d When:\n", len);
merge_time(len);
if (len != 100000){
printf("----------------------------------------------------------------------------------\n");
}
len = len * 10;
}
break;
case 4:
while (len <= 100000){
printf("Array length is%d When:\n", len);
quick_time(len);
if (len != 100000){
printf("----------------------------------------------------------------------------------\n");
}
len = len * 10;
}
break;
case 5:
while (len <= 100000){
printf("Array length is%d When:\n", len);
charu_time(len);
if (len != 100000){
printf("----------------------------------------------------------------------------------\n");
}
len = len * 10;
}
break;
default:
}
}
system("pause");
return 0;
}

# 5, Commissioning, testing and operation results

1. Commissioning

2. Test and operation results

# 6, Experience induction

The fastest sorting algorithm of fast sorting has the disadvantage of instability; the algorithm of merging and sorting the second block has the disadvantage of large auxiliary storage; the space complexity is O(logn) when the fast sorting is optimal; the space complexity is O(n) when the array is bisected every time.

Topics: Windows