插入排序
#include "pch.h"#include <iostream>
#include <stdlib.h>
void Insert_Sort(int k[], int n);
void Binary_Insert_Sort(int k[], int n);
void Shell_Sort(int k[], int n);
int d[] = { 5,3,1 };
int main()
{
int k[] = {50,26,38,80,70,90,8,30,40,20};
Shell_Sort(k,10);
printf("The sorted result: \n");
for (int i = 0; i < 10; i++) {
printf("%d ", k);
}
printf("\n\n");
}
/*直接插入排序*/
void Insert_Sort(int k[], int n) {
int temp,i,j;
for (i = 1; i < n; i++) {
if (k < k) {
temp = k;
for (j = i - 1; temp < k && j>=0; j--) {
k = k;
}
k = temp;
}
}
}
/*折半插入排序*/
void Binary_Insert_Sort(int k[], int n) {
int middle;
int i, j,temp;
for (i = 1; i < n; i++) {
int low = 0;
int high = i - 1;
temp = k;
while (low <= high) {
middle = (low + high) / 2;
if (k < temp) {
low = middle + 1;
}
else if(k > temp) {
high = middle - 1;
}
else {
break;
}
}
for (j = i - 1; j > high; j--) {
k = k;
}
k = temp;
}
}
/*希尔排序*/
void Shell_Sort(int k[], int n) {
int i, j, l, temp;
for (i = 0; i < 3; i++) {
for (j = d; j < n; j++) {
if (k < k]) {
temp = k;
for (l = j - d; k > temp; l-=d) {
k] = k;
}
k] = temp;
}
}
}
}
页:
[1]