以下是我用c语言实现的各种排序算法
#pragma once; #define MAXSIZE 20000 typedef int KeyType; typedef char Infomation; typedef int LESSTNAN; typedef int ShellData[]; static const char info='0'; #ifndef _SDTLIB_H #define _STDLIB_H #include <stdlib.h> #include <time.h> #endif #ifndef _DEBUG_H #define _DEBUG_H #include <stdio.h> #include <iostream> #endif #ifndef _TYPE_H #define _TYPE_H typedef struct { KeyType Key; Infomation Other; }Type; #endif typedef void(*Function)(Type); #ifndef _LIST_H #define _LIST_H typedef struct { Type r[MAXSIZE + 1]; int Length; }List; #endif #pragma once; #include "stdafx.h" void InitList(List &L); void Random(List &L); bool LessThan(LESSTNAN LParam,LESSTNAN WParam); void InsertSort(List &L); void ShellSort(List &L,ShellData Data,int DataLength); void ShellInsert(List &L,int dk); void BubbleSort(List &L); void QuickSort(List &L, int Low, int High); int Partition(List &L, int Low, int High); void SelectSort(List &L); int SelectMinKey(List &L, int Start); void HeapSort(List &L); void HeapAdjust(List &L, int s, int m); void RadixSort(List &L); Type* MergeSort(List &L); void MergingSort(Type SR[], Type TR[], int s, int t); void Merge(Type SR[], Type TR[], int i, int m, int n); void ListTranverse(List &L,Function p_function); bool IsType(List &L,int Length, KeyType param); #include "SortList.h" void InitList(List &L) { int i; L.Length = 0; for (i = 0; i < MAXSIZE + 1; i++) { L.r[i].Key = 0; L.r[i].Other = info; } } void Random(List &L) { srand((unsigned)time(NULL)); int i=1; while (i < MAXSIZE + 1) { int temp = rand(); if (IsType(L, i, temp) == false) { L.r[i].Key = temp; i++; } } L.Length = MAXSIZE; } bool IsType(List &L,int Length, KeyType param) { int i; for (i = 1; i < Length+1; i++) { if (L.r[i].Key == param) return true; } return false; } bool LessThan(LESSTNAN LParam, LESSTNAN WParam) { if (LParam < WParam) return true; else return false; } void ListTranverse(List &L, Function p_function) { int i; for (i = 1; i < L.Length + 1; i++) { (*p_function)(L.r[i]); } } void InsertSort(List &L) { int i, j; for (i = 2; i < L.Length + 1; i++) { if (LessThan(L.r[i].Key, L.r[i - 1].Key) == true) { L.r[0] = L.r[i]; L.r[i] = L.r[i - 1]; for (j = i - 2; LessThan(L.r[0].Key, L.r[j].Key); j--) { L.r[j + 1] = L.r[j]; } L.r[j + 1] = L.r[0]; } } } void ShellInsert(List &L, int dk) { int i, j; for (i = dk + 1; i < L.Length + 1; i++) { if (LessThan(L.r[i].Key, L.r[i - dk].Key) == true) { L.r[0] = L.r[i]; for (j = i - dk; j>0 && LessThan(L.r[0].Key, L.r[j].Key); j -= dk) { L.r[j + dk] = L.r[j]; } L.r[j + dk] = L.r[0]; } } } void ShellSort(List &L, ShellData Data, int DataLength) { int i; for (i = 0; i < DataLength; i++) { ShellInsert(L, Data[i]); } } void BubbleSort(List &L) { int i, j; Type temp; for (i = 1; i < L.Length + 1; i++) { for (j = 1; j < L.Length+1-i; j++) { if (L.r[j].Key>L.r[j + 1].Key) { temp = L.r[j]; L.r[j] = L.r[j + 1]; L.r[j + 1] = temp; } } } } void QuickSort(List &L, int Low, int High) { if (Low < High) { int Piv = Partition(L, Low, High); QuickSort(L, Low, Piv - 1); QuickSort(L, Piv + 1, High); } } int Partition(List &L, int Low, int High) { KeyType PivotKey = L.r[Low].Key; Type temp; while (Low < High) { while (Low < High&&L.r[High].Key >= PivotKey) { High--; } temp = L.r[Low]; L.r[Low] = L.r[High]; L.r[High] = temp; while (Low < High&&L.r[Low].Key <= PivotKey) { Low++; } temp = L.r[Low]; L.r[Low] = L.r[High]; L.r[High] = temp; } return Low; } void SelectSort(List &L) { int i,j; Type temp; for (i = 1; i < L.Length + 1; i++) { j = SelectMinKey(L, i); if (i != j) { temp = L.r[i]; L.r[i] = L.r[j]; L.r[j] = temp; } } } int SelectMinKey(List &L, int Start) { KeyType temp=L.r[Start].Key; int Min=Start; while (Start < L.Length + 1) { if (temp>L.r[Start].Key) { temp = L.r[Start].Key; Min = Start; } Start++; } return Min; } void Merge(Type SR[], Type TR[], int i, int m, int n) { int k,j; for (j = m + 1, k = i; i <= m&&j <= n; k++) { if (LessThan(SR[i].Key, SR[j].Key) == true) TR[k] = SR[i++]; else TR[k] = SR[j++]; } while (i <= m) { TR[k] = SR[i]; i++; k++; } while (j <= n) { TR[k] = SR[j]; j++; k++; } } void MergingSort(Type SR[], Type TR[], int s, int t) { if (s == t) TR[s] = SR[s]; else { int m = (s + t) / 2; MergingSort(SR, TR, s, m); MergingSort(SR, TR, m + 1, t); Merge(SR, TR, s, m, t); } } Type* MergeSort(List &L) { Type * temp=new Type[MAXSIZE+1]; MergingSort(L.r, temp, 1, L.Length); return temp; } void HeapAdjust(List &L, int s, int m) { Type RC = L.r[s]; int j; for (j = 2 * s; j <= m; j *= 2) { if (j < m&&LessThan(L.r[j].Key, L.r[j + 1].Key) == true) j++; if (LessThan(RC.Key, L.r[j].Key) == false) break; L.r[s] = L.r[j]; s = j; } } void HeapSort(List &L) { int i; Type temp; for (i = L.Length / 2; i > 0; i--) { HeapAdjust(L, i, L.Length); } for (i = L.Length; i > 1; i--) { temp = L.r[1]; L.r[1] = L.r[i]; L.r[i] = temp; HeapAdjust(L, 1, i - 1); } }