六种常见算法实例

不想废话,直接上代码,代码都是调试过的。

实现的算法有:冒泡,直接选择,直接插入,归并,二叉堆,快速排序这六种。

对于后三种的理解方法,请百度这个系列的文章“白话经典算法系列 ”,个人感觉作者讲的特别不错!

下面贴代码

#include <stdio.h>
#include <conio.h>
//冒泡排序
void BubbleSort(int arry[], int n)
{
	int i, j, k;
	for (i = 0; i < n; i++)
	{
		k = 0;
		for (j = 0; j < n - 1; j++)
		{
			if (arry[j] > arry[j + 1])
			{
				int t = arry[j];
				arry[j] = arry[j+1];
				arry[j+1] = t;
				k = 1;
			}
		}
		if (k == 0)
			break;
	}
}
//插入排序
void InsertSort(int arry[], int n)
{
	//{ 9,23,1,8,5,13,7,12,0,4 };
	int i, j, k;
	for (i = 1; i < n; i++)
	{
		k = arry[i];
		for (j = i - 1; j >= 0&& arry[j] >=k; j--)
		{
			arry[j + 1] = arry[j];
		}
		arry[j + 1] = k;
	}
}
//选择排序
void SelectSort(int arry[], int n)
{
	int i, j, k;
	for (int i = 0; i < n; i++)
	{
		k = i;
		for (j = i+1; j < n; j++)
		{
			if (arry[k]> arry[j])
			{
				k = j;
			}
		}
		int t = arry[i];
		arry[i] = arry[k];
		arry[k] = t;
	}
}
//堆排序
//最小堆向上向下排列函数
void MakeMinHeapUp(int arry[], int i)
{
	int j = (i - 1) / 2;
	int temp = arry[i];
	while (j >= 0)
	{
		if (arry[j] < arry[i])
			break;
		arry[i] = arry[j];
		i = j;
		j = (j - 1) / 2;
	}
	arry[i] = j;
}
void MakeMinHeapDown(int arry[],int n,int i)
{
	int j = i * 2 + 1;
	int temp = arry[i];
	while (j < n)
	{
		if (j+1<n&&arry[j + 1] < arry[j])
			j++;
		if (arry[i] < arry[j])
			break;
		arry[i] = arry[j];
		i = j;
		j = 2 * j + 1;
	}
	arry[i] = temp;
}
//最大堆向上向下排列函数
void MakeMaxHeapUp(int arry[], int i)
{
	int j = (i - 1) / 2;
	int temp = arry[i];
	while (j >= 0)
	{
		if (arry[j] > arry[i])
			break;
		arry[i] = arry[j];
		i = j;
		j = (j - 1) / 2;
	}
	arry[i] = j;
}
void MakeMaxHeapDown(int arry[], int n, int i)
{
	int j = i * 2 + 1;
	int temp = arry[i];
	while (j < n)
	{
		if (j + 1<n&&arry[j + 1] > arry[j])
			j++;
		if (arry[i] > arry[j])
			break;
		arry[i] = arry[j];
		i = j;
		j = 2 * j + 1;
	}
	arry[i] = temp;
}
//生成最小堆,排序后为降序
void MakeMinHeap(int arry[], int n)
{
	for (int i = n / 2 - 1; i >= 0; i--)
	{
		MakeMinHeapDown(arry, n, i);
	}
}
//生成最大堆,排序后为升序
void MakeMaxHeap(int arry[], int n)
{
	for (int i = n / 2 - 1; i >= 0; i--)
	{
		MakeMaxHeapDown(arry, n, i);
	}
}
void HeapSortMain(int arry[], int n)
{
	int temp;
	for (int i = n - 1; i >= 1; i--)
	{
		temp = arry[i];
		arry[i] = arry[0];
		arry[0] = temp;
		//MakeMinHeap(arry, i);
		MakeMaxHeap(arry, i);
	}
}
void HeapSort(int arry[],int n)
{
	//MakeMinHeap(arry, n);
	MakeMaxHeap(arry, n);
	HeapSortMain(arry, n);
}


//快速排序
int GetIndex(int arry[], int l, int r)
{
	int i = l, j = r;
	int k = arry[l];
	while (i < j)
	{
		while (i<j&&k<arry[j])
		{
			j--;
		}
		if (i < j)
		{
			arry[i] = arry[j];
			i++;
		}
		while (i<j&&k>arry[i])
		{
			i++;
		}
		if (i < j)
		{
			arry[j] = arry[i];
			j--;
		}
	}
	arry[i] = k;
	return i;
}
void QuickSortMain(int arry[], int l, int r)
{
	if (l < r)
	{
		int mid = GetIndex(arry, l, r);
		QuickSortMain(arry, l, mid-1);
		QuickSortMain(arry, mid + 1, r);
	}
}
void QuickSort(int arry[], int n)
{
	QuickSortMain(arry, 0, n - 1);
}

//归并排序
void MergeArry(int arry[], int first, int mid, int last, int temp[])
{
	int i = first, j = mid + 1;
	int k = 0;
	while (i <= mid&&j <= last)
	{
		if (arry[i]>arry[j])
		{
			temp[k++] = arry[j++];
		}
		else
		{
			temp[k++] = arry[i++];
		}
	}
	while (i <= mid)
	{
		temp[k++] = arry[i++];
	}
	while (j <= last)
	{
		temp[k++] = arry[j++];
	}
	for (i = 0; i < k; i++)
	{
		arry[first + i] = temp[i];
	}
}
void MergeSortMain(int arry[],int first,int last,int temp[])
{
	if (first < last)
	{
		int mid = (first + last) / 2;
		MergeSortMain(arry, first, mid,temp);
		MergeSortMain(arry, mid + 1, last,temp);
		MergeArry(arry, first, mid, last, temp);
	}
}
void MergeSort(int arry[],int n)
{
	int *p = new int[n];
	if (p == NULL)
	{
		return;
	}
	MergeSortMain(arry, 0, n - 1, p); 
	delete[] p;
}

int main()
{
	//int arry[10] = { 9,8,7,6,5,4,3,2,1,0 };
	int arry[10] = { 9,23,1,8,5,13,7,12,0,4 };
	//BubbleSort(arry, 10);
	//InsertSort(arry, 10);
	//SelectSort(arry, 10);
	//ShellSort(arry, 10);
	//QuickSort(arry, 10);
	//MergeSort(arry, 10);
	//HeapSort(arry, 10);
	int i = 0;
	for (; i < 10; i++)
	{
		printf("%d ", *(arry + i));
	}
	getch();
}

  

posted @ 2015-09-30 23:03  DemoApp  阅读(1608)  评论(0编辑  收藏  举报