(快速排序,堆排序,希尔排序,选择排序,交换排序,插入排序)
分为.h ,.C文件可以持有对代码的持有权
下面是.h文件
#ifndef _STRIGHTINSERTSORT_H_
#define _STRIGHTINSERTSORT_H_
#define QUICK_SORT 0
#define HEAP_SORT 1
#define SHELL_SORT 2
#define STRIGHT_SELECT_SORT 3
#define STRIGHT_SWAP_SORT 4
#define STRIGHT_INSERT_SORT 5
typedef void (*SORT)(int *, int);
int sort(int *array, int count, int sortType);
void quickSort(int *array, int count);
void heapSort(int *array, int count);
void shellSort(int *array, int count);
void strightSwapSort(int *array, int count);
void strightSelectSort(int *array, int count);
void strightInsertSort(int *array, int count);
void show(int *array, int count);
int * randArray(int count, int max, int min);
#endif
下面是.c文件
#include <stdio.h>
#include <malloc.h>
#include "sort.h"
const SORT sortFun[] = {
quickSort,
heapSort,
shellSort,
strightSelectSort,
strightSwapSort,
strightInsertSort,
};
int sort(int *array, int count, int sortType) {
if (sortType < 0 || sortType >= sizeof(sortFun)/sizeof(SORT)) {
printf("请输入正确的sortType\n");
return 0;
}
sortFun[sortType](array, count);
}
static void stepInsertSort(int *array, int count, int start, int step);
static void adjustHeap(int *array, int count, int root);
static int swapOnceSort(int *array, int head, int tail);
static void innerQuickSort(int *array, int head, int tail);
static void innerQuickSort(int *array, int head, int tail) {
//不稳定 O(nlog2^n)
int middle;
if (head < tail) {
middle = swapOnceSort(array, head, tail);
innerQuickSort(array, head, middle-1);
innerQuickSort(array,middle+1, tail);
}
}
static int swapOnceSort(int *array, int head, int tail) {
int tmp = array[head];
while (head < tail) {
while (head < tail && array[tail] > tmp) {
--tail;
}
if (head < tail) {
array[head++] = array[tail];
}
while (head < tail && array[head] < tmp) {
++head;
}
if (head < tail) {
array[tail--] = array[head];
}
}
array[head] = tmp;
return head;
}
static void stepInsertSort(int *array, int count, int start, int step) {
int index;
int i;
int j;
int tmp;
for (index = start + step; index < count; index += step) {
tmp = array[index];
for (i = start; i < index && tmp >= array[i]; i += step) {
}
for (j = index; j > i; j -= step) {
array[j] = array[j-step];
}
array[i] = tmp;
}
}
static void adjustHeap(int *array, int count, int root) {
int leftIndex;
int rightIndex;
int maxIndex;
int tmp;
while (root < count/2) {
leftIndex = root*2 + 1;
rightIndex = root*2 + 2;
maxIndex = rightIndex > count-1 ? leftIndex
: ((array[rightIndex] > array[leftIndex]) ? rightIndex : leftIndex);
if (array[root] < array[maxIndex]) {
tmp = array[root];
array[root] = array[maxIndex];
array[maxIndex] = tmp;
root = maxIndex;
} else {
return ;
}
}
}
void quickSort(int *array, int count) {
innerQuickSort(array, 0, count-1);
}
void heapSort(int *array, int count) {
//不稳定 nlog2^n
int root;
int tmp;
for (root = count/2 - 1; root > 0; root--) {
adjustHeap(array, count, root);
}
while (count > 0) {
adjustHeap(array, count, 0);
tmp = array[0];
array[0] = array[count-1];
array[count - 1] = tmp;
count--;
}
}
void shellSort(int *array, int count) {
//不稳定 nlog^2 n
int step;
int start;
for (step = count/2; step > 0; step /= 2) {
for (start = 0; start < step; start++) {
stepInsertSort(array, count, start, step);
}
}
}
void strightSwapSort(int *array, int count) {
//稳定 O(n^2)
int i;
int j;
int tmp;
int hasSwap;
for (i = 0; i < count - 1; i++) {
for (j = 0, hasSwap = 0; j < count - 1 - i; j++) {
if (array[j] > array[j+1]) {
tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
hasSwap = 1;
}
}
if (!hasSwap) {
return;
}
}
}
void strightSelectSort(int *array, int count) {
//不稳定 O(n^2)
int index;
int minIndex;
int i;
int tmp;
for (index = 0; index < count - 1; index++) {
for (minIndex = i = index; i < count; i++) {
if (array[minIndex] > array[i]) {
minIndex = i;
}
}
if (minIndex != index) {
tmp = array[minIndex];
array[minIndex] = array[index];
array[index] = tmp;
}
}
}
void strightInsertSort(int *array, int count) {
//稳定 O(n^2)
int index;
int i;
int j;
int tmp;
for (index = 1; index < count; index++) {
tmp = array[index];
for (i = 0; i < index && tmp >= array[i]; i++) {
}
for (j = index; j > i; j--) {
array[j] = array[j-1];
}
array[i] = tmp;
}
}
void show(int *array, int count) {
int i;
for (i = 0; i < count; i++) {
printf("%d ",array[i]);
}
printf("\n");
}
int * randArray(int count, int max, int min) {
int i;
int delta = max - min;
int *result = NULL;
if(count <= 0) {
return NULL;
}
result = (int *)calloc(sizeof(int), count);
srand(time(NULL));
for (i = 0; i < count; i++) {
result[i] = rand() % delta + min;
}
return result;
}
最后定义一个指向函数的指针类型的数组
typedef void (*SORT)(int *, int);
const SORT sortFun[] = {
quickSort, //快速排序
heapSort, //堆排序
shellSort, //希尔排序
strightSelectSort, //直接选择排序
strightSwapSort, //直接交换排序
strightInsertSort, //直接插入排序
};