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;
}
#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;
}
相关文章推荐
- 20150124 【 Linux-C 函数指针 】 模拟快速排序程序--快排
- linux bash shell实现对数组快速排序(升序)
- 【linux C】快速排序实现
- Linux各大发行版介绍
- 交叉编译场景分析(arm-linux)(七)--编译sqlite
- 嵌入式Linux操作系统学习规划
- linux 中的一点知识--必知十事
- 关于linux中nfs搭建常见的故障
- 详解 Linux系统集群的安装与并行计算
- Linux网络服务器
- 在linux上开发php的安装!
- ubuntu(linux)安装VMware-Workstation-6.5.1
- Linux+Apache2+openssl实现https验证
- Linux的应用--Video Streaming探讨 六
- 安装和使用花生壳(linux)
- linux+apache+php+mysql安装配置文档
- Linux 命令集
- Linux之网络配置
- linux下终止用户会话二法
- Linux下通过java程序启动sh文件