forked from sambhav2612/SortingAlgorithms.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection-sort.cpp
More file actions
44 lines (33 loc) · 973 Bytes
/
selection-sort.cpp
File metadata and controls
44 lines (33 loc) · 973 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 "includeAll.h"
using namespace std;
void sort (int array[], int size) {
int minIndex = 0;
// moving the boundary one by one of unsorted subarray
for (int i = 0; i < size-1; ++i) {
// Finding the minimum element in unsorted array
minIndex = i;
for (int j = i+1; j < size; ++j) {
if (array[minIndex] > array[j])
minIndex = j;
}
// Swap the smallest found element with the ith element
swap (&array[minIndex], &array[i]);
}
}
int main () {
int size = 0, array[100] = { 0 };
cout << endl << "Enter Size: ";
cin >> size;
cout << endl << "Enter elements: " << endl;
for (int i = 0; i < size; ++i)
cin >> array[i];
cout << endl << "Array before sorting: " << endl;
for (int i = 0; i < size; ++i)
cout << array[i];
// Calling the selection sort function the sort the given array
sort (array, size);
cout << endl << "Array after sorting: " << endl;
for (int i = 0; i < size; ++i)
cout << array[i];
return 0;
}