c语言实现各种排序,c语言实现各种排序算法

以下是我用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 

#include 

#endif

#ifndef _DEBUG_H

#define _DEBUG_H

#include 

#include 

#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 

{

L.r[i].Key = 0;

L.r[i].Other = info;

}

}

void Random(List &L)

{

srand((unsigned)time(NULL));

int i=1;

while (i 

{

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 

{

if (L.r[i].Key == param)

return true;

}

return false;

}

bool LessThan(LESSTNAN LParam, LESSTNAN WParam)

{

if (LParam 

return true;

else

return false;

}

void ListTranverse(List &L, Function p_function)

{

int i;

for (i = 1; i 

{

(*p_function)(L.r[i]);

}

}

void InsertSort(List &L)

{

int i, j;

for (i = 2; 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 

{

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 

{

ShellInsert(L, Data[i]);

}

}

void BubbleSort(List &L)

{

int i, j;

Type temp;

for (i = 1; i 

{

for (j = 1; 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 

{

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 

{

while (Low = PivotKey)

{

High--;

}

temp = L.r[Low];

L.r[Low] = L.r[High];

L.r[High] = temp;

while (Low 

{

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 

{

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 

{

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 

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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值