关于插入、合并、快速排序的源码

下面3个排序函数都是对数组进行排序, 排序后为非递减序列
函数模版,  两个参数, 一个是数组首地址, 另一个是元素个数, 如 merge_sort( A, 20);

/*******************************Insertion sort ****************************************/
template  < class  T >
void  insertion_sort(T entry[],  int  count)
{
   int first_unsorted;    //  position of first unsorted entry
   int position;          //  searches sorted part of list
   T current;        //  holds the entry temporarily removed from list

   for (first_unsorted = 1; first_unsorted < count; first_unsorted++)
      if (entry[first_unsorted] < entry[first_unsorted - 1]) {
         position = first_unsorted;
         current = entry[first_unsorted];         //  Pull unsorted entry out of the list.
         do {               //  Shift all entries until the proper position is found.
            entry[position] = entry[position - 1];
            position--;                           //  position is empty.
         }
 while (position > 0 && entry[position - 1] > current);
         entry[position] = current;
      }

}

/*******************************Insertion ^^^^ sort ****************************************/


合并排序:

/******************************Merge sort***************************************/
template < typename T >
void  merge(T arr[],  int  low,  int  mid,  int  high)
/* merge arr[low..mid] and arr[mid+1..high]; */
{
    int i, j, k;
    int len1=mid-low+1;
    int len2=high-mid;
    T *left=new T[len1];
    T *right=new T[len2];
    for(i=0; i<len1; i++)
        left[i]=arr[low+i];
    for(i=0; i<len2; i++)
        right[i]=arr[mid+1+i];
    for(i=0, j=0, k=low; i<len1 && j<len2 ; k++)
        if(left[i]<=right[j])
            arr[k]=left[i++];
        else
            arr[k]=right[j++];
    if(i>=len1)
        while( j<len2)
            arr[k++]=right[j++];
    else 
        while( i<len1)
            arr[k++]=left[i++];
      delete[] left;               // remember to delete 
      delete[] right;
    return;
}


template < typename T >
void  recursive_merge_sort(T arr[],  int  low,  int  high)
{
    if(low<high)
    {
        int    mid=(low+high)/2;
        recursive_merge_sort(arr, low, mid);
        recursive_merge_sort(arr, mid+1, high);
        merge(arr, low, mid, high);
    }

}


template < typename T >
void  merge_sort(T arr[],  int  num)
{
    recursive_merge_sort(arr, 0, num-1);
}


/******************Merge ^^^^^ sort****************************************************/



快速排序:


/******************************************Quick sort *******************************/
template  < class  T >
int  partition(T entry[],  int  low,  int  high)
{
   T pivot;
   int i,            //  used to scan through the list
       last_small;   //  position of the last key less than pivot
   swap(entry[low], entry[(low + high) / 2]);
   pivot = entry[low];   //  First entry is now pivot.
   last_small = low;
   for (i = low + 1; i <= high; i++)
      if (entry[i] < pivot) {
         last_small = last_small + 1;
         swap(entry[last_small], entry[i]);  //  Move large entry to right and small to left.
      }

   swap(entry[low], entry[last_small]);      //  Put the pivot into its proper position.
   return last_small;
}


template  < class  T >
void  recursive_quick_sort(T entry[],  int  low,  int  high)
{
   int pivot_position;
   if (low < high) {   //   Otherwise, no sorting is needed.
      pivot_position = partition(entry, low, high);
      recursive_quick_sort(entry, low, pivot_position - 1);
      recursive_quick_sort(entry, pivot_position + 1, high);
   }

}


template  < class  T >
void  quick_sort(T entry[],  int  count)
{
   recursive_quick_sort(entry, 0, count - 1);
}


例子:

#include<iostream>
#include<cstdlib>
#include<ctime>
using   namespace  std;

int  main()
{
    srand((unsigned)time( NULL ) ); //Seed the random-number generator with current time         
    int i, n=20;
    int arr[30];
    cout<<"before sorting: ";
    for(i=0; i<n; i++)
    {
        arr[i]=rand()%100;               // rand() 返回0-RAND_MAX (32767)的伪随机数
        cout<<arr[i]<<endl;
    }

    //quick_sort(arr, n):       // 调用quick_sort 
    //insertion_sort(arr, n);    // 调用insertion_sort
    merge_sort(arr, n);
    cout<<"after sorting: ";
    for(i=0; i<n; i++)
        cout<<arr[i]<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值