您的位置:首页 > 其它

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

2011-02-23 09:20 225 查看
下面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;


}
转载地址:http://blog.csdn.net/shaofei1983/archive/2007/10/25/1843249.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: