关于插入、合并、快速排序的源码
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
函数模版, 两个参数, 一个是数组首地址, 另一个是元素个数, 如 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
相关文章推荐
- 冒泡,插入,折半插入,希尔,快速,简单选择排序源码总结
- 关于排序(冒泡、选择、插入、快速)的几种算法
- 插入排序, 合并排序, 快速排序C++源代码
- 冒泡 ,插入,折半插入,希尔,快速,简单选择等排序源码
- 关于快速排序,因为MixingMilk了解了快速排序
- 插入,冒泡,选择,快速排序,二分查找
- 大二 第一次数据结构作业 (数组的排序,删除,查找,插入,合并)(线性表)
- 常见排序算法实现(直接插入排序,冒泡排序,快速排序,简单选择排序)
- 【内部排序】一:直接插入排序(Straight Insertion Sorting)的多种实现(不断优化+源码)
- 关于有序链表插入删除(内核源码)
- 常用排序算法--合并排序和快速排序
- 关于java中快速排序和冒泡排序的小结
- 九大排序算法及其实现- 插入.冒泡.选择.归并.快速.堆排序.计数.基数.桶排序.堆排序
- 数据结构-数组排序(冒泡 选择 插入 归并(合并有序数组))-C语言
- 排序方法,合并排序 、快速排序
- python排序算法-冒泡排序,选择排序,直接插入排序,希尔排序,归并排序,快速排序,堆排序
- 插入排序,选择排序,归并排序等等源码及简要分析
- 关于嵌套循环连接,排序合并连接,和哈希连接
- java:四种int【】数据排序,冒泡,插入,选择,快速排序
- 快速简单理解——直接插入排序