C语言-内部排序算法





#include 
//直接插入排序
void InsertSort(int a[], int n)
{
	int i, j;
	int temp;
	for(i = 1; i <= n-1; ++i)
	{
		temp = a[i];
		for(j = i-1; j >= 0 && a[j] > temp; --j)
		{
			a[j+1] = a[j];
		}
		a[j+1] = temp;
	}
}
//折半插入排序
void BinaryInsertSort(int a[], int n)
{
	int i, j, low, heigh, mid;
	int temp;
	for(i = 1; i <= n-1; ++i)
	{
		temp = a[i];
		low = 0; 
		heigh = i-1;
		while(low <= heigh)
		{
			mid = (low + heigh) / 2;
			if(a[mid] > temp)
			{
				heigh = mid - 1;
			}
			else
			{
				low = mid + 1;
			}
		}
		for(j = i-1; j >= heigh+1; --j)
		{
			a[j+1] = a[j];
		}
		a[heigh + 1] = temp;
	}
}
//希尔排序
void ShellSort(int a[], int n)
{
	int i, j, dk;
	int temp;
	for(dk = n/2; dk > 0; dk /= 2)
	{
		for(i = dk; i <= n-1; ++i)
		{
			temp = a[i];
			for(j = i-dk; j >= 0 && a[j] > temp; j -= dk)
			{
				a[j+dk] = a[j];	
			}
			a[j+dk] = temp;
		}
	}
}
//冒泡排序
void BubbleSort(int a[], int n)
{
	int i, j;
	int temp;
	for(i = 1; i <= n-1; ++i)
	{
		bool flag = false;
		for(j = 0; j <= n-1-i; ++j)
		{
			if(a[j] > a[j+1])
			{
				flag = true;
				temp = a[j];
				a[j] = a[j+1];
				a[j+1] = temp;
			}
		}
		if(flag == false)
			return;
	}
}
//快速排序
int Partition(int a[], int low, int heigh)
{
	int posTemp = a[low];
	while(low < heigh)
	{
		while(low < heigh && a[heigh] >= posTemp)
		{
			--heigh;
		}
		a[low] = a[heigh];
		while(low < heigh && a[low] <= posTemp)
		{
			++low;
		}
		a[heigh] = a[low];
	}
	a[low] = posTemp;
	return low;
}
void QuickSort(int a[], int low, int heigh)
{
	if(low < heigh)
	{
		int Pos = Partition(a,low,heigh);
		QuickSort(a,low,Pos-1);
		QuickSort(a,Pos+1,heigh);
	}
}
//简单选择排序
void SelectSort(int a[], int n)
{
	int i, j, temp;
	for(i = 0; i < n-1; ++i)
	{
		int pos = i;
		for(j = i+1; j < n; ++j)
		{
			if(a[j] < a[pos])
			{
				pos = j;
			}	
		}
		if(pos != i)
		{
			temp = a[i];
			a[i] = a[pos];
			a[pos] = temp;
		}
	}
}
//堆排序
void AdjustDown(int a[], int i, int n)
{
	int largest;
	int temp = a[i];
	for(largest = 2*i+1; largest < n; largest = 2*largest+1)
	{
		if(largest != n-1 && a[largest+1] > a[largest])
		{
			largest++;
		}
		if(temp < a[largest])
		{
			a[i] = a[largest];
			i = largest;
		}
		else
		{
			break;
		}
	}
	a[i] = temp;
}
void BuildMaxHeap(int a[], int n)
{
	for(int i = n/2-1; i >= 0; --i)
	{
		AdjustDown(a,i,n);
	}
}
void HeapSort(int a[], int n)
{
	int i, temp;
	BuildMaxHeap(a,n);
	for(i = n-1; i > 0; --i)
	{
		temp = a[0];
		a[0] = a[i];
		a[i] = temp;
		AdjustDown(a,0,i);
	}
}
//二路归并排序
int* b = new int[20];
void Merge(int a[], int low, int heigh, int mid)
{
	int i, j, k;
	for(k = low; k <= heigh; ++k)
	{
		b[k] = a[k];
	}
	for(i = low,j = mid+1,k = i; i <= mid && j <= heigh; ++k)
	{
		if(b[i] <= b[j])
		{
			a[k] = b[i++];
		}
		else
		{
			a[k] = b[j++];
		}
	}
	while(i <= mid)
	{
		a[k++] = b[i++];
	}
	while(j <= heigh)
	{
		a[k++] = b[j++];
	}
}
void MergeSort(int a[], int low, int heigh)
{
	if(low < heigh)
	{
		int mid = (low + heigh) / 2;
		MergeSort(a,low,mid);
		MergeSort(a,mid+1,heigh);
		Merge(a,low,heigh,mid);
	}
}
//打印输出
void Print(int a[], int n)
{
	for(int i=0; i

 
  

你可能感兴趣的:(数据结构和算法,直接插入排序,折半插入排序,希尔排序,快速排序,内部排序算法)