排序:插入排序及希尔排序
代码
#include <stdio.h>
#define SIZE 11
void showarr(int[],int);
void insertsort(int [],int);
void insertsortV2(int [],int);
void shellsort(int [],int);
int main(void){
int arr[SIZE] = {0,10,4,7,2,1,8,32,20,80,6};
//insertsortV2(arr,SIZE);
//showarr(arr,SIZE);
shellsort(arr,SIZE);
return 0;
}
void showarr(int arr[],int size){
int i;
for(i=1;i<size;i++){
printf("%d,",arr[i]);
if(i == size -1){
printf("\n");
}
}
}
void shellsort(int arr[],int size){
int n,i,j;
printf("please input n:\n");
while(scanf("%d",&n) == 1 ){
if((n+1)<size){
for(i=n+1;i<size;i++){
arr[0] = arr[i];
j = i - n;
while((arr[0] < arr[j]) && j>0){
arr[j+n] = arr[j];
arr[j] = arr[0];
j = j-n;
}
}
//Show one times
showarr(arr,size);
}else{
printf("n > size\n");
printf("please input n:\n");
}
}
}
void insertsortV2(int arr[],int size){
int i,j;
for(i = 2;i<size;i++){
arr[0] = arr[i];
j = i-1;
while(arr[0] < arr[j]){
arr[j+1] = arr[j];
arr[j] = arr[0];
j--;
}
}
}
void insertsort(int arr[],int size){
int i,j;
int temp;
for(i=1;i<size;i++){
temp = arr[i];
j = i-1;
while(temp<arr[j] && j >=0){
arr[j+1] = arr[j];
arr[j] = temp;
j--;
}
}
}
#define SIZE 11
void showarr(int[],int);
void insertsort(int [],int);
void insertsortV2(int [],int);
void shellsort(int [],int);
int main(void){
int arr[SIZE] = {0,10,4,7,2,1,8,32,20,80,6};
//insertsortV2(arr,SIZE);
//showarr(arr,SIZE);
shellsort(arr,SIZE);
return 0;
}
void showarr(int arr[],int size){
int i;
for(i=1;i<size;i++){
printf("%d,",arr[i]);
if(i == size -1){
printf("\n");
}
}
}
void shellsort(int arr[],int size){
int n,i,j;
printf("please input n:\n");
while(scanf("%d",&n) == 1 ){
if((n+1)<size){
for(i=n+1;i<size;i++){
arr[0] = arr[i];
j = i - n;
while((arr[0] < arr[j]) && j>0){
arr[j+n] = arr[j];
arr[j] = arr[0];
j = j-n;
}
}
//Show one times
showarr(arr,size);
}else{
printf("n > size\n");
printf("please input n:\n");
}
}
}
void insertsortV2(int arr[],int size){
int i,j;
for(i = 2;i<size;i++){
arr[0] = arr[i];
j = i-1;
while(arr[0] < arr[j]){
arr[j+1] = arr[j];
arr[j] = arr[0];
j--;
}
}
}
void insertsort(int arr[],int size){
int i,j;
int temp;
for(i=1;i<size;i++){
temp = arr[i];
j = i-1;
while(temp<arr[j] && j >=0){
arr[j+1] = arr[j];
arr[j] = temp;
j--;
}
}
}