-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathselection_sort2.cpp
More file actions
44 lines (36 loc) 路 807 Bytes
/
Copy pathselection_sort2.cpp
File metadata and controls
44 lines (36 loc) 路 807 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
#include <iostream>
using namespace std;
void selectionSort(int arr[], int size)
{
for (int i = 0; i < size - 1; ++i) // Last element will be automatically sorted
{
int maxIndex = i;
for (int j = i + 1; j < size; ++j)
{
if (arr[j] > arr[maxIndex])
{
maxIndex = j;
}
}
swap(arr[maxIndex], arr[i]);
}
}
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int arr[] = {64, 10, 2, 22, 11};
int size = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
printArray(arr, size);
selectionSort(arr, size);
cout << "Sorted array: ";
printArray(arr, size);
return 0;
}