【数据结构】各种排序算法实现

https://www.cnblogs.com/onepixel/articles/7674659.html

#include <stdio.h>

/*【交换排序1之冒泡排序】冒泡排序:两两比较相邻记录的关键字,如果反序则交换,直到没有反序记录为止*/
int bubbleSort1(int *array, int length) //最初级的非严格冒泡排序:易懂但是效率低
{
	int i = 0;
	int j = 0;
	int *p = NULL;

	p = array;

	for (i = 0; i < length;i++)
	{
		for (j = i+1; j < length; j++)
		{
			//最小的放在在前面
			if (p[i] > p[j])
			{
				int temp = p[j];
				p[j] = p[i];
				p[i] = temp;
			}
		}
	}

	for (i = 0; i < length; i++)
	{
		printf("%-5d",p[i]);
	}
	printf("\n");

	return 1;
}

int bubbleSort(int *array, int length)
{
	int i = 0;
	int j = 0;
	int *p = NULL;

	p = array;

	for (i = 0; i < length; i++)
	{
#if 0
		for (j = 0; j < length-i-1; j++)
		{
			//两两比较,最大的放最后
			if (p[j] > p[j+1])
			{
				int temp = p[j+1];
				p[j+1] = p[j];
				p[j] = temp;
			}
		}
#else
		for (j = length-1; j > i; j--)
		{
			//两两比较,最小的放最前
			if (p[j-1] > p[j])
			{
				int temp = p[j];
				p[j] = p[j-1];
				p[j-1] = temp;
			}
		}

#endif
	}

	for (i = 0; i < length; i++)
	{
		printf("%-5d", p[i]);
	}
	printf("\n");
}

/*【交换排序2之选择排序】
选择排序(Selection-sort)是一种简单直观的排序算法。它的工作原理:
首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,
然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。
以此类推,直到所有元素均排序完毕。
*/
//不是每次交互数据,而是记录下标,然后进行交换,因此最大特点是交换数据最少
int selectionSort(int *array,int length)
{
	int i = 0;
	int j = 0;
	int minIndex = 0;

	if (NULL == array || length <= 0)
	{
		return -1;
	}

	for (i = 0; i < length; i++)
	{
		minIndex = i;
		for (j = i+1; j < length; j++)
		{
			if (array[minIndex] > array[j])
			{
				minIndex = j;
			}
		}

		if (i != minIndex)
		{
			int temp = array[minIndex];
			array[minIndex] = array[i];
			array[i] = temp;
		}

	}

	for (i = 0; i < length; i++)
	{
		printf("%-5d", array[i]);
	}
	printf("\n");

	return 0;
}

/*【插入排序1之插入排序*********************************************】
插入排序(Insertion-Sort)的算法描述是一种简单直观的排序算法。
它的工作原理是通过构建有序序列,对于未排序数据,
在已排序序列中从后向前扫描,找到相应位置并插入。
*/
int InsertionSort(int *array, int length)
{
	int i = 0;
	int j = 0;

	if (NULL == array || length <= 0)
	{
		return -1;
	}

	for (i = 0; i < length; i++)
	{
		int temp = array[i];  //取得待插入的数据
		for (j = i; j > 0 && temp < array[j - 1]; j--)
		{
			array[j] = array[j-1]; //如果前一位小,就向后移一位
		}
		array[j] = temp;		//最后插入正确的位置
	}

	for (i = 0; i < length; i++)
	{
		printf("%-5d", array[i]);
	}
	printf("\n");

	return 0;
}

/*【插入排序2之希尔排序】
希尔排序(Shell Sort)分块排序第一个突破O(n2)的排序算法,是简单插入排序的改进版。
它与插入排序的不同之处在于,它会优先比较距离较远的元素。希尔排序又叫缩小增量排序。
4.1 算法描述
先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,具体算法描述:
选择一个增量序列t1,t2,…,tk,其中ti>tj,tk=1;
按增量序列个数k,对序列进行k 趟排序;
每趟排序,根据对应的增量ti,将待排序列分割成若干长度为m 的子序列,分别对各子表进行直接插入排序。
仅增量因子为1 时,整个序列作为一个表来处理,表长度即为整个序列的长度。
希尔排序的核心在于间隔序列的设定。既可以提前设定好间隔序列,也可以动态的定义间隔序列。
*/
int ShellSort(int *array, int length)
{
	int i = 0;
	int j = 0;
	int increment = 0;

	if (NULL == array || length <= 0)
	{
		return -1;
	}

	increment = length;
	do
	{
		increment = increment / 3 + 1;
		for (i = increment; i < length; i++)
		{
			int temp = array[i];  //取得待插入的数据
			for (j = i; j > 0 && temp < array[j - 1]; j--)
			{
				array[j] = array[j - 1]; //如果前一位小,就向后移一位
			}
			array[j] = temp;		//最后插入正确的位置
		}
	} while (increment > 1);

	for (i = 0; i < length; i++)
	{
		printf("%-5d", array[i]);
	}
	printf("\n");

	return 0;
}


/*
归并排序(Merge Sort)
归并排序是建立在归并操作上的一种有效的排序算法。
该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。
将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。
若将两个有序表合并成一个有序表,称为2-路归并。
5.1 算法描述
把长度为n的输入序列分成两个长度为n/2的子序列;
对这两个子序列分别采用归并排序;
将两个排序好的子序列合并成一个最终的排序序列。
*/








int main()
{
	int str[] = {3, 2, 5, 8, 9, 1, -8, 100};

	//bubbleSort(str,sizeof(str)/sizeof(str[0]));
	//selectionSort(str, sizeof(str) / sizeof(str[0]));
	//InsertionSort(str, sizeof(str) / sizeof(str[0]));
	ShellSort(str, sizeof(str) / sizeof(str[0]));

	return 0;
}

猜你喜欢

转载自blog.csdn.net/mashang123456789/article/details/81587477