:1.222KB : :1 :2022-08-26 18:11:05
//此文件包含所有排序算法
#include "sort.h"
#include
using namespace std;
#include "stdio.h"
void insertsort(int a[], int n)
{
int exc = 0, cmp = 0;
for (int i = 2; i < n; i )
{
if (a[i] < a[i - 1])
{//若第i个元素大于i-1元素,直接插入。小于的话,移动有序表后插入
int j = i - 2;
a[0] = a[i]; //复制为哨兵,即存储待排序元素
a[i] = a[i - 1]; //先后移一个元素
exc ;
while (a[0] < a[j])
{//查找在有序表的插入位置
a[j 1] = a[j];
j--; //元素后移
cmp ;
exc ;
}
cmp ;
a[j 1] = a[0]; //插入到正确位置
exc ;
}
cmp ;
for (int k = 1; k < n; k )
{//打印每趟排序的结果
cout << a[k] << " ";
}
cout << endl;
}
cout << "交换次数为:" << exc << "次 " << "比较次数为:" << cmp << "次" << endl;
}
void Linsertsort(int a[], int n)
{
long long int exc = 0, cmp = 0;
for (int i = 2; i < n; i )
{
if (a[i] < a[i - 1])
{//若第i个元素大于i-1元素,直接插入。小于的话,移动有序表后插入
int j = i - 2;
a[0] = a[i]; //复制为哨兵,即存储待排序元素
a[i] = a[i - 1]; //先后移一个元素
exc ;
while (a[0] < a[j])
{//查找在有序表的插入位置
a[j 1] = a[j];
j--; //元素后移
cmp ;
exc ;
}
cmp ;
a[j 1] = a[0]; //插入到正确位置
exc ;
}
cmp ;
/*for (int k = 1; k < n; k )
{//打印每趟排序的结果
cout << a[k] << " ";
}
cout << endl;*/
}
cout << "交换次数为:" << exc << "次 " << "比较次数为:" << cmp << "次" << endl;
}
void selectsort(int a[], int n)
{
int exc = 0, cmp = 0;
for (int i = 1; i < n; i )
{
int k = i; //选择具有最小排序码的对象
for (int j = i 1; j < n; j )
{
if (a[j] < a[k])
k = j; //当前具最小排序码的对象
cmp ;
}
if (k != i) //对换到第i 个位置
{
a[0] = a[i];
a[i] = a[k];
a[k] = a[0];
exc =3;
}
cmp ;
for (int k = 1; k < n; k )
{//打印每趟排序的结果
cout << a[k] << " ";
}
cout << endl;
}
cout << "交换次数为:" << exc << "次 " << "比较次数为:" << cmp << "次" << endl;
}
void Lselectsort(int a[], int n)
{
long long int exc = 0, cmp = 0;
for (int i = 1; i < n; i )
{
int k = i; //选择具有最小排序码的对象
for (int j = i 1; j < n; j )
{
if (a[j] < a[k])
k = j; //当前具最小排序码的对象
cmp ;
}
if (k != i) //对换到第i 个位置
{
a[0] = a[i];
a[i] = a[k];
a[k] = a[0];
exc = 3;
}
cmp ;
/*for (int k = 1; k < n; k )
{//打印每趟排序的结果
cout << a[k] << " ";
}
cout << endl;*/
}
cout << "交换次数为:" << exc << "次 " << "比较次数为:" << cmp << "次" << endl;
}
void bubblesort(int a[], int n)
{
int exc = 0, cmp = 0;
for (int i = 1; i < n; i)
{
for (int j = 1; j < n - i; j)
{
if (a[j] > a[j 1])
{
a[0] = a[j];
a[j] = a[j 1];
a[j 1] = a[0];
exc = 3;
}
cmp ;
}
for (int k = 1; k < n; k )
{//打印每趟排序的结果
cout << a[k] << " ";
}
cout << endl;
}
cout << "交换次数为:" << exc << "次 " << "比较次数为:" << cmp << "次" << endl;
}
void Lbubblesort(int a[], int n)
{
long long int exc = 0, cmp = 0;
for (int i = 1; i < n; i)
{
for (int j = 1; j < n - i; j)
{
if (a[j] > a[j 1])
{
a[0] = a[j];
a[j] = a[j 1];
a[j 1] = a[0];
exc = 3;
}
cmp ;
}
/*for (int k = 1; k < n; k )
{//打印每趟排序的结果
cout << a[k] << " ";
}
cout << endl;*/
}
cout << "交换次数为:" << exc << "次 " << "比较次数为:" << cmp << "次" << endl;
}
void shellsort()
{
}
void heapsort()
{
}
void quicksort(int a[], int low, int high, int n, int&qcmp, int&qexc) //递归
{
if (low < high)
{
int pivotpos = partition(a, low, high, n, qcmp, qexc);
quicksort(a, low, pivotpos - 1, n, qcmp, qexc); //依次对两个子表进行递归排序
quicksort(a, pivotpos 1, high, n, qcmp, qexc);
}
}
int partition(int a[], int low, int high, int n, int&qcmp, int&qexc) //划分操作
{
int pivot = a[low]; //选取首个元素作为枢纽
while (low < high)
{
while (low < high && a[high] >= pivot)
{
high--;
qcmp ;
}
a[low] = a[high];
qexc ;
while (low < high && a[low] <= pivot)
{
low ;
qcmp ;
}
a[high] = a[low];
qexc ;
}
a[low] = pivot;
qexc ;
for (int k = 1; k < n; k )
{//打印每趟排序的结果
cout << a[k] << " ";
}
cout << endl;
return low;
}
void Lquicksort(int a[], int low, int high, int n, long long int&qcmp1, long long int& qexc1)
{
if (low < high)
{
int pivotpos = Lpartition(a, low, high, n, qcmp1, qexc1);
Lquicksort(a, low, pivotpos - 1, n, qcmp1, qexc1);
Lquicksort(a, pivotpos 1, high, n, qcmp1, qexc1);
}
}
int Lpartition(int a[], int low, int high, int n, long long&qcmp1, long long&qexc1)
{
int pivot = a[low];
while (low < high)
{
while (low < high && a[high] >= pivot)
{
high--;
qcmp1 ;
}
a[low] = a[high];
qexc1 ;
while (low < high && a[low] <= pivot)
{
low ;
qcmp1 ;
}
a[high] = a[low];
qexc1 ;
}
a[low] = pivot;
qexc1 ;
return low;
}
void mergesort()
{
}