码迷,mamicode.com
首页 > 编程语言 > 详细

各种排序算法代码汇总

时间:2019-02-25 16:40:52      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:stream   ble   归并   quic   pad   sel   快速排序   space   sub   

#include <iostream>
#include <vector>
using namespace std;

//快速排序
void QuickSort(vector<int>& v, const int left, const int right)
{
if (left < right)
{
int i = left + 1;
int j = right;
int povit = v[left];
while (i < j)
{
while (povit > v[i])
{
//从左边找第一个大于pivot的
i++;
}
while (povit < v[j])
{
//从右边找第一个小于pivot的
j--;
}
if (i < j)
{
swap(v[i], v[j]);
}
}
swap(v[left], v[j]);
QuickSort(v, left, j-1);
QuickSort(v, j+1, right);
}
}

//归并排序
void Merge(vector<int> sub_arr1, vector<int> sub_arr2, vector<int> &arr)
{
int i = 0;
int j = 0;
while (i < sub_arr1.size() && j < sub_arr2.size())
{
if (sub_arr1[i] < sub_arr2[j])
{
arr.push_back(sub_arr1[i]);
i++;
}
else
{
arr.push_back(sub_arr2[j]);
j++;
}
}
while (i < sub_arr1.size())
{
arr.push_back(sub_arr1[i]);
i++;
}
while(j < sub_arr2.size())
{
arr.push_back(sub_arr2[j]);
j++;
}
}
void MergeSort(vector<int> &arr)
{
if (arr.size() < 2)
{
return;
}
int mid = arr.size() / 2;
vector<int> sub_arr1;
vector<int> sub_arr2;
for (int i = 0; i < mid; i++)
{
sub_arr1.push_back(arr[i]);
}
for (int i = mid; i < arr.size(); i++)
{
sub_arr2.push_back(arr[i]);
}
MergeSort(sub_arr1);
MergeSort(sub_arr2);
arr.clear();
Merge(sub_arr1, sub_arr2, arr);
}

//堆排序
void HeapAdjust(vector<int>&arr, int index, int len)
{
int max = index;
int lchild = index * 2 + 1;
int rchild = index * 2 + 2;
if (lchild < len && arr[lchild] > arr[max])
{
max = lchild;
}
if (rchild < len && arr[rchild] > arr[max])
{
max = rchild;
}
if (max != index)
{
swap(arr[max], arr[index]);
HeapAdjust(arr, max, len);
}
}
void HeapSort(vector<int> &arr, int len)
{
//调整成大顶堆, 从下至上每一个二叉树都调整成大顶堆
for (int i = len/2 -1; i >= 0; i--)
{
HeapAdjust(arr, i, len);
}
for (int i = len-1; i >=1; i--)
{
swap(arr[0], arr[i]);
HeapAdjust(arr, 0, i);
}
}

//插入排序
void InsertSort(vector<int> &arr, int n)
{
int i, j;
for (i = 1; i < n; i++)
{
int temp = arr[i];
for (j = i-1; j >= 0; j--)
{
if (temp < arr[j])
{
arr[j+1] = arr[j];
}
else
break;
}
arr[j+1] = temp;
}
}

//希尔排序
void ShellSort(vector<int> &arr, int len)
{
int gap = len;
do
{
gap = gap / 3 + 1;
for (int i = 0; i < gap; i++)
{
int j, k;
for (j = i + gap; j < len; j += gap)
{
int temp = arr[j];
for (k = j - gap; k >= i; k -= gap)
{
if (temp < arr[k])
{
arr[k + gap] = arr[k];
}
else
break;
}
arr[k+gap] = temp;
}
}
}while (gap > 1);

}

//冒泡排序
void BubbleSort(vector<int>& arr, int length)
{
for (int i = 0; i < length-1; i++)
{
for (int j = 0; j < length-1-i; j++)
{
if (arr[j] > arr[j+1])
{
swap(arr[j], arr[j+1]);
}
}
}
}

//选择排序
void SelectSort(vector<int>& arr, int n)
{
for(int i = 0; i < n; i++)
{
int min = i;
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[min])
{
min = j;
}
}
swap(arr[min], arr[i]);
}
}
int main()
{
vector<int> v;
v.push_back(3);
v.push_back(2);
v.push_back(1);
v.push_back(5);
v.push_back(12);
v.push_back(9);
v.push_back(6);
SelectSort(v, v.size());
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
system("pause");
return 0;
}

各种排序算法代码汇总

标签:stream   ble   归并   quic   pad   sel   快速排序   space   sub   

原文地址:https://www.cnblogs.com/mengjuanjuan/p/10431561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!