-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection_sort.cpp
52 lines (48 loc) · 1021 Bytes
/
selection_sort.cpp
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
#include <iostream>
using namespace std;
void selectionSort(int array[], int n)
{
for (int i = 0; i < n - 1; i++)
{
int min = i;
for (int j = i + 1; j < n; j++)
{
if (array[j] < array[min])
{
min = j;
}
if (min != i)
{
swap(array[i], array[min]);
// int temp = array[min];
// array[min] = array[i];
// array[i] = temp;
}
}
}
}
void printArray(int array[], int n)
{
for (int i = 0; i < n; i++)
{
cout << array[i] << " ";
}
}
int main()
{
int n;
cout << "Input array size: ";
cin >> n;
int array[n];
cout << "Input array element: ";
for (int i = 0; i < n; i++)
{
cin >> array[i];
}
selectionSort(array, n);
cout << "Sorted array: ";
printArray(array, n);
cout << endl;
main();
return 0;
}