// insert_sort.cpp : Defines the entry point for the console application.
// 直接插入排序:从小到大排序
#include "stdafx.h"
void insert_sort(int *array,const int n)
{
int j,k,s;
int temp;
for(j = 0;j < n;j++)
{
k = 0;
s = j;
for(k = 0;k < s; k++) //j
{
if(array[k] > array[s])
{
temp = array[s]; //save end data
printf("%d\n",temp);
while(s > k)
{
printf("%d %d %d %d\n",k,s,array[s-1],array[s]);
array[s] = array[s-1];
s--;
}
array[k] = temp;
}
}
}
for(int q=0;q < n;q++)
printf("%d ",array[q]);
}
#define N 10
int _tmain(int argc, _TCHAR* argv[])
{
int array[N] = {0};
for(int i = 0;i < N;i++)
scanf("%d ",&array[i]);
insert_sort(array,N);
return 0;
}
// 直接插入排序:从小到大排序
#include "stdafx.h"
void insert_sort(int *array,const int n)
{
int j,k,s;
int temp;
for(j = 0;j < n;j++)
{
k = 0;
s = j;
for(k = 0;k < s; k++) //j
{
if(array[k] > array[s])
{
temp = array[s]; //save end data
printf("%d\n",temp);
while(s > k)
{
printf("%d %d %d %d\n",k,s,array[s-1],array[s]);
array[s] = array[s-1];
s--;
}
array[k] = temp;
}
}
}
for(int q=0;q < n;q++)
printf("%d ",array[q]);
}
#define N 10
int _tmain(int argc, _TCHAR* argv[])
{
int array[N] = {0};
for(int i = 0;i < N;i++)
scanf("%d ",&array[i]);
insert_sort(array,N);
return 0;
}