#include
#include
#include
#include
#define COUNT 100
void InsertSort(int *arr);
void BInsertSort(int * arr);
void ShellInsert(int * arr ,int dk);
void ShellSort(int *arr);
void BubbleSort(int * arr);
void QuickSort(int * arr,int low ,int high);
int FindPos(int *arr,int low ,int high);
void SelectSort(int * arr);
void HeadSort(int *arr);
void HeapAdjust(int *arr,int s,int m);
void CreatHeap(int *arr);
void MSort(int *R ,int * T ,int low, int high);
void Merge(int *R,int *T,int low,int mid ,int high);
int main(){
int arr[COUNT];
int buff[COUNT];
srand(time(NULL));
for(int i =0;i<COUNT;i++){
arr[i] = rand();
}
int n ;
printf("请输入排序方法序号\n");
printf("1 直接插入排序\n");
printf("2 折半插入排序\n");
printf("3 希尔排序\n");
printf("4 冒泡排序\n");
printf("5 快速排序\n");
printf("6 简单选择排序\n");
printf("7 堆排序\n");
printf("8 归并排序\n");
while(scanf("%d",&n)!=EOF){
switch(n){
case 1: InsertSort(arr);break;
case 2: BInsertSort(arr);break;
case 3: ShellSort(arr);break;
case 4: BubbleSort(arr);break;
case 5: QuickSort(arr,0,COUNT-1);break;
case 6: SelectSort(arr);break;
case 7: HeadSort(arr);break;
case 8: MSort(arr,buff,0,COUNT-1);break;
default: printf("输入错误,请重新输入:\n");
}
for(int i =0 ;i<COUNT;i++){
printf("%d\n",arr[i]);
}
for(int i =0;i<COUNT;i++){
arr[i] = rand();
}
}
return 0;
}
void InsertSort(int *arr){
for(int i = 1,val=0,j;i<COUNT;i++){
if(i<COUNT&&arr[i]<arr[i-1]){
val = arr[i];
arr[i]=arr[i-1];
for( j =i-2;j>=0&&arr[j]>val;j--){
arr[j+1]=arr[j];
}
arr[j+1] = val;
}
}
}
void BInsertSort(int * arr){
for(int i =1 ; i < COUNT;i++){
if(arr[i]<arr[i-1]){
int val = arr[i];
int low = 0;
int high = i-1;
while(low<=high){
int mid = (low+high)/2;
if(arr[mid]<val){
low = mid+1;
}else{
high = mid-1;
}
}
for(int j = i-1;j>=high+1;j--){
arr[j+1]=arr[j];
}
arr[high+1] = val;
}
}
}
void ShellInsert(int * arr ,int dk){
for(int i =dk,j ;i<COUNT;i++){
if(arr[i]<arr[i-dk]){
int val = arr[i];
for( j = i-dk;j>=0&&arr[j]>val;j-=dk){
arr[j+dk]=arr[j];
}
arr[j+dk] = val;
}
}
}
void ShellSort(int * arr){
int dt[10] = {1000,300,200,99,70,44,19,9,4,1};
for(int i =0 ;i<10;i++){
ShellInsert(arr,dt[i]);
}
}
void BubbleSort(int * arr){
int flag = 1;
for(int i = 1 ;i< COUNT&&flag == 1;i++){
flag = 0;
for(int j =0 ;j< COUNT - i;j++){
if(arr[j]>arr[j+1]){
flag = 1;
arr[j] = arr[j]^arr[j+1];
arr[j+1] = arr[j]^arr[j+1];
arr[j] = arr[j]^arr[j+1];
}
}
}
}
int FindPos(int * arr ,int low ,int high){
int val = arr[low];
while(low<high){
while(low<high&&arr[high]>=val){
high--;
}
arr[low] = arr[high];
while(low<high&&arr[low]<=val){
low++;
}
arr[high] = arr[low];
}
arr[low] = val;
return low;
}
void QuickSort(int * arr,int low ,int high){
if(low<high){
int pos = FindPos(arr,low,high);
QuickSort(arr,low,pos-1);
QuickSort(arr,pos+1,high);
}
}
void SelectSort(int * arr){
for(int i =0;i<COUNT -1;i++){
int k = i;
for(int j = i+1;j<COUNT;j++){
if(arr[k]>arr[j]){
k = j;
}
}
if(k!=i){
arr[i] = arr[i]^arr[k];
arr[k] = arr[i]^arr[k];
arr[i] = arr[i]^arr[k];
}
}
}
void HeapAdjust(int * arr,int s,int m){
int j = 0;
int val = arr[s];
for(int j= 2*s;j<=m;j*=2){
if(j<m&&arr[j]<arr[j+1]){
j++;
}
if(val>=arr[j])
break;
arr[s] = arr[j];
s=j;
}
arr[s]= val;
}
void CreatHeap(int *arr){
for(int j =COUNT/2;j>=0;j--){
HeapAdjust(arr,j,COUNT-1);
}
}
void HeadSort(int *arr){
CreatHeap(arr);
for(int i = COUNT-1;i>=1;i--){
arr[i] = arr[i]^arr[0];
arr[0] = arr[i]^arr[0];
arr[i] = arr[i]^arr[0];
HeapAdjust(arr,0,i-1);
}
}
void Merge(int *R ,int *T,int low ,int mid,int high){
int i = low;
int j = mid+1;
int k = 0;
while(i<=mid&&j<=high){
if(R[i]<R[j]){
T[k++] = R[i++];
}else{
T[k++] = R[j++];
}
}
while(i<=mid){
T[k++] = R[i++];
}
while(j<=high){
T[k++] = R[j++];
}
for(int t = 0;t<k;t++){
R[low+t] = T[t];
}
}
void MSort(int *R ,int * T ,int low, int high){
if(low<high){
int mid =(low+high)/2;
MSort(R,T,low,mid);
MSort(R,T,mid+1,high);
Merge(R,T,low,mid,high);
}
}