0%

Algorithm-选择排序

C语言实现的选择排序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
 
void SelectionSort(int *a, int n);
 
int main()
{
    int a[6] = {3, 6, 4, 2, 1, 5};
    int i;
    for (i = 0; i < 6; i++)
        printf("a[%d]=%d ", i, a[i]);
    printf("\n");
    SelectionSort(a, 6);
    for (i = 0; i < 6; i++)
        printf("a[%d]=%d ", i, a[i]);
    printf("\n");
    return 0;
}
 
void SelectionSort(int *a, int n)
{
    int i, j;
    int tmp;
    int t;
    for (i = 0; i < n - 1; i++)
    {
        tmp = i;
        for (j = i + 1; j < n; j++)
        {
            if (a[j] < a[tmp])
                tmp = j;
        }
        t = a[tmp];
        a[tmp] = a[i];
        a[i] = t;
    }
}

Welcome to my other publishing channels