排序算法(1)--选择排序

时间:2022-10-11 22:08:05
选择排序较为简单,复杂度O(n^2),不稳定排序算法,实现如下:
#include <stdio.h>
#define MAX 100

int main()
{
int i, j, temp, n, index;
int a[MAX];

printf("Input n:\n");
scanf("%d", &n);

for(i = 0; i < n; i++)
scanf("%d", &a[i]);

//sort, low->high
for(i = 0; i < n; i++){
index = n - i - 1;
for(j = 0; j < n - i; j++){
if(a[j] > a[index])
index = j;
}
temp = a[index];
a[index] = a[n - i - 1];
a[n - i - 1] = temp;
}

//print
for(i = 0; i < n; i++)
printf("%d ", a[i]);
putchar('\n');

return 0;
}