快速排序算法

好长时间没有写算法了,感觉手有些生,写起来不快,问题很多

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>

void swap(int v[], int i, int j)
{
	int temp = 0;
	temp = v[i];
	v[i] = v[j];
	v[j] = temp;
}

void qsort(int v[], int left, int right)
{
	int last = 0;
	int i = 0;
	if(left >= right) 
	{
		return ;
	}

	swap(v, left, (left + right)/2);
	last = left;
	for(i=left+1; i<=right; ++i)
	{
		if(v[i] < v[left])
			swap(v, ++last, i);
	}
	swap(v, last, left);
	qsort(v, left, last - 1);
	qsort(v, last+1, right);
	
}


void printV(int v[], int n)
{
	int i = 0;
	for(; i<n; ++i)
	{
		printf("%d ", v[i]);
	}

	printf("\n");
}

int main()
{
	int va[] = {2,10,6,8,7,21,23,22};
	printV(va, sizeof(va)/sizeof(int));

	qsort(va, 0, sizeof(va)/sizeof(int));
	printV(va, sizeof(va)/sizeof(int));
	return 0;
}


 

你可能感兴趣的:(快速排序算法)