-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path023-SelectionSort.cpp
More file actions
59 lines (45 loc) · 838 Bytes
/
023-SelectionSort.cpp
File metadata and controls
59 lines (45 loc) · 838 Bytes
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <stdlib.h>
using std::cout;
using std::endl;
void SelectionSort(int list[], int size)
{
for (int i = 0; i < size; i++)
{
int min = i;
int j = i + 1;
for (; j < size; j++)
{
if (list[min] > list[j])
{
min = j;
}
}
int temp = list[i];
list[i] = list[min];
list[min] = temp;
}
}
int main()
{
srand(time(NULL));
int n = rand() % 10 + 1;
int list[n];
int i = 0;
cout << "size = " << n << endl;
while (i < n)
{
list[i] = rand() % 1000;
cout << list[i] << "\t";
i++;
}
cout << endl;
SelectionSort(list, n);
i = 0;
while (i < n)
{
cout << list[i] << "\t";
i++;
}
return 0;
}