各种排序算法代码汇总
2019-02-25 16:20 mengjuanjuan1994 阅读(315) 评论(0) 编辑 收藏 举报//快速排序
void QuickSort(vector<int>& v, const int left, const int right)
{
if (left < right)
{
int i = left;
int j = right;
int povit = v[left];
while (i < j)
{
while (povit <= v[j] && i < j)
{
//从右边找第一个小于pivot的
j--;
}
while (povit >= v[i] && i < j)
{
//从左边找第一个大于pivot的
i++;
}
if (i < j)
{
swap(v[i], v[j]);
}
}
swap(v[left], v[j]);
QuickSort(v, left, j-1);
QuickSort(v, j+1, right);
}
}
//快速排序的非递归遍历
int partition1(vector<int> &v, int left, int right)
{
int i = left;
int j = right;
int povit = v[left];
while (i < j)
{
while (v[j] >= povit && i < j)
{
j--;
}
while (v[i] <= povit && i < j)
{
i++;
}
if (i < j)
{
swap(v[i], v[j]);
}
}
swap(v[left], v[j]);
return j;
}
void QiockSortNor(vector<int> &v, int left, int right)
{
if (left >= right)
return;
stack<int> s;
s.push(left);
s.push(right);
while (!s.empty())
{
int right = s.top();
s.pop();
int left = s.top();
s.pop();
if (left < right)
{
int boundary = partition1(v, left, right);
// 左区间
s.push(left);
s.push(boundary - 1);
// 右区间
s.push(boundary + 1);
s.push(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;
}