十大排序(常用的几个)及测试代码

十大排序(常用的几个)及测试代码

#include <iostream>
#include <ctime>
#include <algorithm>
#include <climits>

using namespace std;

int len = 0;


//related fucntion
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);

//sort algorithm
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));
  //len = 4;
  //int arr[len] = {7,2,4,6};
  //print(arr);
  //selection(arr);
  //bubble(arr);
  //shell(arr);
  //merge(arr,0,4,7);
  //quick(arr,0,len-1);
  //counting(arr);
  //print(arr);
  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);
    //print(arr1);
    //selection(arr2);
    //bubble(arr2);
    //insertion(arr2);
    //shell(arr2);
    //merge_sort(arr2,0,len-1);
    //quick_sort(arr2,0,len-1);
    heap_sort(arr2,len);
    //counting(arr2);
    //print(arr2);

    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 );
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值