十大排序(常用的几个)及测试代码
#include <iostream>
#include <ctime>
#include <algorithm>
#include <climits>
using namespace std;
int len = 0;
void swap(int *arr,int i,int j);
void print(int arr[]);
void generate_arr(int *arr,int maxValue);
void check(int maxSize,int maxValue);
void copy_arr(int *src,int *tgt);
void selection(int *arr);
void bubble(int *arr);
void insertion(int *arr);
void shell(int *arr);
void merge(int *arr,int leftptr,int leftbound,int rightptr,int rightbound);
void merge_sort(int *arr,int left,int right);
int quick(int *arr,int leftbound,int rightbound);
void quick_sort(int *arr,int left,int right);
void heapify(int *arr,int n,int i);
void build_heap(int *arr,int n);
void heap_sort(int *arr,int n);
void counting(int *arr);
int main()
{
srand((unsigned int)time(NULL));
check(100,100);
return 0;
}
void swap(int *arr,int i,int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void print(int arr[])
{
for(int i = 0; i < len;i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
void generate_arr(int *arr,int maxValue)
{
for(int i = 0;i < len;i++)
{
arr[i] = (rand() % maxValue);
}
}
void check(int maxSize,int maxValue)
{
bool result = true;
for(int j = 0; j < 100;j++)
{
len = (rand() % maxSize);
int *arr1 = new int[len];
int *arr2 = new int[len];
generate_arr(arr1,maxValue);
copy_arr(arr1,arr2);
print(arr1);
sort(arr1,arr1+len);
heap_sort(arr2,len);
for(int i = 0;i < len;i++)
{
if(arr2[i] != arr1[i])
{
result = false;
cout << "false" << endl;
delete [] arr1;
delete [] arr2;
return;
}
}
delete [] arr1;
delete [] arr2;
}
cout << "true" << endl;
}
void copy_arr(int *src,int *tgt)
{
for(int i = 0;i < len;i++)
{
tgt[i] = src[i];
}
}
void selection( int *arr ) {
if( len < 2 ) {
return;
}
for( int i = 0; i < len-1; i++ ) {
int minpos = i;
for( int j = i+1; j < len; j++ ) {
if( arr[j] < arr[minpos] ) {
minpos = j;
}
}
if( minpos != i ) {
swap( arr, minpos, i );
}
}
}
void bubble( int *arr ) {
if( len < 2 ) {
return;
}
for( int i = len-1; i > 0; i-- ) {
for( int j = 0; j < i; j++ ) {
if( arr[j] > arr[j+1] ) {
swap( arr, j , j+1 );
}
}
}
}
void insertion( int *arr ) {
if( len < 2 ) {
return;
}
for( int i = 1; i < len; i++ ) {
for( int j = i; j > 0; j-- ) {
if( arr[j] < arr[j-1] ) {
swap( arr, j, j-1 );
}
}
}
}
void shell( int *arr ) {
if( len < 2 ) {
return;
}
for( int gap = len/2; gap > 0; gap /= 2 )
for( int i = gap; i < len; i++ )
for( int j = i; j > gap-1; j -= gap ) {
if( arr[j] < arr[j-gap] ) {
swap( arr, j , j-gap );
}
}
}
void merge( int *arr, int leftptr, int leftbound, int rightptr, int rightbound ) {
if( left >= right ) {
return;
}
int left = leftptr, right = rightptr;
int *ans = new int[rightbound - leftptr + 1]();
int k = 0;
while( left <= leftbound && right <= rightbound ) {
ans[k++] = arr[left] <= arr[right] ? arr[left++] : arr[right++];
}
while( left <= leftbound ) {
ans[k++] = arr[left++];
}
while( right <= rightbound ) {
ans[k++] = arr[right++];
}
while( k-- ) {
arr[leftptr+k] = ans[k];
}
delete [] ans;
}
void merge_sort( int *arr, int left, int right ) {
if( left >= right ) {
return;
}
int mid = left + (right - left) / 2;
merge_sort( arr, left, mid);
merge_sort( arr, mid+1, right );
merge( arr, left, mid, mid+1, right );
}
int quick( int *arr, int leftbound, int rightbound ) {
int pivot = arr[leftbound];
int left = leftbound+1;
int right = rightbound;
while( left <= right ) {
while( left <= right && arr[left] < pivot ) {
left++;
}
while( left <= right && arr[right] >= pivot ) {
right--;
}
if( left < right ) {
swap( arr, left, right );
}
}
swap( arr, leftbound, right );
return right;
}
void quick_sort( int *arr, int left, int right ) {
if( left >= right ) {
return;
}
int pivot = quick( arr, left, right );
quick_sort( arr, left, pivot-1 );
quick_sort( arr, pivot+1, right );
}
void heapify( int *arr, int n, int i )
{
int c1 = 2*i+1;
int c2 = 2*i+2;
int max_pos = i;
if( c1 < n && arr[c1] > arr[max_pos] )
{
max_pos = c1;
}
if( c2 < n && arr[c2] > arr[max_pos] )
{
max_pos = c2;
}
if( max_pos != i )
{
swap( arr, max_pos, i );
heapify( arr, n, max_pos );
}
}
void build_heap( int *arr, int n )
{
int last_node = n-1;
int parent = (last_node-1)/2;
for( ; parent >=0; parent-- )
{
heapify( arr, n, parent );
}
}
void heap_sort( int *arr, int n )
{
build_heap( arr, n );
for( int i = n-1; i > 0; i-- )
{
swap( arr, i, 0 );
heapify( arr, i, 0 );
}
}