#include <iostream>
using namespace std;
void swap(int &x,int &y)
{
if (x != y)
{
int temp = x;
x = y;
y = temp;
}
}
/InsertSort
void InsertSort(int* a,int n)
{
int i,j;
for ( i = 1; i < n; i++)
{
if (a[i] < a[i - 1])
{
int temp = a[i];
for ( j = i-1; j >=0 && a[j] > temp;j--)
{
a[j+1] = a[j];
}
a[j+1] = temp;
}
}
}
//Bubble Sort 下沉
void BubbleSort1(int *a, int n)
{
int i,j;
bool flag;
for (i = 0; i < n - 1; i++)
{
flag = false;
for (j = 0;j < n-1-i; j++)
{
if (a[j] > a[j+1])
{
int temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
flag = true;
}
}
if (!flag)
{
return;
}
}
}
//BubbleSort 上浮
void BubbleSort2(int* a,int n)
{
int i,j;
bool flag;
for (i = 0; i < n-1; i++)
{
flag = false;
for (j = n-1; j > i; j-- )
{
if (a[j] < a[j-1])
{
int temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
flag = true;
}
}
if (!flag)
{
return;
}
}
}
//BubbleSort 双向
void BubbleSort3(int* a, int low,int high)
{
int i;
while(low < high)
{
for (i = 0; i < high-low;i++)
{
int left = low+i,right = high-i;
if (a[left] > a[left+1])
{
swap(a[left],a[left+1]);
}
if (a[right] < a[right-1])
{
swap(a[right],a[right-1]);
}
}
high--;
low++;
}
}
//SelectSort
void SelectSort(int* a,int n)
{
int i,j,minIndex;
for (i = 0; i < n; ++i)
{
minIndex = i;
for (j = i+1;j < n; ++j)
{
if (a[j] < a[minIndex])
{
minIndex = j;
}
}
if (minIndex != i)
{
swap(a[i],a[minIndex]);
}
}
}
//QuickSort
int partition(int* a,int low, int high)
{
int i = low;
int j = high;
int privot = a[low];
while(i < j)
{
while(a[j] >= privot&& i < j)
j--;
a[i] = a[j];
while(a[i] <= privot&& i < j)
i++;
a[j] = a[i];
}
a[i] = privot;
return i;
}
void QuickSort(int* a,int low, int high)
{
if(low < high)
{
int k = partition(a,low,high);
QuickSort(a,low,k-1);
QuickSort(a,k+1,high);
}
}
//MergeSort
//将有二个有序数列a[first...mid]和a[mid...last]合并。
void mergearray(int a[], int first, int mid, int last, int temp[])
{
int i = first, j = mid + 1;
int m = mid, n = last;
int k = 0;
while (i <= m && j <= n)
{
if (a[i] <= a[j])
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}
while (i <= m)
temp[k++] = a[i++];
while (j <= n)
temp[k++] = a[j++];
for (i = 0; i < k; i++)
a[first + i] = temp[i];
}
void mergesort(int a[], int first, int last, int temp[])
{
if (first < last)
{
int mid = (first + last) / 2;
mergesort(a, first, mid, temp); //左边有序
mergesort(a, mid + 1, last, temp); //右边有序
mergearray(a, first, mid, last, temp); //再将二个有序数列合并
}
}
bool MergeSort(int a[], int n)
{
int *p = new int[n];
if (p == NULL)
return false;
mergesort(a, 0, n - 1, p);
delete p;
return true;
}
//HeapSort 大根堆 从小到大排序
void MaxHeapFixdown(int* a,int i,int n)//n为节点总数,二叉堆编号从0开始,i的父节点是 (i-1)/2 左孩子 2*i +1 右孩子 2*i +2
{
int j,temp;
temp = a[i];
j = 2*i + 1;
while(j < n)
{
if (j+1 < n && a[j+1] > a[j])
{
j++;
}
if (a[j] <= temp)
{
break;
}
a[i] = a[j];
i = j;
j = 2*i + 1;
}
a[i] = temp;
}
void makeMaxHeap(int*a, int n)
{
for (int i = n/2-1;i >= 0;i--)
{
MaxHeapFixdown(a,i,n);
}
}
void MaxHeapSort(int *a,int n)
{
for (int i = n-1; i >= 1;i--)
{
swap(a[i],a[0]);
MaxHeapFixdown(a,0,i);
}
}
//BinaryInsertSort
void BinaryInsertSort(int* a, int n)
{
int low,high,mid,current;
for (int i = 1; i < n; i++)
{
if (a[i] < a[i-1])
{
current = a[i];
low = 0;
high = i-1;
while(low <= high)
{
mid = (low + high)/2;
if (a[i] < a[mid])
{
high = mid - 1;
}else{
low = mid + 1;
}
}
for (int j = i-1; j>=low; j--)
{
a[j+1] = a[j];
}
a[low] = current;
}
}
}
int main()
{
int N;
cin>>N;
int* a = new int[N];
for (int i = 0; i < N; i++)
{
cin>>a[i];
}
// InsertSort(a,N);
//BubbleSort1(a,N);
//BubbleSort2(a,N);
//BubbleSort3(a,0,N-1);
//SelectSort(a,N);
//QuickSort(a,0,N-1);
//MergeSort(a,N);
//makeMaxHeap(a,N);
//MaxHeapSort(a,N);
BinaryInsertSort(a,N);
for (int i = 0; i < N; i++)
{
cout<<a[i]<<" ";
}
return 0;
}
各种内部排序
最新推荐文章于 2021-05-30 11:34:19 发布