-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
65 lines (60 loc) · 1.37 KB
/
Copy pathmain.cpp
File metadata and controls
65 lines (60 loc) · 1.37 KB
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
60
61
62
63
64
65
#include <iostream>
using namespace std;
/*!
* \brief selectionSort Sorts the array using the selection sort algorithm
* \param arr Array of integers to sort
* \param n Length of the array
*/
void selectionSort(int* arr, int n);
/*!
* \brief swap Swaps two elements in the array
* \param arr Array to perform the operation on
* \param a Index of the first element
* \param b Index of the second element
*/
void swap(int* arr, int a, int b);
void printArray(int *arr, int n);
int main()
{
int arr[12] = {12, 2, 54, 1, 3, 4, 16, 13, 10, 24, 22, 30};
printArray(arr, 12);
selectionSort(arr, 12);
printArray(arr, 12);
return 0;
}
void selectionSort(int *arr, int n)
{
for (int i = 0; i < n; i++)
{
//range i -> n defines the sub-array to find minimum within
int min = arr[i];
int minIdx = i;
for (int j = i; j < n; j++)
{
//find the minimum
if (arr[j] < min)
{
min = arr[j];
minIdx = j;
}
}
//swap it with subarray's beginning
swap(arr, i, minIdx);
}
}
void swap(int* arr, int a, int b)
{
int tmp = arr[a];
arr[a] = arr[b];
arr[b] = tmp;
}
void printArray(int *arr, int n)
{
for (int i = 0; i < n; i++)
{
if (i)
cout << ", ";
cout << arr[i];
}
cout << endl;
}