-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTasfik_selection_sort.cpp
53 lines (51 loc) · 1.42 KB
/
Tasfik_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
53
// Selection sort algorithm
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
// T -> O(N^2) | S -> O(1)
void selectionSort(int *numbers, int size, bool increasing = true)
{
/*
numbers : it contains the numbers to be sorted
size : total numbers in the numbers array
increasing : if true array will be ascending else descending
*/
//keep track of the biggest/smallest number
//and swap it with the last index
int bidx = -1;
for (int i = 0; i < size - 1; i++)
{
bidx = 0;
for (int j = 0; j < size - i; j++)
{
if (increasing && numbers[j] > numbers[bidx])
bidx = j;
else if (!increasing && numbers[j] < numbers[bidx])
bidx = j;
}
swap(numbers[size - i - 1], numbers[bidx]);
}
}
int main()
{
int totalNumbers;
//how many numbers you want to add
cout << "Enter the total numbers: " << endl;
cin >> totalNumbers;
int numbers[1005];
//take input the numbers
cout << "Insert the numbers: " << endl;
for (int i = 0; i < totalNumbers; i++)
{
cin >> numbers[i];
}
//helper function to sort the array
selectionSort(numbers, totalNumbers);
//print the sorted array
cout << "The numbers after being sorted: " << endl;
for (int i = 0; i < totalNumbers; i++)
{
cout << numbers[i] << " ";
}
return 0;
}