附上C语言版本----点击此处
新手小白,代码粗糙,有不足之处欢迎指出嗷嗷QAQ
头文件与Sort类
#include<iostream>
#include<cstdlib>
using namespace std;
#define MAX 10 //可自行设置
class Sort
{
int *arry;
public:
Sort(int *a):arry(a){};
~Sort(){delete arry;};
void print();
void swap(int i, int j);
void sort1();//优化版冒泡排序
void sort2();//选择法排序
void sort3();//简单插入排序
void sort4();//折半插入排序
void sort5();//希尔排序
//堆排序
void sort6();
void sort6_siftdown(int i, int j);
//归并排序
void sort7();
void sort7_MSort(int frist, int last, int* temp);
void sort7_Merge( int frist, int mid, int last, int* temp);
//快速排序
void sort8();
void sort8_quick(int *a,int left, int right);
void sort9();//计数排序
void sort10();//基数排序
};
输出与置换操作
void Sort::print()
{
int i,*a=this->arry;
for (i = 0; i < MAX; i++)
cout<<a[i]<<" ";
cout<<endl;
}
void Sort::swap(int i, int j)
{
int t,*a=this->arry;
t = a[i]; a[i] = a[j]; a[j] = t;
}
优化版冒泡排序
void Sort::sort1()
{
int i, j,*a=this->arry;
for (i = 1; i < MAX; i++)
{
int flag = 1;
for (j = 0; j < MAX - i; j++)
{
if (a[j] > a[j + 1]) { flag = 0; swap(j, j + 1); }
}
if (flag) break;
}
}
选择法排序
void Sort::sort2()
{
int i, j, max,*a=this->arry;
for (i = 0; i < MAX; i++)
{
max = 0;
for (j = 0; j < MAX - i; j++)
{
if (a[j] > a[max]) max = j;
}
swap(j - 1, max);
}
}
简单插入排序
void Sort::sort3()
{
int i, j, temp,*a=this->arry;
for (i = 0; i < MAX - 1; i++)
{
if (a[i + 1] < a[i])
{
temp = a[i + 1];
for (j = i; j >= 0; j--)
{
if (temp > a[j]) break;
a[j + 1] = a[j];
}
a[j + 1] = temp;
}
}
}
折半插入排序
void Sort::sort4()
{
int i, t, left, right, middle, temp,*a=this->arry;
for (i = 0; i < MAX - 1; i++)
{
if (a[i + 1] < a[i])
{
temp = a[i + 1];
left = 0; right = i;
while (left <= right)
{
middle = (left + right) / 2;
if (temp > a[middle]) left = middle + 1;
else right = middle - 1;
}
if (a[middle] < temp) middle++;
for (t = i; t >= middle; t--) a[t + 1] = a[t];
a[middle] = temp;
}
}
}
希尔排序
void Sort::sort5()
{
int increment = MAX, i, j, t, temp,*a=this->arry;
do
{
increment = increment / 3 + 1;
for (i = 0; i + increment < MAX; i += increment)
{
if (a[i + increment] < a[i])
{
temp = a[i + increment];
for (j = i; j >= 0; j -= increment)
{
if (temp > a[j]) break;
a[j + increment] = a[j];
}
a[j + increment] = temp;
}
}
} while (increment > 1);
}
堆排序
void Sort::sort6()
{
int i, j = MAX, t,*a=this->arry;
for (i = j / 2 - 1; i >= 0; i--) sort6_siftdown(i, j);
for (t = 0; t < MAX; t++)
{
swap(--j, 0);
sort6_siftdown(0, j);
}
}
void Sort::sort6_siftdown(int i, int j)
{
int t, max,*a=this->arry;
while (i * 2 + 1 < j)
{
max = i;
if (a[i * 2 + 1] > a[i]) max = i * 2 + 1;
if (i * 2 + 2 < j && a[i * 2 + 2] > a[max]) max = i * 2 + 2;
if (max != i) { swap(i, max); i = max; }
else break;
}
}
归并排序
void Sort::sort7()
{
int* temp = (int*)malloc(sizeof(int) * MAX);
sort7_MSort(0, MAX - 1, temp);
}
void Sort::sort7_MSort(int frist, int last, int* temp)
{
if (frist < last)
{
int mid = (frist + last) / 2;
sort7_MSort( frist, mid, temp);
sort7_MSort( mid + 1, last, temp);
sort7_Merge(frist, mid, last, temp);
}
}
void Sort::sort7_Merge(int frist, int mid, int last, int* temp)
{
int i = frist, j = mid + 1, k = 0, t,*a=this->arry;
while (i <= mid && j <= last)
{
if (a[i] < a[j]) temp[k++] = a[i++];
else temp[k++] = a[j++];
}
while(i <= mid) temp[k++] = a[i++];
while (j <= last) temp[k++] = a[j++];
for (t = 0; t < k; t++) a[t + frist] = temp[t];
}
快速排序
void Sort::sort8()
{
int *a=this->arry;
sort8_quick(a,0, MAX - 1);
}
void Sort::sort8_quick(int *a,int left, int right)
{
if (left >= right) return;
int temp = a[left], i = left, j = right;
while (i != j)
{
while (a[j] >= temp && j > i) j--;
while (a[i] <= temp && j > i) i++;
if (j > i) swap(i, j);
}
swap(i, left);
sort8_quick(a,left, i - 1); sort8_quick(a,i + 1, right);
}
计数排序
void Sort::sort9()
{
int *a=this->arry,min = a[0], max = a[0], i;
int* new_a = (int*)malloc(MAX * sizeof(int));
for (i = 1; i < MAX; i++)
{
if (a[i] > max) max = a[i];
if(a[i]<min) min = a[i];
}
int* book = (int*)malloc((max - min + 1) * sizeof(int));
for (i = 0; i < max - min + 1; i++) book[i] = 0;
for (i = 0; i < MAX; i++) book[a[i] - min]+=1;
for (i = 1; i < max - min + 1; i++) book[i] += book[i - 1];
for (i = 0; i < MAX; i++)
{
new_a[book[a[i] - min] - 1] = a[i]; book[a[i]-min]--;
}
for (i = 0; i < MAX; i++) a[i] = new_a[i];
}
基数排序 (不适用于负数情况)
void Sort::sort10()
{
int book[10][MAX+1], flag = 1, i, j = 1, t, k, temp,*a=this->arry;
for (i = 0; i < 10; i++) book[i][MAX] = 0;
while (flag)
{
flag = 0;
for (i = 0; i < MAX; i++)
{
if (a[i] / j % 10 != 0) flag = 1;
temp = a[i] / j % 10;
book[temp][book[temp][MAX]] = a[i];
book[temp][MAX]++;
}
j *= 10;
for (i = 0, k = 0; i < 10; i++)
{
for (t = 0; t < book[i][MAX]; t++) { a[k++] = book[i][t]; }
book[i][MAX] = 0;
}
}
}
主函数
int main()
{
int a[10]={7,3,199,435,6756,54,2,13,343,0};//举例
Sort *one=new Sort(a);
one->print();
one->sort10();
one->print();
}
END