2008秋-计算机软件基础-简单选择排序
//===========================================
// 简单选择排序
// Author:Eman Lee
//===========================================
#include<stdio.h>
#define N 10
void Display(int *a, int n)
{
int i;
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
void SelectionSort(int *a, int n)
{
int i, j, index, value;
for (i = 0; i < n - 1; i ++) {
index = i;
value = a[i];
for (j = i + 1; j < n; j ++)
if (value > a[j]) {
index = j;
value = a[j];
}
a[index] = a[i];
a[i] = value;
Display(a, n);
}
}
void main()
{
int a[N],i ;
for(i=0;i<N;i++)
a[i]=N-i;
Display(a, N);
SelectionSort(a, N);
}
// 简单选择排序
// Author:Eman Lee
//===========================================
#include<stdio.h>
#define N 10
void Display(int *a, int n)
{
int i;
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
void SelectionSort(int *a, int n)
{
int i, j, index, value;
for (i = 0; i < n - 1; i ++) {
index = i;
value = a[i];
for (j = i + 1; j < n; j ++)
if (value > a[j]) {
index = j;
value = a[j];
}
a[index] = a[i];
a[i] = value;
Display(a, n);
}
}
void main()
{
int a[N],i ;
for(i=0;i<N;i++)
a[i]=N-i;
Display(a, N);
SelectionSort(a, N);
}