您的位置:首页 > 其它

比较排序算法

2016-04-08 12:08 155 查看
常用的比较排序算法有:

直接插入排序,希尔排序,选择排序,堆排序,冒泡排序,快速排序,归并排序等。

它们的时间复杂度及空间复杂度为:



实现代码如下:

#include<iostream>
using namespace std;

#include<assert.h>
#include<stack>

//插入排序
void InsertSort(int *a, size_t size)
{
assert(a);
for (int i = 1; i < size; i++)
{
int index = i;
int tmp = a[index];
int end = index - 1;
while (end >= 0 && a[end] > tmp)
{
a[end + 1] = a[end];
end--;
}
a[end + 1] = tmp;
}
}

//希尔排序
void ShellSort(int *a, size_t size)
{
assert(a);
int gap = size;
while (gap > 1)
{
gap = gap / 3 + 1;
for (size_t i = gap; i < size; i++)
{
int index = i;
int tmp = a[index];
int end = index - gap;
while (end >= 0 && a[end] > tmp)
{
a[end + gap] = a[end];
end -= gap;
}
a[end + gap] = tmp;
}
}
}

//选择排序
void SelectSort(int *a, size_t size)
{
assert(a);
int left, right;
for (left = 0, right = size - 1; left < right; left++, right--)
{
int min = left;
int max = right;
for (int i = left; i <= right; i++)
{
if (a[i] < a[min])
{
swap(a[i], a[min]);
}
if (a[i] > a[max])
{
swap(a[i], a[max]);
}
}
}
}

void Adjust(int *a, int size, int root)
{
int parent = root;
int child = parent * 2 + 1;
while (child < size)
{
if ((child + 1 < size) && (a[child] < a[child + 1]))
{
child++;
}
if (a[child] > a[parent])
{
swap(a[child], a[parent]);
parent = child;
child = child * 2 + 1;
}
else
{
break;
}
}
}

//堆排序
void HeapSort(int *a, int size)
{
assert(a);
for (int i = (size - 2) / 2; i >= 0; i--)
{
Adjust(a, size, i);
}
for (int j = size - 1; j >= 0; j--)
{
swap(a[0], a[j]);
Adjust(a, j, 0);
}
}

void Print(int *a, size_t size)
{
assert(a);
for (size_t i = 0; i < size; i++)
{
cout << a[i] << " ";
}
cout << endl;
}

//冒泡排序
void BubbleSort(int *a, int size)
{
assert(a);
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - 1 - i; j++)
{
if (a[j] > a[j + 1])
{
swap(a[j], a[j + 1]);
}
}
}
}

int PartSort1(int *a, int left, int right)
{
int key = a[right];
int begin = left;
int end = right - 1;
while (begin < end)
{
while (begin < end && a[begin] <= key)
{
begin++;
}
while (end > begin && a[end] >= key)
{
end--;
}
if (a[begin] > a[end])
{
swap(a[begin], a[end]);
}
}
if (a[begin] > key)
{
swap(a[begin], a[right]);
return begin;
}
else
{
return right;
}
}

//优化
int PartSort2(int *a, int left, int right)
{
assert(a);
int key = a[right];
int cur = left;
int prev = cur - 1;
while (cur < right)
{
if (a[cur] < key && ++prev != cur)
{
swap(a[cur], a[prev]);
}
++cur;
}
swap(a[++prev], a[right]);
return prev;
}

//快速排序(递归)
void QuickSort1(int *a, int left, int right)
{
assert(a);
if (right > left)
{
int boundary = PartSort2(a, left, right);
QuickSort1(a, left, boundary - 1);
QuickSort1(a, boundary + 1, right);
}
}

//非递归
void QuickSort2(int *a, int left, int right)
{
assert(a);
stack<int> s;
if (right > left)
{
int boundary = PartSort1(a, left, right);
if (left < boundary - 1)
{
s.push(left);
s.push(boundary - 1);
}
if (right > boundary + 1)
{
s.push(boundary + 1);
s.push(right);
}

while (!s.empty())
{
int end = s.top();
s.pop();
int begin = s.top();
s.pop();
boundary = PartSort1(a, begin, end);
if (begin < boundary - 1)
{
s.push(begin);
s.push(boundary - 1);
}
if (boundary + 1 < end)
{
s.push(boundary + 1);
s.push(end);
}
}
}
}

void SectionSort(int *a, int *tmp, int begin1, int end1, int begin2, int end2)
{
int index = begin1;
while (begin1 <= end1 && begin2 <= end2)
{
if (a[begin1] < a[begin2])
{
tmp[index++] = a[begin1++];
}
else
{
tmp[index++] = a[begin2++];
}
}
if (begin1 <= end1)
{
for (int i = begin1; i <= end1; i++)
{
tmp[index++] = a[begin1++];
}
}
if (begin2 <= end2)
{
for (int i = begin2; i <= end2; i++)
{
tmp[index++] = a[begin2++];
}
}
}

void _MergeSort(int *a, int *tmp, int left, int right)
{
int mid = left + (right - left) / 2;
if (left < right)
{
_MergeSort(a, tmp, left, mid);
_MergeSort(a, tmp, mid+1, right);
SectionSort(a, tmp, left, mid, mid+1, right);
memcpy(a + left, tmp + left, (right - left + 1)*sizeof(int));
}
}

//归并排序
void MergeSort(int *a,int size, int left, int right)
{
int *tmp = new int[size];
_MergeSort(a, tmp,left, right);
delete []tmp;
}

int main()
{
int a[10] = { 2, 5, 4, 9, 3, 6, 8, 7, 1, 0 };
//	InsertSort(a, 10);
//	ShellSort(a, 10);
//	SelectSort(a, 10);
//	HeapSort(a, 10);
//	BubbleSort(a, 10);
//	QuickSort1(a, 0, 9);
QuickSort2(a, 0, 9);
//	MergeSort(a,10, 0, 9);
Print(a, 10);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: