#include<iostream>
#include<vector>
using namespace std;
//直插法排序
void Insertsort(vector<int>& v)
{
int i, j;
for (i = 2; i < v.size(); i++)
{
if (v[i] < v[i - 1])
{
v[0] = v[i];
for ( j = i - 1; v[0] < v[j]; j--)
{
v[j + 1] = v[j];
}
v[j+1] = v[0];
}
}
}
//折半插入排序
void BInsertsort(vector<int>& v)
{
int high, low, mid;
for (int i = 2; i < v.size(); i++)
{
v[0] = v[i];
low = 1;
high = i - 1;
while (low <= high)
{
mid = (high + low) / 2;
if (v[0] < v[mid])
high = mid - 1;
else
low = mid + 1;
}
for (int j = i - 1; j >= high + 1; j--)
v[j + 1] = v[j];
v[high + 1] = v[0];
}
}
//希尔排序
void ShellInsert(vector<int>& v, int dk)
{
int i, j;
for (i = dk + 1; i < v.size(); i++)
{
if (v[i] < v[i - dk])
{
v[0] = v[i];
for (j = i - dk; j > 0 && v[0] < v[j]; j = j - dk)
v[j + dk] = v[j];
v[j + dk] = v[0];
}
}
}
void ShellSort(vector<int>& v, int dlta[], int t)
{
for (int k = 0; k < t; k++)
ShellInsert(v, dlta[k]);
}
//冒泡算法
void bubble_sort(vector<int>& v)
{
int m, j;
for (m = 2; m <= v.size() - 1; m++)
{
for(j=1;j<=v.size()-m;j++)
if (v[j] > v[j + 1])
{
v[0] = v[j];
v[j] = v[j + 1];
v[j + 1] = v[0];
}
}
}
//快速排序
int Partition(vector<int>& v, int low, int high)
{
v[0] = v[low];
int pivot = v[low];
while (low < high)
{
while (low < high&&v[high] >= pivot)
high--;
v[low] = v[high];
while (low < high&&v[low] <= pivot)
low++;
v[high] = v[low];
}
v[low] = v[0];
return low;
}
void QSort(vector<int>& v, int low, int high)
{
if (low < high)
{
int pivotloc = Partition(v, low, high);
QSort(v, low, pivotloc - 1);
QSort(v, pivotloc + 1, high);
}
}
//简单选择排序
void SelectSort(vector<int>& v)
{
int k;
for (int i = 1; i < v.size(); i++)
{
k = i;
for (int j = i + 1; j < v.size(); j++)
if (v[j] < v[k])
k = j;
if (k != i)
{
v[0] = v[k];
v[k] = v[i];
v[i] = v[0];
}
}
}
//堆排序
void HeapAdjust(vector<int>& v, int s,int m) //堆调整
{
int rc = v[s];
for (int j = 2 * s; j <= m; j *= 2)
{
if (j < m && v[j] < v[j + 1])
++j;
if (rc >= v[j])
break;
v[s] = v[j];
s = j;
}
v[s] = rc;
}
void HeapSort(vector<int>& v)
{
int i;
for (i = (v.size() - 1) / 2; i >= 1; i--)
HeapAdjust(v, i, v.size() - 1);
for (i = v.size() - 1; i > 1; i--)
{
v[0] = v[1];
v[1] = v[i];
v[i] = v[0];
HeapAdjust(v, 1, i - 1);
}
}
int main()
{
int N;
cin >> N;
vector<int> v(N + 1,0);
for (int i = 1; i < N + 1; i++)
cin >> v[i];
QSort(v,1,N);
for (int i = 1; i < v.size(); i++)
cout << v[i] << endl;
}
代码区——排序算法
最新推荐文章于 2021-11-19 11:40:14 发布