源代码下载路径 http://download.csdn.net/detail/dxt1107/6450399,原创。
main.c
/*
Author:duxiaotian
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sort.h"
#include "common.h"
#define MAX_ELEM_NUM 32
#define SORT_NUM 10
static int def[MAX_ELEM_NUM] = {-1,7,2,30,12,5,19,8,4,18,8,0,0,0,0,0};
void showMenu(void)
{
printf("1.heap sort\n");
printf("2.quick sort\n");
printf("3.insert sort\n");
printf("4.merge sort\n");
printf("5.shell sort\n");
printf("choose sort algorithm:\n>");
}
inline void restoreDefault(int A[])
{
memcpy(A,def,MAX_ELEM_NUM*sizeof(int));
}
int main(int argc,char *argv[])
{
int A[MAX_ELEM_NUM];
char c;
restoreDefault(A);
printf("unsort array:");
showArray(A,SORT_NUM);
showMenu();
while(c = getchar()){
if (c == 0x0a){
continue;
}
switch(c){
case '1':
printf("heap sort:\n");
heapSort(A,SORT_NUM);
break;
case '2':
printf("quick sort:\n");
quickSort(A,1,SORT_NUM);
break;
case '3':
printf("insert sort:\n");
insertSort(A,SORT_NUM);
break;
case '4':
printf("merge sort:\n");
mergeSort(A,1,SORT_NUM);
break;
case '5':
printf("shell sort:\n");
shellSort(A,SORT_NUM);
break;
case 'q':
exit(1);
default:
break;
}
restoreDefault(A);
printf("restore default:");
showArray(A,SORT_NUM);
showMenu();
}
return 0;
}
直接插入排序
#include <stdio.h>
#include "common.h"
void insertSort(int A[],int n)
{
int i,j;
for (i = 2;i <= n;i++){
if (A[i] < A[i-1]){
A[0] = A[i];
for (j = i-1;A[0] < A[j];j--){
A[j+1] = A[j];
}
A[j+1] = A[0];
}
showArray(A,n);
}
}
堆排序
#include <stdio.h>
#include "common.h"
void adjustDown(int A[],int k,int n){
int i;
A[0] = A[k];//复制为哨兵
// 2*k为左节点
for (i = 2*k;i <= n;i = i*2){
//A[i]<A[i+1],即右节点大于左节点则移动到右节点与根节点比较。
if (i < n && A[i] < A[i+1])
i++;
if (A[0] < A[i]){//若根节点较小则交换
A[k] = A[i];
k = i;
}
else{
break;
}
}
A[k] = A[0];
}
void buildHeap(int A[],int n)
{
int i;
for(i = n/2;i > 0 ;i--){
printf("i = %d,A[i] = %d\n",i,A[i]);
adjustDown(A,i,n);
showArray(A,n);
}
}
void heapSort(int A[],int n)
{
int i;
printf("build heap:\n");
buildHeap(A,n);
printf("begin sort:\n");
for (i = n;i >=1;i--){
swap(&A[1],&A[i]);
adjustDown(A,1,i-1);
showArray(A,n);
}
}
二路归并排序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "common.h"
void merge(int A[],int left,int mid,int right)
{
int i,j,k;
int B[256];
memset(B,0x0,sizeof(B));
for (i = left;i <= right;i++){
B[i] = A[i];
}
for (k = left,i = left,j = mid+1;i <= mid && j <= right;k++)
{
if (B[i] < B[j]){
A[k] = B[i];
i++;
}
else{
A[k] = B[j];
j++;
}
}
while(i <= mid){
A[k++] = B[i++];
}
while(j <= right){
A[k++] = B[j++];
}
}
void mergeSort(int A[],int left,int right)
{
int mid;
if (left < right){
mid = (left+right)/2;
mergeSort(A,left,mid);
mergeSort(A,mid+1,right);
merge(A,left,mid,right);
showArray(A,10);
}
}
快速排序
/*
Author:dxt
*/
#include <stdio.h>
#include "common.h"
void quickSort(int A[],int p,int r)
{
showArray(A,10);
int q;
if (p < r){
q = partition(A,p,r);
quickSort(A,p,q-1);
quickSort(A,q+1,r);
}
}
inline int partition(int *A,int p,int r)
{
// printf("partition:[%d %d] ---",p,r);
int i = p-1,j;
int tmp;
int x = A[r];
for (j = p;j < r;j++){
if (A[j] <= x){
i++;
swap(&A[i],&A[j]);
}
}
swap(&A[i+1],&A[r]);
return i+1;
}
希尔排序
/*
Author:duxiaotian
*/
#include <stdio.h>
#include "common.h"
void shellSort(int A[],int n)
{
int i,j;
int dk;
for (dk = n/2;dk >=1;dk=dk/2){
for (i = dk+1;i <= n;i++){
if (A[i] < A[i-dk]){
A[0] = A[i];
for (j = i-dk;j > 0&& A[0] < A[j];j-=dk)
A[j+dk]=A[j];
A[j+dk] = A[0];
}
}
printf("dk = %d\n",dk);
showArray(A,n);
}
}
Makefile
OBJS= mergesort.o insertsort.o quicksort.o heapsort.o shellsort.o main.o
EXEC=sort
all:$(EXEC)
@echo "generate sort binary......ok!"
$(EXEC):$(OBJS)
gcc -o $@ $(OBJS)
clean:
rm *.o $(EXEC)
common.h
#ifndef __COMMON_H__
#define __COMMON_H__
inline static void showArray(int A[],int n)
{
int i;
for (i = 1;i <= n;i++){
printf("(%d) ",A[i]);
}
printf("\n\n");
}
inline static void swap(int *x,int *y){
int tmp;
tmp = *x;
*x = *y;
*y = tmp;
}
#endif
sort.h
#ifndef __SORT_H__
#define __SORT_H__
void heapSort(int A[],int n);
void quickSort(int A[],int p,int r);
void insertSort(int A[],int n);
void mergeSort(int A[],int left,int right);
#endif
堆排序建堆过程图解:
建堆完成。
堆排序首先必须建立好堆,然后不断交换堆顶元素与最后未排序元素,
如第一次将A[1]= 30与A[10]=5交换,得到第一个排好序的元素,即堆顶元素,
交换后已经不是大根堆,需要向下调整,使其成为大根堆,然后交换A[1]与A[9],这样
反复n次后得到排好序的序列。
堆排序的核心在于怎样向下调整堆,结合图解就很容易明白。后续会有更详细其他算法图解。
以上文件对各类排序算法进行了统一管理,方便学习!
代码下载路径 http://download.csdn.net/detail/dxt1107/6450399