Quick Sort

//header in use
#include <stdio.h>
#include <tchar.h>

#include <stdlib.h>
#include <time.h>
#include <iostream>

using namespace std;
#define MAX 20

int _tmain(int argc, _TCHAR* argv[])
{
	int sortbox[MAX];
	long start, end;
	int Z = 0;
	void QuickSort(int *, int, int);
	srand(time(NULL));						//using rand() and time() to build ramdom number vector
	start = clock();
	while(Z != 1000){
		for(int i = 0; i != MAX; i++){
			sortbox[i] = rand() % 1000;
//			cout<<sortbox[i]<<' ';
		}
		QuickSort(sortbox, 0, MAX-1);
		Z++;
	}
	end = clock() - start;
	cout << endl;
	for(int i = 0; i != MAX; i++)
	{
		cout<<sortbox[i]<<' ';
	}
	cout << endl << end;
	return 0;
}

void QuickSort(int *box,int l, int r)				//
{
	int s = 0;
	int partition(int *, int, int );
	if(l<r){
		s = partition(box, l, r);
		QuickSort(box, l, s-1);
		QuickSort(box, s+1, r);
	}
	return;
}

int partition(int *box, int l, int r)
{
	int p = box[l];
	int i = l;
	int j = r + 1;
	void swap(int &, int &);							//referance! Use it carefully

	
	while(j>=i){
		do i++; while(i <= r && box[i] < p);
		do j--; while(j >=l && box[j] > p);
		swap(box[i], box[j]);
	}
	swap(box[i], box[j]);
	swap(box[l], box[j]);
	/*///
	cout << endl;
	for(int i = 0; i != MAX; i++)
	{
		cout<<box[i]<<' ';
	}
	*/
	return j;
}

void swap(int &a, int &b)
{
	int tmp = a;
	a = b;
	b= tmp;
	return;
}

 

自己写的快速排序,随机生成数列循环一千次并记录时间,时间很不均匀,差别很大。本来希望实现选择前中后的中位数来进行但是出了问题,就没有写入了。

Quick sort is a popular sorting algorithm that works by partitioning an array into two sub-arrays, and then recursively sorting each sub-array. It is a divide-and-conquer algorithm that has an average time complexity of O(n log n), making it one of the fastest sorting algorithms. The basic idea behind quick sort is to select a pivot element, partition the array around the pivot element, and then recursively apply the same process to each of the sub-arrays. The partitioning process involves selecting a pivot element, rearranging the array so that all elements less than the pivot are on one side and all elements greater than the pivot are on the other side, and then returning the index of the pivot element. This pivot index is then used to divide the array into two sub-arrays, which are recursively sorted. Here's an example implementation of quick sort in Python: ``` def quick_sort(arr): if len(arr) <= 1: return arr else: pivot = arr[0] left = [] right = [] for i in range(1, len(arr)): if arr[i] < pivot: left.append(arr[i]) else: right.append(arr[i]) return quick_sort(left) + [pivot] + quick_sort(right) ``` This implementation selects the first element of the array as the pivot, and then uses list comprehensions to create the left and right sub-arrays. The left sub-array contains all elements less than the pivot, while the right sub-array contains all elements greater than or equal to the pivot. The function then recursively sorts the left and right sub-arrays and combines them with the pivot element to produce the final sorted array.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值