Sorts

See this ariticle on my own blog https://dyingdown.github.io/2020/01/16/Sorts/
Here are some sorting ways and I use many data to test and compare. Just to illustrate the property of each algorithm.

Insertion Sort

//
// Insertion_Sort
//
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e8 + 10;

ll compare = 0;
ll insert_sort(int num[], int len) {
	ll times = 0;
	for(int i = 1; i < len; i++) {
		int key = num[i];
		int j = i - 1;
		while(j >= 0 && key < num[j]) {
			compare ++;
//			cout << compare << endl;
			num[j + 1] = num[j];
			j--; 
		}
		num[j + 1] = key;
		times ++;
	}
	return times;
}

int arr[MAXN];

void fun(const char* name) {
	compare = 0;
	clock_t st, ed;
	int len = 0;
	ifstream file(name);
	while(file >> arr[len++]);
	st = clock();
	ll times = insert_sort(arr, len);
	ed = clock();
	printf("%s\t%lldms\t%lld\t%lld\n", name, ed - st, times, compare);
}
int main() {
	fun("new_100.txt");
	fun("new_10000.txt");
	fun("new_100000.txt");
	fun("new_1000000.txt");
	fun("new_10000000.txt");
	fun("new_100000000.txt");
	fun("100.txt");
	fun("10000.txt");
	fun("100000.txt");
	fun("1000000.txt");
	fun("10000000.txt");
	fun("100000000.txt");
	return 0;
}

Data is divided into two parts, deduplicated and undeduplicated raw data

Datatimesswap timecompare time
new_100.txt0ms1002690
new_10000.txt27ms863318573679
new_100000.txt159ms31211245057118
new_1000000.txt194ms32768269903790
new_10000000.txt219ms32768269871114
new_100000000.txt191ms32768269870958
raw_100.txt0ms1002690
raw_10000.txt15ms1000025296862
raw_100000.txt1405ms1000002499320214
raw_1000000.txt251126ms1000000249837170214
raw_10000000.txtData lostData lostData lost
raw_100000000.txt30h+??

Binary Insertion Sort

//
// Insertion_Sort
//
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e8 + 10;

ll compare = 0;
ll BInsert_sort(int num[], int len) {
	ll times = 0;
	for(int i = 2; i < len; i++) {
		int key = num[i];
		int low = 1, high = i - 1, mid = (low + high) / 2;
		while(low <= high) {
			compare ++;
			mid = (low + high) / 2;
			if(key < num[mid]) {
				high = mid - 1;
			} else {
				low = mid + 1;
			}
		}
		for(int j = i - 1; j >= high + 1; j --) {
			num[j + 1] = num[j];
		}
		num[high + 1] = key;
		times ++;
	}
	return times;
}

int arr[MAXN];

void fun(const char* name) {
	compare = 0;
	clock_t st, ed;
	int len = 0;
	ifstream file(name);
	while(file >> arr[len++]);
	st = clock();
	ll times = BInsert_sort(arr, len);
	ed = clock();
	printf("%s\t%lldms\t%lld\t%lld\n", name, ed - st, times, compare);
}
int main() {
	fun("new_100.txt");
	fun("new_10000.txt");
	fun("new_100000.txt");
	fun("new_1000000.txt");
	fun("new_10000000.txt");
	fun("new_100000000.txt");
	fun("100.txt");
	fun("10000.txt");
	fun("100000.txt");
	fun("1000000.txt");
	fun("10000000.txt");
	fun("100000000.txt");
	return 0;
}
Datatimesswap timecompare time
Duplicated_100.txt0ms99532
Duplicated_10000.txt5ms8632100941
Duplicated_100000.txt64ms31210422787
Duplicated_1000000.txt83ms32767446088
Duplicated_10000000.txt70ms32767446053
Duplicated_100000000.txt47ms32767446085
raw_100.txt0ms99532
raw_10000.txt1ms999910001
raw_100000.txt423ms99999100001
raw_1000000.txt50109ms99999918547631
raw_10000000.txt18406979ms9999999218645622
raw_100000000.txt30h+??

Shell Sort

//
// Insertion_Sort
//
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e8 + 10;

ll compare = 0;
ll Shell_sort(int num[], int len) {
	ll times = 0;
    for (int increment = len / 2; increment > 0; increment /= 2){
        for (int i = increment; i < len; i++) {
            int insert_num = num[i], j;
            for (j = i - increment; j >= 0; j -= increment) {
            	compare ++;
                if (num[j] > insert_num) 
                    num[j + increment] = num[j];
                else  break;
            }
            num[j + increment] = insert_num;
            times ++;
        }
    }
	return times;
}

int arr[MAXN];

void fun(const char* name) {
	compare = 0;
	clock_t st, ed;
	int len = 0;
	ifstream file(name);
	while(file >> arr[len++]);
	st = clock();
	ll times = Shell_sort(arr, len);
	ed = clock();
	printf("%s\t%lldms\t%lld\t%lld\n", name, ed - st, times, compare);
}
int main() {
	fun("new_100.txt");
	fun("new_10000.txt");
	fun("new_100000.txt");
	fun("new_1000000.txt");
	fun("new_10000000.txt");
	fun("new_100000000.txt");
	fun("100.txt");
	fun("10000.txt");
	fun("100000.txt");
	fun("1000000.txt");
	fun("10000000.txt");
	fun("100000000.txt");
	return 0;
}
Datatimesswap timecompare time
Duplicated_100.txt0ms509957
Duplicated_10000.txt2ms103615218937
Duplicated_100000.txt8ms405766990732
Duplicated_1000000.txt8ms4587684114313
Duplicated_10000000.txt11ms4587683149548
Duplicated_100000000.txt9ms4587683395501
raw_100.txt0ms509957
raw_10000.txt0ms12001810001
raw_100000.txt34ms1500022100001
raw_1000000.txt254ms1800002664758346
raw_10000000.txt2917ms220000031966765936
raw_100000000.txt57906ms250000003814053571221

Bubble Sort

//
// Bubble_Sort
//
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e8 + 10;

ll compare = 0;

ll bubble_sort(int num[], int len) {
	ll times = 0;
	for(int i = 0; i < len; i++) {
		for(int j = i + 1; j < len; j ++) {
			if(num[i] > num[j]) {
				swap(num[i], num[j]);
				times ++;
			}
		}
		compare ++;

	}
	return times;
}

int arr[MAXN];

void fun(const char* name) {
	compare = 0;
	clock_t st, ed;
	int len = 0;
	ifstream file(name);
	while(file >> arr[len++]);
	st = clock();
	ll times = bubble_sort(arr, len);
	ed = clock();
	printf("%s\t%lldms\t%lld\t%lld\n", name, ed - st, times, compare);
}

int main() {
//	freopen("in.txt", "r", stdin);
	fun("new_100.txt");
	fun("new_10000.txt");
	fun("new_100000.txt");
	fun("new_1000000.txt");
	fun("new_10000000.txt");
	fun("new_100000000.txt");
	fun("100.txt");
	fun("10000.txt");
	fun("100000.txt");
	fun("1000000.txt");
	fun("10000000.txt");
	fun("100000000.txt");
	return 0;
}
Datatimesswap timecompare time
Duplicated_100.txt0ms2690101
Duplicated_10000.txt135ms185736798634
Duplicated_100000.txt1633ms24505711831212
Duplicated_1000000.txt1822ms26990379032769
Duplicated_10000000.txt1869ms26987111432769
Duplicated_100000000.txt1792ms26987095832769
raw_100.txt0ms2690101
raw_10000.txt220ms2290346910001
raw_100000.txt15912ms1125994864100001
raw_1000000.txt1258151ms158514592011000001
raw_10000000.txt30h+??
raw_100000000.txt30h+??

Heap Sort

//
// Heap Sort
//
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e8 + 10;

ll compare = 0;
void make_heap(int num[], int len) {
	for (int i = len - 1; i > 0; i --) {
		if(i & 1 && num[i] > num[(i - 1) / 2])
			swap(num[i], num[(i - 1) / 2]);
		else if( !(i & 1) and num[i] > num[(i - 2) / 2])
			swap(num[i], num[(i - 2) / 2]);
		compare ++;
	}
}
ll heap_sort(int num[], int len) {
	ll times = 0;
	while(len) {
		make_heap(num, len) ;
		len --;
		swap(num[0], num[len]);
		times ++;
	}
	return times;
}

int arr[MAXN];

void fun(const char* name) {
	compare = 0;
	clock_t st, ed;
	int len = 0;
	ifstream file(name);
	while(file >> arr[len++]);
	st = clock();
	ll times = heap_sort(arr, len);
	ed = clock();
	printf("%s\t%lldms\t%lld\t%lld\n", name, ed - st, times, compare);
}
int main() {
	fun("new_100.txt");
	fun("new_10000.txt");
	fun("new_100000.txt");
	fun("new_1000000.txt");
	fun("new_10000000.txt");
	fun("new_100000000.txt");
	fun("100.txt");
	fun("10000.txt");
	fun("100000.txt");
	fun("1000000.txt");
	fun("10000000.txt");
	fun("100000000.txt");
	return 0;
}
Datatimesswap timecompare time
new_100.txt0ms1015050
new_10000.txt45ms863437268661
new_100000.txt718ms31212487078866
new_1000000.txt801ms32769536887296
new_10000000.txt802ms32769536887296
new_100000000.txt801ms32769536887296
100.txt0ms1015050
10000.txt96ms1000150005000
100000.txt9708ms1000015000050000
1000000.txt2059306ms1000001500000500000

Quick Sort

//
// Quick_Sort
// 
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e8 + 10;

ll compare = 0; 
ll times = 0;
void quick_sort(int num[], int start, int end){
	
    if (start >= end) return;
    int l = start, r = end;
    int pivot = num[(end - start) / 2 + start];;
    while (l <= r){
        while (l <= r && num[r] > pivot){
        	compare ++;
        	r--;
		}  
        while (l <= r && num[l] < pivot) {
        	compare ++;
        	l++;
		}  
        if (l <= r) {
        	times ++;
        	swap(num[l++], num[r--]);
		}
    }
    quick_sort(num, start, r);
    quick_sort(num, l, end); 
}


int arr[MAXN];

void fun(const char* name) {
	compare = 0;
	times = 0;
	clock_t st, ed;
	int len = 0;
	ifstream file(name);
	while(file >> arr[len++]);
	st = clock();
	quick_sort(arr, 0, len - 1);
	ed = clock();
	printf("%s\t%lldms\t%lld\t%lld\n", name, ed - st, times, compare);
}
int main() {
	fun("new_100.txt");
	fun("new_10000.txt");
	fun("new_100000.txt");
	fun("new_1000000.txt");
	fun("new_10000000.txt");
	fun("new_100000000.txt");
	fun("100.txt");
	fun("10000.txt");
	fun("100000.txt");
	fun("1000000.txt");
	fun("10000000.txt");
	fun("100000000.txt");
	return 0;
} 
Datatimesswap timecompare time
Duplicated_100.txt0ms180579
Duplicated_10000.txt0ms2872486065
Duplicated_100000.txt1ms117382361212
Duplicated_1000000.txt3ms123193391221
Duplicated_10000000.txt1ms123384396556
Duplicated_100000000.txt2ms123792379925
raw_100.txt0ms180579
raw_10000.txt3ms3375210001
raw_100000.txt6ms436249100001
raw_1000000.txt65ms572379114357440
raw_10000000.txt631ms72919003142909802
raw_100000000.txt6479ms8962988131346887740
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值