快速排序算法

// 1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#define MAXSIZE 10
#define Sqtype int

typedef struct
{
	Sqtype r[MAXSIZE+1];
	int length;
}SqList;

void QSort(SqList *L,int low,int high);
int Partition(SqList *L,int low,int high);
void swap(SqList *L,int i,int j);


void QuickSort(SqList *L)
{
	QSort(L,0,L->length-1);
}

void QSort(SqList *L,int low,int high)
{
	int pivot;
	if(low<high)
	{
		pivot = Partition(L,low,high);

		QSort(L,low,pivot-1);
		QSort(L,pivot+1,high);
	}
}

int Partition(SqList *L,int low,int high)
{
	Sqtype pivotkey = L->r[low];
	while(low<high)
	{
		while(low<high&&L->r[high]>=pivotkey)
			high--;
		swap(L,low,high);

		while(low<high&&L->r[low]<=pivotkey)
			low++;
		swap(L,low,high);
	}
	return low;
}

void swap(SqList *L,int i,int j)
{
	Sqtype temp = L->r[i];
	L->r[i] = L->r[j];
	L->r[j] = temp;
}

int main(int argc, char* argv[])
{
	SqList L;
	int i=0;
	Sqtype temp[MAXSIZE] = {20,90,60,70,10,50,80,40,30,0};
	memcpy(L.r,temp,sizeof(temp));
	L.length = MAXSIZE;

	QuickSort(&L);

	for(i=0;i<L.length;i++)
	{
		printf("%d\n",L.r[i]);
	}

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值