面试题目之简单算法【快速排序】

H公司2010年笔试题目,快速排序

#include "stdio.h"
#include "conio.h"
#include "string.h"

void main()
{
void quickSort( int date[], int low, int high);
int a[6];
printf("请输入6个数字\n");
for(int i = 0; i<6; i++)
{
scanf("%d", &a[i]);
}

int arrSize = sizeof(a)/sizeof(a[0]);
printf("数组元素的个数:%d\n",arrSize);

quickSort(a,0,arrSize-1);

//排序好的数字进行输出
for(int i=0; i < 6; i++)
{
printf("%d,",a[i]);

}
printf("\n");
}


void quickSort( int date[], int low, int high)
{
int l,h,temp;
if(low < high)
{
l = low;
h = high;
temp = date[l]; //枢轴
while(l < h)
{
while(l < h && date[h] > temp ) h--; //从右边向左搜索 小于temp的数字
if(l < h)
{
date[l++] = date[h];
}

while(l < h && date[l] < temp ) l++; //从左向右搜索大于temp的数字
if(l < h)
{
date[h--] = date[l];
}
}

date[l] = temp;
quickSort(date, low, l-1);
quickSort(date, l+1, high);
}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值