插入排序
#include <stdio.h>
typedef int ElementType;
void InsertionSort(ElementType X[],int N); //插入排序
int main(void)
{
int i;
int b[20] = {12,4,49,26,34,78,51,3,15,16,4,1,4,51,34};
InsertionSort(b,20);
for( i = 0; i < 20; i++)
printf("%-4d",b[i]);
printf("\nHello World!\n");
return 0;
}
void InsertionSort(ElementType A[],int N) //插入排序思想
{
int j,P;
ElementType Tmp;
for( P = 1; P < N; P++) //从第一个一直往后***r> {
Tmp = A[P];
for( j = P; j > 0 && A[j - 1] > Tmp; j--) //假设前N-1个已经有序,从第N个开始往前找
{ //如果该位置不是要找的位置,该位置向后移
A[j] = A[j - 1];
}
A[j] = Tmp; //找到该位置
}
}
typedef int ElementType;
void InsertionSort(ElementType X[],int N); //插入排序
int main(void)
{
int i;
int b[20] = {12,4,49,26,34,78,51,3,15,16,4,1,4,51,34};
InsertionSort(b,20);
for( i = 0; i < 20; i++)
printf("%-4d",b[i]);
printf("\nHello World!\n");
return 0;
}
void InsertionSort(ElementType A[],int N) //插入排序思想
{
int j,P;
ElementType Tmp;
for( P = 1; P < N; P++) //从第一个一直往后***r> {
Tmp = A[P];
for( j = P; j > 0 && A[j - 1] > Tmp; j--) //假设前N-1个已经有序,从第N个开始往前找
{ //如果该位置不是要找的位置,该位置向后移
A[j] = A[j - 1];
}
A[j] = Tmp; //找到该位置
}
}