/*
这两个算法是比较经典的排序算法吧,闲来无事,敲了下实现代码
*/
#include <iostream>
#include <time.h>
#include <assert.h>
//quick sort
int Partition(int arr[], int start, int end);
void QuickSort(int arr[], int start, int end);
//heap sort
#define HEAPPARENT(i) ( i/2-1 )
#define HEAPLEFT(i) ( i*2+1 )
#define HEAPRIGHT(i) ( i*2+2 )
void HeapBuild(int arr[], int start, int end); //create heap
void HeapVerify(int arr[], int end, int pos); //keep heap
void HeapSort(int arr[], int start, int end); //heap sort
int Partition(int arr[], int start, int end)
{
int pos = start;
int key = arr[pos];
for ( int i=pos; i<=end; ++i )
{
if ( arr[i] < key )
{
arr[pos] = arr[i];
pos ++;
arr[i] = arr[pos];
}
}
arr[pos] =key;
return pos;
}
void QuickSort(int arr[], int start, int end)
{
if ( start < end )
{
int pos = Partition(arr, start, end);
QuickSort(arr, start, pos-1);
QuickSort(arr, pos+1, end);
}
}
void RandomProduce(int arr[], int size, int arrbk[], int arrsrc[])
{
for ( int i=0; i<size; ++i )
{
arr[i] = rand();
arrbk[i] = arr[i];
arrsrc[i] = arr[i];
}
}
int qcompare(const void *op1, const void *op2)
{
return *(int *)op1 - *(int *)op2;
}
bool ArrCompare(int arrsrc[], int arrquick[], int arrheap[], int size)
{
bool ret = true;
for ( int i=0; i<size; ++i )
{
if ( arrsrc[i] != arrquick[i] || arrsrc[i] != arrheap[i] )
{
ret = false;
break;
}
}
return ret;
}
void ArrPrint(int arr[], int size)
{
for ( int i=0; i<size; ++i )
{
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
void HeapBuild(int arr[], int start, int end)
{
int large = 0;
int left = 0;
int right = 0;
for ( int pos =(end-start+1)/2-1; pos>=start; --pos )
{
large = pos;
left = HEAPLEFT(pos);
right = HEAPRIGHT(pos);
if ( left <= end && arr[pos] < arr[left] )
{
large = left;
}
if ( right <= end && arr[large] < arr[right] )
{
large = right;
}
if ( large != pos )
{
HeapVerify(arr, end, pos);
}
}
}
void HeapVerify(int arr[], int end, int pos)
{
if ( pos < end )
{
int left = HEAPLEFT(pos);
int right = HEAPRIGHT(pos);
int large = pos;
if ( left <= end && arr[pos] < arr[left] )
large = left;
if ( right <= end && arr[large] < arr[right] )
large = right;
if ( large != pos )
{
int temp = arr[pos];
arr[pos] = arr[large];
arr[large] = temp;
HeapVerify(arr, end, large);
}
}
}
void HeapSort(int arr[], int start, int end)
{
HeapBuild(arr, start, end);
for ( int pos=end; pos>=start; --pos )
{
int temp = arr[end];
arr[end] = arr[start];
arr[start] = temp;
end --;
HeapVerify(arr, end, start);
}
}
int main(int argc, char* argv[])
{
int *arrsrc;
int *arrquick;
int *arrheap;
int times = 0;
int size = 0;
srand(time(NULL));
std::cout << "Please enter test times and size : ";
while ( std::cin>>times>>size )
{
int *block = (int *)malloc(3*size*sizeof(int));
assert(NULL != block);
arrsrc = block;
arrquick = block+size;
arrheap = block+2*size;
while ( times-- > 0 )
{
RandomProduce(arrsrc, size, arrquick, arrheap);
qsort(arrsrc, size, sizeof(int), qcompare);
HeapSort(arrheap, 0, size-1);
QuickSort(arrquick, 0, size-1);
if ( !ArrCompare(arrsrc, arrquick, arrheap, size) )
{
std::cout << "Error!" << std::endl;
ArrPrint(arrsrc, size);
ArrPrint(arrquick, size);
ArrPrint(arrheap, size);
}
}
free(block);
std::cout << "Please enter test times and size :";
}
return 0;
}