直接选择排序

直接选择排序

#include <iostream>
using namespace std;

#define N 10

void SimpleSelectionSort(int *a, int n)
{
int i, j, min_i, t;
for (i = 0; i < n - 1; i++)
{
min_i = i;
for (j = i + 1; j < n; j++)
{
if (a[min_i] > a[j])
{
min_i = j;
}
}
if (min_i != i)
{
t = a[i];
a[i] = a[min_i];
a[min_i] = t;
}
}
}

int main()
{
int *a;
a = new int[N];

for (int i = 0; i < N; i++)
{
cin >> a[i];
}

SimpleSelectionSort(a, N);

for (int i = 0; i < N; i++)
{
cout << a[i] << " ";
}
cout << endl;
return 0;
}
  • 时间复杂度: O(pow(n,2))

测试

[blazehu@MacBook ~]$ g++ -o SimpleSelectionSort SimpleSelectionSort.cpp
[blazehu@MacBook ~]$ ./SimpleSelectionSort
5
6
4
7
3
8
9
2
1
0
0 1 2 3 4 5 6 7 8 9