快速排序过程的优化

        一最左边的数为枢轴元素,进行快速排序,并对算法进行优化。同时用不同规模的数据进行测试,发现快速排序基本上与数据规模呈线性关系。以下是源代码和运行结果:

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <sys/time.h>

#define MAX 1000000

using namespace std;

void readNum(int a[]){
	string filename;
	ifstream infile("data_100w.txt", ios::in);
	string textline = "";
	int i = 0;
	while(getline(infile, textline, '\n')){
		a[i] = atoi(string(textline).c_str());
		i++;
	}
	infile.close();
}

void quick_sort_1(int a[], int left, int right){
	if(left < right){
		int pivot = a[left], tmp = -1;
		int i = left, j = right;
		while(i < j){
			while(i < j && a[j] >= pivot){
				j--;
			}
			if(i < j){
				tmp = a[i];
				a[i] = a[j];
				a[j] = tmp;
			}
			while(i < j && a[i] <= pivot){
				i++;
			}
			if(i < j){
				tmp = a[i];
				a[i] = a[j];
				a[j] = tmp;
			}
		}
		quick_sort_1(a, left, i - 1);
		quick_sort_1(a, i + 1, right);
	}
}

void quick_sort_2(int a[], int left, int right){
	if(left < right){
		int pivot = a[left], tmp = -1;
		int i = left, j = right;
		while(i < j){
			while(i < j && a[j] >= pivot){
				j--;
			}
			while(i < j && a[i] <= pivot){
				i++;
			}
			if(i < j){
				tmp = a[i];
				a[i] = a[j];
				a[j] = tmp;
			}
		}
		a[left] = a[i];
		a[i] = pivot;
		quick_sort_2(a, left, i - 1);
		quick_sort_2(a, i + 1, right);
	}
}

void quick_sort_3(int a[], int left, int right){
	if(left < right){
		int pivot = a[left];
		int i = left, j = right;
		while(i < j){
			while(i < j && a[j] >= pivot){
				j--;
			}
			if(i < j){
				a[i] = a[j];
			}
			while(i < j && a[i] <= pivot){
				i++;
			}
			if(i < j){
				a[j] = a[i];
			}
		}
		a[i] = pivot;
		quick_sort_3(a, left, i - 1);
		quick_sort_3(a, i + 1, right);
	}
}
 
int verify(int a[]){
	for(int i = 0; i < MAX - 1; i++){
		if(a[i] > a[i+1])
			return -1;
	}
	return 0;
}

long getCurrentTime(){
	struct timeval tv;
	gettimeofday(&tv, NULL);
	return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

int main()
{
	int a[MAX] = {0};
	int b[MAX] = {0};
	int c[MAX] = {0};

	readNum(a);
	readNum(b);
	readNum(c);

	cout << "Number Counts:" << MAX << endl;
	cout << "Sort Result:" << endl;

	long time_1 = getCurrentTime();
	cout << "quick_sort_1 had spend ";
	quick_sort_1(a, 0, MAX-1);
	long time_2 = getCurrentTime();
	cout << time_2 - time_1 << " ms, sort result : ";
	cout << (verify(a) == 0 ? "True" : "False") << endl;

	long time_3 = getCurrentTime();
	cout << "quick_sort_2 had spend ";
	quick_sort_2(b, 0, MAX-1);
	long time_4 = getCurrentTime();
	cout << time_4 - time_3 << " ms, sort result : ";
	cout << (verify(b) == 0 ? "True" : "False");
	double bili = 100 * (time_2 - time_1 - time_4 + time_3) / (time_2 - time_1);
	cout << ", 优化幅度:" << bili << "%" << endl;	

	long time_5 = getCurrentTime();
	cout << "quick_sort_3 had spend ";
	quick_sort_3(c, 0, MAX-1);
	long time_6 = getCurrentTime();
	cout << time_6 - time_5 << " ms, sort result : ";
	cout << (verify(c) == 0 ? "True" : "False");
	double bili2 = 100 * (time_2 - time_1 - time_6 + time_5) / (time_2 - time_1);
	cout << ", 优化幅度:" << bili2 << "%" << endl;	
	
	return 1;
}

运行结果:

100万数据的运行结果

1000万数据 的运行结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值