排序

#include <iostream>

using namespace std;

void BubbleSort(int *arr, int Count)			//冒泡排序
{
	if (arr == NULL || Count <= 0)
	{
		return;
	}
	int i, j;
	for (i = 0; i < Count; i++)
	{
		int temp = 0;
		for (j = 0; j < Count - i - 1; j++)
		{
			if (arr[j + 1] < arr[j])
			{
				temp       = arr[j];
				arr[j]     = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
	return;
}

void SelectSort(int *arr, int Count)			//选择排序
{
	int iTemp;
	int iPos;
	if (arr == NULL && Count <= 1)
	{
		return;
	}
	for (int i = 0; i < Count - 1; i++)
	{
		iTemp = arr[i];
		iPos = i;
		for (int j = i + 1; j < Count; j++)
		{
			if (iTemp > arr[j])
			{
				iTemp = arr[j];
				iPos = j;
			}
		}
		arr[iPos] = arr[i];
		arr[i] = iTemp;
	} 
}

void InsertSort(int *arr, int Count)				//直接插入排序
{
	int iTemp;
	int iPos;
	if (arr == NULL && Count <= 1)
	{
		return;
	}
	for (int i = 1; i < Count; i++)
	{
		iTemp = arr[i];
		iPos = i - 1;
		while ((iPos >= 0) && (iTemp < arr[iPos]))
		{
			arr[iPos + 1] = arr[iPos];
			iPos--;
		}
		arr[iPos + 1] = iTemp;
	}
}

void CockTail(int *arr, int size)			//鸡尾酒排序(改进的冒泡排序,也叫双向冒泡排序)
{
	int tail = size - 1;
	int i, j;
	for (i = 0; i < tail;)
	{
		for (j = tail; j > i; --j)
		{
			if (arr[j] < arr[j - 1])
			{
				int temp = arr[j];
				arr[j] = arr[j - 1];
				arr[j - 1] = temp;
			}
		}
		++i;
		for (j = i; j < tail; ++j)
		{
			if (arr[j] > arr[j + 1])
			{
				int temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
		tail--;
	}
}


void MergeArray(int a[], int first, int mid, int last)
{
	int begin1 = first, begin2 = mid + 1;
	int end1 = mid,   end2 = last;
	int k = 0;
	int *temp = new int[last - first + 1];

	while (begin1 <= end1 && begin2 <= end2)
	{
		if (a[begin1] <= a[begin2])
			temp[k++] = a[begin1++];
		else
			temp[k++] = a[begin2++];
	}

	while (begin1 <= end1)
		temp[k++] = a[begin1++];

	while (begin2 <= end2)
		temp[k++] = a[begin2++];

	for (int i = 0; i < k; i++)
		a[first + i] = temp[i];
	delete []temp;
}
void MergeSort(int a[], int first, int last)			//归并排序
{
	if (first < last)
	{
		int mid = (first + last) / 2;
		MergeSort(a, first, mid);    //左边有序
		MergeSort(a, mid + 1, last); //右边有序
		MergeArray(a, first, mid, last); //再将二个有序数列合并
	}
}

//Data swap function
void Swap(int &p,int &q)                           
{                                                       
	int temp = p;
	p = q;
	q  = temp;
} 

//Partition function
int Partition(int *arr,int nLow,int nHigh)                 
{                                                  
	int iTemp = arr[nHigh];    
	int i = nLow, j = nLow - 1;   
	for(; i < nHigh; i++)
	{
		if (arr[i] <= iTemp)
		{
			j++;
			if(i != j)
			{
				Swap(arr[i], arr[j]);
			}
		}
	}
	Swap(arr[j + 1], arr[nHigh]);
	return (j + 1);                                         
}
//Quick sort
void QuickSort(int *arr,int nLow,int nHigh)          //快速排序   
{                                                                                                        
	if(nLow < nHigh)                                         
	{                                                 
		int nIndex = Partition(arr , nLow, nHigh);                          
		QuickSort(arr , nLow, nIndex-1);                            
		QuickSort(arr , nIndex+1, nHigh);                            
	}                                                 
} 



void ShellSort(int arr[], int n, int gapMax)			//希尔排序
{  
	int gap;  
	for(gap = gapMax; gap > 0; gap--)  
	{  
		for(int i = 0; i < gap; i++)  
		{  
			for(int j = i + gap; j < n; j = j + gap)  
			{  
				//if(arr[j] < arr[j-gap])  
				{  
					int iTemp = arr[j];  
					int iPos = j - gap;  
					while(iPos >= 0 && arr[iPos] > iTemp)  
					{  
						arr[iPos + gap] = arr[iPos];  
						iPos = iPos - gap;  
					}  
					arr[iPos + gap] = iTemp;  
				}  
			}  
		}  
	}  
}

/*
输入:数组A,堆的长度hLen,以及需要调整的节点i功能:调堆
*/

void AdjustHeap(int A[], int hLen, int i)
{
	int left = 2 * i;  //节点i的左孩子
	int right = (2 * i) + 1; //节点i的右孩子节点
	int largest = i;
	int temp;
	while (left < hLen || right < hLen)
	{
		if (left < hLen && A[largest] < A[left])
		{
			largest = left;
		}

		if (right < hLen && A[largest] < A[right])
		{
			largest = right;
		}

		if (i != largest)   //如果最大值不是父节点
		{
			temp = A[largest]; //交换父节点和和拥有最大值的子节点交换
			A[largest] = A[i];
			A[i] = temp;

			i = largest;         //新的父节点,以备迭代调堆
			left = 2 * i;  //新的子节点
			right = (2 * i) + 1;
		}
		else
		{
			break;
		}
	}
}

/*
输入:数组A,堆的大小hLen
功能:建堆
*/
void BuildHeap(int A[], int hLen)
{
	int i;
	int begin = hLen / 2 - 1;  //最后一个非叶子节点
	for (i = begin; i >= 0; i--)
	{
		AdjustHeap(A, hLen, i);  
	}
}

/*
输入:数组A,待排序数组的大小aLen
功能:堆排序
*/
void HeapSort(int A[], int aLen)					//堆排序
{
	int hLen = aLen;
	int iTemp;
	BuildHeap(A, hLen);      //建堆

	while (hLen > 1)
	{
		iTemp = A[hLen - 1];    //交换堆的第一个元素和堆的最后一个元素
		A[hLen - 1] = A[0];	
		A[0] = iTemp;
		hLen--;        //堆的大小减一
		AdjustHeap(A, hLen, 0);  //调堆
	}
}


int main(int argc, char *argv[])
{
	int arr[10] = {12, 25, 9, 39, 18, 0, 31, 16, 68, 46};
	int i = 0;
	cout << "Input the numbers:" << endl;
	//for (i = 0; i < 10; i++)
	//{
	//	cin >> arr[i];
	//}
	cout << "Before:" << endl;
	for (i = 0; i < 10; i++)
	{
		cout << arr[i] << endl;
	}
	//BubbleSort(arr, 10);
	//cout << "BubbleSort After:" << endl;
	//for (i = 0; i < 10; i++)
	//{
	//	cout << arr[i] << endl;
	//}
	//SelectSort(arr, 10);
	//cout << "SelectSort After:" << endl;
	//for (i = 0; i < 10; i++)
	//{
	//	cout << arr[i] << endl;
	//}
	//InsertSort(arr, 10);
	//cout << "InsertSort After:" << endl;
	//for (i = 0; i < 10; i++)
	//{
	//	cout << arr[i] << endl;
	//}

	//CockTail(arr, 10);
	//cout << "CockTail After:" << endl;
	//for (i = 0; i < 10; i++)
	//{
	//	cout << arr[i] << endl;
	//}

	//MergeSort(arr, 0, 9);
	//cout << "MergeSort After:" << endl;
	//for (i = 0; i < 10; i++)
	//{
	//	cout << arr[i] << endl;
	//}

	//QuickSort(arr, 0, 9);
	//cout << "QuickSort After:" << endl;
	//for (i = 0; i < 10; i++)
	//{
	//	cout << arr[i] << endl;
	//}

	//ShellSort(arr, 10, 3);
	//cout << "ShellSort After:" << endl;
	//for (i = 0; i < 10; i++)
	//{
	//	cout << arr[i] << endl;
	//}

	//HeapSort(arr, 10);
	//cout << "HeapSort After:" << endl;
	//for (i = 0; i < 10; i++)
	//{
	//	cout << arr[i] << endl;
	//}


	int s;
	cin >> s;
	//getchar();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值