用C语言实现快速排序算法

直接插入排序和快速排序函数

具体实现
#include<stdio.h>

typedef struct {
	int a[6];
	int length;
}list, * sqlist;

void insertsort(sqlist& l);//直接插入排序
/*
* 快速排序
*/
int partition(sqlist& l, int low, int high);
void qsort(sqlist& l, int low, int high);

void insertsort(sqlist& l) {
	int i, j;
	for (i = 2; i <= l->length; ++i)
		if (l->a[i] < l->a[i - 1]) {
			l->a[0] = l->a[i];
			l->a[i] = l->a[i - 1];
			for (j = i - 2; l->a[0] < l->a[j]; --j)
				l->a[j + 1] = l->a[j];
			l->a[j + 1] = l->a[0];
		}
}

int partition(sqlist& l, int low, int high) {
	int pivotkey;
	l->a[0]= l->a[low];
	pivotkey = l->a[low];
	while (low < high) {
		while (low < high && l->a[high] >= pivotkey)
			--high;
			l->a[low] = l->a[high];
		while (low < high && l->a[low] <= pivotkey) 
			++low;
		l->a[high] = l->a[low];
	}
	l->a[low] = l->a[0];
	return low;
}

void qsort(sqlist& l, int low, int high) {
	int pivotloc;
	if (low < high) {
		pivotloc = partition(l, low, high);
		qsort(l, low, pivotloc - 1);
		qsort(l, pivotloc + 1, high);
	}
}
int main() {
	list l;
	l.length = 5;
	int i;
	l.a[1] = 2;
	l.a[2] = 6;
	l.a[3] = 1;
	l.a[4] = 7;
	l.a[5] = 3;
	sqlist s = &l;
	//insertsort(s);
	qsort(s, 1, 5);
	for (i = 1; i < 6; i++) {
		printf("%d\n", s->a[i]);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序小旭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值