2008秋-计算机软件基础-直接插入排序
//==============================================
// 直接插入排序(Straight Selection Sort)
// Author: eman lee
// 直接插入排序 c语言源程序
//==============================================
#define N 5
#include<stdio.h>
//显示元素
void Display(int a[], int elementCount)
{
int i;
for (i = 0; i < elementCount; i++)
{
printf(" %d ", a[i]);
}
printf("\n");
}
//直接插入排序
void StraightSelectionSort(int a[], int elementCount)
{
int i, j, index, value;
for (i = 0; i < elementCount - 1; i ++)
{
index = i;
value = a[i];
for (j = i + 1; j < elementCount; j ++)
if (value > a[j])
{
index = j;
value = a[j];
}
a[index] = a[i];
a[i] = value;
Display(a, elementCount);
}
}
void main()
{
int a[N] = {5, 4, 3, 2, 1};
printf("原始状态\n");
Display(a, N);
printf("各趟排序之后\n");
StraightSelectionSort(a, N);
}
// 直接插入排序(Straight Selection Sort)
// Author: eman lee
// 直接插入排序 c语言源程序
//==============================================
#define N 5
#include<stdio.h>
//显示元素
void Display(int a[], int elementCount)
{
int i;
for (i = 0; i < elementCount; i++)
{
printf(" %d ", a[i]);
}
printf("\n");
}
//直接插入排序
void StraightSelectionSort(int a[], int elementCount)
{
int i, j, index, value;
for (i = 0; i < elementCount - 1; i ++)
{
index = i;
value = a[i];
for (j = i + 1; j < elementCount; j ++)
if (value > a[j])
{
index = j;
value = a[j];
}
a[index] = a[i];
a[i] = value;
Display(a, elementCount);
}
}
void main()
{
int a[N] = {5, 4, 3, 2, 1};
printf("原始状态\n");
Display(a, N);
printf("各趟排序之后\n");
StraightSelectionSort(a, N);
}