各种排序算法
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef int ElemType;
void InsertSort(ElemType A[], int n)
{
int i, j;
for(i = 2; i <= n; i++)
{
if(A[i] < A[i-1])
{
A[0] = A[i];
for(j = i-1; A[0] < A[j]; j--)
A[j+1] = A[j];
A[j+1] = A[0];
}
}
}
void InsertBSort(ElemType A[], int n)
{
int i, j, low, high, mid;
for(i = 2; i < n; i++)
{
A[0] = A[i];
low = 1;
high = i-1;
while(low <= high)
{
mid = (low + high) / 2;
if(A[0] < A[mid])
mid = high-1;
else
low = mid+1;
}
for(j = i-1; j >= high+1; j--)
A[j+1] = A[j];
A[high+1] = A[0];
}
}
void BubbleSort(ElemType A[], int n)
{
int i, j, flag,temp;
for(i = 0; i < n-1; i++)
flag = false;
for(j = n-1; j > i; j--)
{
if(A[j] < A[j-1])
{
temp = A[j];
A[j] = A[j-1];
A[j-1] = temp;
flag = true;
}
if(flag == false)
return;
}
}
int Partition(ElemType A[], int low, int high)
{
ElemType pivot = A[low];
while(low < high)
{
while(low < high && A[high] >= pivot)
--high;
A[low] = A[high];
while(low < high && A[low] <= pivot)
++low;
A[high] = A[low];
}
A[low] = pivot;
return low;
}
int QuickSort(ElemType A[], int low, int high)
{
if(low < high)
{
int pivotpos = Partition(A, low, high);
QuickSort(A, low, pivotpos-1);
QuickSort(A, pivotpos+1, high);
}
}
void SelectSort(ElemType A[], int n)
{
int i, j, min, temp;
for(i = 0; i < n-1; i++)
{
min = i;
for(j = i+1; j < n; j++)
{
if(A[j] < A[i])
min = j;
if(min != i)
{
temp = A[min];
A[min] = A[i];
A[i] = A[min];
}
}
}
}
void AdjustDown(ElemType A[], int k, int len)
{
int i;
for(i = 2*k; i <= len; i*=2)
{
if(i < len && A[i] < A[i+1])
i++;
if(A[0] > A[i])
break;
else
{
A[k] = A[i];
k = i;
}
}
A[0] = A[k];
}
void BuildMaxHeap(ElemType A[], int len)
{
int i;
for(int i = len/2; i > 0; i--)
AdjustDown(A, i, len);
}
void DBubbleSort(ElemType A[], int n)
{
int low = 0, high = n-1;
bool flag = true;
while(low < high && flag)
{
flag = false;
for(int i = low; i < high; i++)
{
if(A[i] > A[i+1]){
swap(A[i], A[i+1]);
flag = true;
}
}
high--;
for(int j = high; j > low; j--)
{
if(A[j] < A[j-1])
{
swap(A[j], A[j-1]);
flag = true;
}
}
low++;
}
}
int main()
{
return 0;
}