C/C++代码实现8种内部排序
#include<iostream>
using namespace std;
int n;
const int maxn = 100;
int R[maxn / 2 + 2];
int L[maxn / 2 + 2];
const int inf = 0x7ffff;
void InsertSort(int r[])
{
for (int i = 2; i <= n; i++)
{
if (r[i] < r[i - 1])
{
r[0] = r[i];
r[i] = r[i - 1];
int j = i - 2;
while (r[0] < r[j])
{
r[j + 1] = r[j];
--j;
}
r[j + 1] = r[0];
}
}
}
void BiInsertSort(int r[])
{
for (int i = 2; i <= n; i++)
{
r[0] = r[i];
int low = 1;
int high = i - 1;
while (low <= high)
{
int mid = high - (high - low) / 2;
if (r[0] < r[mid])
high = mid - 1;
else low = mid + 1;
}
int j = i - 1;
while (j >= high + 1)
{
r[j + 1] = r[j];
--j;
}
r[j + 1] = r[0];
}
}
void ShellInsert(int r[], int gap)
{
for (int i = gap + 1; i <= n; i++)
{
if (r[i] < r[i - gap])
{
r[0] = r[i];
int j = i - gap;
while (j > 0 && r[0] < r[j])
{
r[j + gap] = r[j];
j -= gap;
}
r[j + gap] = r[0];
}
}
}
void ShellSort(int r[], int delta[], int t)
{
for (int i = 0; i < t; i++)
{
ShellInsert(r, delta[i]);
}
}
void BubbleSort(int r[])
{
for (int i = 1; i < n; i++)
{
for (int j = 1; j <= n - i; j++)
{
if (r[j + 1] < r[j])
{
int t = r[j + 1];
r[j + 1] = r[j];
r[j] = t;
}
}
}
}
void Swap1(int* p, int* q) {
int temp;
temp = *p;
*p = *q;
*q = temp;
}
void Swap2(int& p, int& q) {
int temp = p;
p = q;
q = temp;
}
int Partition(int r[], int low, int high)
{
int pivotkey = r[low];
while (low < high) {
while (low < high && r[high] >= pivotkey) {
high--;
}
Swap1(&r[high], &r[low]);
while (low < high && r[low] <= pivotkey) {
low++;
}
Swap1(&r[low], &r[high]);
}
return low;
}
void QuickSort(int r[], int low, int high) {
if (low < high)
{
int pivotloc = Partition(r, low, high);
QuickSort(r, low, pivotloc - 1);
QuickSort(r, pivotloc + 1, high);
}
}
int QuickSort_(int r[], int low, int high)
{
if(low < high)
{
int pivotkey = r[low];
int pl = low;
int ph = high;
while (low < high)
{
while (low < high && r[high] >= pivotkey) {
high--;
}
if(low < high)
r[low++] = r[high];
while (low < high && r[low] <= pivotkey) {
low++;
}
if(low < high)
r[high--] = r[low];
}
int pivotloc = low;
r[pivotloc] = pivotkey;
QuickSort_(r, pl, pivotloc - 1);
QuickSort_(r, pivotloc + 1, ph);
}
}
void SelectSort(int r[])
{
for(int i = 1; i < n; i++)
{
int k = i;
for(int j = i + 1; j <= n; j++)
{
if(r[j] < r[k])
k = j;
}
if(k != i)
{
Swap1(&r[k], &r[i]);
}
}
}
void Merge(int r[], int left, int mid, int right)
{
int n1 = mid - left;
for(int i = 0; i < n1; i++)
{
L[i] = r[left + i];
}
int n2 = right - mid;
for(int j = 0; j < n2; j++)
{
R[j] = r[mid + j];
}
L[n1] = inf;
R[n2] = inf;
int i = 0;
int j = 0;
for(int k = left; k < right; k++)
{
if(L[i] <= R[j])
r[k] = L[i++];
else
r[k] = R[j++];
}
}
void MergeSort(int r[], int left, int right)
{
if(left < right - 1)
{
int mid = (left + right) / 2;
MergeSort(r, left, mid);
MergeSort(r, mid, right);
Merge(r, left, mid, right);
}
}
void HeapAdjust(int r[], int parent, int n)
{
int temp = r[parent];
for(int j = 2 * parent; j <= n; j++)
{
if(j < n && r[j] < r[j + 1]) ++j;
if(temp >= r[j]) break;
r[parent] = r[j];
parent = j;
}
r[parent] = temp;
}
void HeapSort(int r[])
{
for(int i = n / 2; i > 0; i--)
{
HeapAdjust(r, i, n);
}
for(int i = n; i > 1; i--)
{
int t = r[1];
r[1] = r[i];
r[i] = t;
HeapAdjust(r, 1, i - 1);
}
}
void print(int r[])
{
for (int i = 1; i <= n; i++)
{
cout << r[i] << " ";
}
cout << endl;
}
int main() {
cin >> n;
int r[maxn + 1];
for (int i = 1; i <= n; i++)
cin >> r[i];
HeapSort(r);
print(r);
return 0;
}
转载请注明原文地址:https://blackberry.8miu.com/read-3392.html