您的位置:首页 > 运维架构 > Linux

Linux C 快速排序

2013-11-14 17:15 141 查看
sort.h

#ifndef _SORT_H_

#define _SORT_H_

void merge(int *src,unsigned int size);

void qsort(int *src,unsigned int low,unsigned int hight);

int partition(int *src,unsigned int low,unsigned int hight);

#endif

sort.c

#include "sort.h"

#include <stdio.h>

/*****************

 * date  20131114

 */

void merge(int *src,unsigned int size){
int i = 0,j = 0;
for(;i<size;i++){
for(;j<i;j++){
if(*(src+j) > *(src +j +1)){
int temp = *(src + j);
*(src + j) = *(src + j +1);
*(src + j +1) = temp;
}
}
}

}

int partition(int *src,unsigned int low,unsigned int hight){
int lo = low;
int hi = hight;
int pivot = *(src + lo);
while(lo < hi){
while(lo<hi&&pivot < *(src + hi)) hi--;
*(src + lo) = *(src + hi);
while(lo<hi&&pivot > *(src + lo)) lo++;
*(src + hi) = *(src + lo);
}
*(src + lo) = pivot;
return lo;

}

void qsort(int *src,unsigned int low,unsigned int hight){

if(low < hight){
int pindex = partition(src,low,hight);
int k = 0;
partition(src,low,pindex );
partition(src,pindex+1,hight);
}

}

int main(void){
int a[] = {97,65,44,189,223,4,18,88,75,1,390};
int *p = (int*)a;
//merge(p,7);
qsort(p,0,6);
int k = 0;
fo
4000
r(;k<7;k++){
printf("%d\t",*(a + k));
}
return -1;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  快速排序 c