《C语言》冒泡排序、快速排序、选择排序、堆排序、桶排序、插入排序。

冒泡排序、快速排序、选择排序、堆排序、桶排序、插入排序。

Main.c

#include <time.h>
#include "MySort.h"

#define N 10

typedef int Int_D;

void main()
{

	Int_D Array[N] = { 0};

	srand((unsigned int)time(NULL));

	for (int i = 0; i < N; i++)
	{
		Array[i] = rand() % 100;
	}


	Show(Array,N);
    //FindMax(Array, N);
	//冒泡排序
	//BubbleSort(Array, N);
	//快速排序
	//QuickSort(Array, 0, N-1);
	//选择排序
	//SelectSort(Array, N);
	//堆排序
	//HeapSort(Array, N);
	//桶排序
	//BucketSort(Array, N);
	//插入排序
	InsertSort(Array, N);
	Show(Array, N);

	system("pause");
}

MySort.h

#pragma once

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
	typedef int D_Type;

    //显示排序状态
	void Show(D_Type* Array, int Count);
	//数据交换
	void Swap(D_Type* a, D_Type* b);
	//让最大值登顶(辅助堆排序)
	void FindMax(D_Type* Array, int Count);

	//冒泡排序
	void BubbleSort(D_Type* Array, int Count);
	//快速排序
	void QuickSort(D_Type* Array, int Left,int Right);
	//选择排序
	void SelectSort(D_Type* Array, int Count);
	//堆排序
	void HeapSort(D_Type* Array, int Count);
	//桶排序
	void BucketSort(D_Type* Array, int Count); 
	//插入排序
	void InsertSort(D_Type* Array, int Count);

#ifdef __cplusplus
}

#endif

MySort.c

#include "MySort.h"

//显示排序状态
void Show(D_Type* Array, int Count)
{
	for (int i = 0; i < Count; i++)
	{
		printf("%5d", Array[i]);
	}
	puts("");
}

//数据交换
void Swap(D_Type* a, D_Type* b)
{
	D_Type Temp = *a;
	*a = *b;
	*b = Temp;
}

//让最大值登顶(辅助堆排序)
void FindMax(D_Type* Array, int Count)
{
	for (int i = Count - 1; i > 0; i--)
	{
		int Parent = i / 2;
		int Child = i + 1;

		if (Array[Child] < Array[Child - 1])
		{
			Child--;
		}

		if (Array[Child]>Array[Parent])
		{
			Swap(&Array[Child], &Array[Parent]);
		}
	}
}

//冒泡排序
void BubbleSort(D_Type* Array, int Count)
{
	//因为只剩下最后一个不需要排序,所以“i < Count - 1”
	for (int i = 0; i < Count - 1; i++)
	{
		//因为每冒泡一次就有一个最大值沉底,所以“j < Count - 1 - i”
		for (int j=0; j < Count - 1 - i; j++)
		{
			if (Array[j] < Array[j+1])
			{
				//数据交换
				Swap(&Array[j], &Array[j + 1]);
			}
		}
	}
}

//快速排序
void QuickSort(D_Type* Array, int Left, int Right)
{
	if (Left < Right)
	{
		int i = Left;
		for (int j = i + 1; j <= Right;j++)
		{
			
			if (Array[Left] < Array[j])
			{
				i++;
				Swap(&Array[i], &Array[j]);
			}
		}
		//分界,保证左边的数据小于“Array[Left]”,右边的大于“Array[Left]”
		Swap(&Array[i], &Array[Left]); 
		QuickSort(Array, Left, i - 1);
		QuickSort(Array, i + 1, Right);
	}
}

//选择排序
void SelectSort(D_Type* Array, int Count)
{
	int Max = 0;
	for (int i = 0; i < Count-1; i++)
	{
		//假设最大值的下标是“i”
		Max = i;
		for (int j = i+1; j < Count; j++)
		{
			
			if (Array[Max] < Array[j])
			{
				//保存最大值得下标
				Max = j;
			}
		}
		//“Max”不等于“i”,表示“Max” 保存了另一个最大值
		if (Max != i)
		{
			//数据交换
			Swap(&Array[i],&Array[Max]);
		}
	}
		
}

//堆排序
void HeapSort(D_Type* Array, int Count)
{

	//每次选出一个最大值
	for (int i = 0; i < Count; i++)
	{
		FindMax(Array + i, Count - i);
	}
}

//桶排序
void BucketSort(D_Type* Array, int Count)
{
	//先找出一个最大值
	FindMax(Array, Count);

	//假如数组中最大值是 “N” 那么一定不可能出现“N+1”的值
	D_Type MaxVal = Array[0] + 1;
	
	//分配内存(桶)
	D_Type* Bucket = (D_Type*)malloc(sizeof(D_Type)*MaxVal);
	//内存清空
	memset(Bucket, 0, sizeof(D_Type)*MaxVal);

	//将默认值设置为 “N+1”
	for (int i = 0; i < MaxVal; i++)
	{
		Bucket[i] = MaxVal;
	}

	//数据入桶
	for (int i = 0; i < Count; i++)
	{
		Bucket[Array[i]] = Array[i];
	}
	
	//数据出桶
	int Num = 0;
	for (int i = 0; i < MaxVal; i++)
	{
		if (Bucket[i] != MaxVal)
		{
			Array[Num] = Bucket[i];
			Num++;
		}
	}
	//释放内存
	free(Bucket);
	Bucket=NULL;
}

//插入排序
void InsertSort(D_Type* Array, int Count)
{

	for (int i = 1; i < Count; i++)
	{
		D_Type Temp = Array[i];
		int j = i;
		while (j>0 && Array[j - 1] > Temp)
		{
			Array[j] = Array[j - 1];
			j--;
		}
		Array[j] = Temp;
	}
}

猜你喜欢

转载自blog.csdn.net/baidu_41905806/article/details/84727284