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;
for(;k<7;k++){
printf("%d\t",*(a + k));
}
return -1;
}