# 排序算法分析

#include<iostream>
#include<cassert>
#include <algorithm>
using namespace std;
void insertion_sort(int a[], int n)//insertion sort
{
assert(a != NULL );
int temp;
int j = 0;
for (int p = 1; p < n; ++p)
{
temp = a[ p];
for ( j = p; j > 0 && a[j - 1 ]>temp; --j)
{
a [j] = a[j - 1 ];
}
a [j] = temp;
}
}
// bubble sort 的改进版，一旦没发生交换则排序结束， flag去掉则为原始版本。
void bubble_sort(int a[], int n)
{
assert(a != NULL );
int i, j ,flag= 1;
for (i = 0 ; i < n - 1 && flag; ++i)
{
flag = 0 ;
for ( j = 0; j < n - 1 - i;++j )
if ( a[j]>a [j + 1 ])
{
swap (a[ j], a[j + 1 ]);
flag = 1 ;
}
}
}
void selection_sort(int a[], int n)//selection sort
{
assert(a != NULL );
int i, j , min;
for (i = 0 ; i < n; ++i )
{
min = i;
for ( j = i + 1; j < n;++ j)
if ( a[j] < a[ min])
min = j;
if(min != i)
swap (a[ min], a [i]);
}
}
int partition(int a[], int start, int end) //partition 算法
{
assert(a != NULL );
int middle = ( start + end ) / 2 ;
int pivotIndex ;
if (a[ start] < a[ middle])
if ( a[middle] < a[ end])
pivotIndex = middle;
else if (a[start ] < a[end])
pivotIndex = end;
else
pivotIndex = start;
else if ( a[start] <a[ end])
pivotIndex = start;
else if ( a[middle]<a [end])
pivotIndex = end;
else
pivotIndex = middle;
int pivot = a[ pivotIndex];
swap(a [pivotIndex], a[end ]);
int storeIndex = start;
for (int i = start ; i < end; ++i)
{
if ( a[i]<pivot )
{
swap (a[ i], a[storeIndex ]);
++storeIndex;
}
}
swap(a [storeIndex], a[end ]);
cout << pivot << endl;
return storeIndex ;
}
void quick_sort(int a[], int left, int right) //quick sort
{
if (left == right)
return;
int index = partition(a, left , right);
if (index > left)
quick_sort (a, left, index - 1 );
if (index < right)
quick_sort (a, index+1 , right);
}
//归并排序的merge相当于两个已排序链表或者数组的合并
void merge( int arr[], int temp_arr[], int start_index, int mid_index , int end_index)
{
int i = start_index , j = mid_index + 1;
int k = 0;
while (i < mid_index + 1 && j < end_index + 1)
temp_arr [k++] = arr[i ] > arr[j ] ? arr[j ++] : arr[i ++];
while (i < mid_index+1 )
temp_arr [k++] = arr[i ++];
while (j < end_index+1 )
temp_arr [k++] = arr[j ++];
for (i = 0 , j = start_index; j < end_index+1; i ++, j++)
arr [j] = temp_arr[i ];
}
void merge_sort(int a[], int temp_a[], int start_index, int end_index )
{
if (start_index == end_index)
return;
int mid_index = ( start_index + end_index ) / 2 ;
merge_sort(a , temp_a, start_index, mid_index );
merge_sort(a , temp_a, mid_index + 1, end_index );
merge(a , temp_a, start_index, mid_index , end_index);
}
void heap_adjust(int a[], int i, int len)//heap sort
{
int child ;
int temp;
for (; 2 * i + 1 < len; i = child )
{
child = 2 * i + 1;
if(child <len - 1 && a[child + 1 ] > a[child])
++child;
if ( a[i] < a[ child])
{
temp = a[ i];
a [i] = a[child ];
a [child] = temp;
}
else
break;
}
}
void heap_sort(int a[], int len)
{
assert(a != NULL );
int i;
for ( i = len / 2; i >= 0 ; -- i)
for (i = len - 1; i > 0 ; -- i)
{
int tmp = a[0];
a [0] = a[i ];
a [i] = tmp;
}
}
//shell sort，中间两个循环的思想和插入排序一样，每次相当于对 incur个子数组进行排序
void shell_sort(int a[], int len)
{
int i, j , incr;
int temp;
for (incr = len / 2; incr > 0 ; incr /= 2 )
{
for ( i = incr; i < len; i++)
{
temp = a[ i];
for ( j = i; j >= incr && a[j - incr ]>temp; j -= incr )
a [j] = a[j - incr];
a [j] = temp;
}
}
}
int main() {
int arr[] = { 61 , 17 , 29 , 22 , 34 , 60 , 72 , 21 , 50 , 1 , 62 };
int len = (int) sizeof(arr) / sizeof(*arr);
cout << len << endl;
int *temp_a = new int [len];
merge_sort(arr ,temp_a, 0,len -1);
delete[]temp_a;
for (int i = 0; i < len; i++)
cout << arr[ i] << ' ';
cout << endl ;
return 0;
}

#### 排序算法实现及分析

2011-07-15 11:00:26

#### 几种排序算法实现分析

2013-06-30 23:09:11

#### 7种排序算法实现及分析

2014-10-07 07:23:17

#### 排序算法实现

2015年12月10日 14KB 下载

#### 排序算法实现(下)

2017-06-26 16:21:07

#### 排序算法实现以及比较

2016-07-31 19:33:28

#### 五种排序算法实现

2009-07-24 16:30:00

#### 数据结构——排序查找算法实现

2016-08-18 16:13:03

#### 通过分析 JDK 源代码研究 TreeMap 红黑树算法实现（自然排序）

2016-08-19 15:59:38

#### 几种排序算法实现

2018-04-01 21:17:55