-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathselection-sort.cpp
More file actions
74 lines (57 loc) · 2.22 KB
/
selection-sort.cpp
File metadata and controls
74 lines (57 loc) · 2.22 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
66
67
68
69
70
71
72
73
74
# include "includeAll.h"
using namespace std;
// "Selection Sort" Sorting Algorithm Time Complexity O(N^2).
void sort (vector<int>& listOfElements) {
// The selection sort algorithm divides the list into two parts
// the subarray of already sorted elements which is built from left
// to right the sorted sublist is empty and the unsorted list is the entire list
// the algorithms finds the smallest element in the unordered subarray and swaps
// it wit the left most element in the array and then moves the subarray boundry
// one element to the right
// Select the minimun index in the sorted subarray
int minIndex = 0;
// Advance the position of the last item in the sorted subarray
// through the entire array
for (int i = 0; i < listOfElements.size()-1; ++i) {
// We assume the min element is the first element
minIndex = i;
// Find the min element by testing elemens after i
// in the unsorted subarray
for (int j = i+1; j < listOfElements.size(); ++j) {
// If the element is less, then it should become the new minimun
if (listOfElements[minIndex] > listOfElements[j])
minIndex = j;
}
swap (listOfElements[minIndex], listOfElements[i]);
// Print the list with the state of each iteration to simulate the bubble sort
// For the user
cout << "Iteration #" << i+1 << ":" ;
for (int k = 0; k < listOfElements.size(); ++k)
cout << listOfElements[k];
cout << endl;
}
}
int main () {
// Read and declare size of desired list by user
int sizeOfList;
cout << "Enter Size: " << endl;
cin >> sizeOfList;
// Declare vector to save list of elements
vector<int> listOfElements(sizeOfList);
// Receive list elements from the user and save them in the vector
cout << endl << "Enter elements: " << endl;
for (int i = 0; i < sizeOfList; ++i)
cin >> listOfElements[i];
// Print the list before sorting for the user
cout << endl << "Array before sorting: " << endl;
for (int i = 0; i < sizeOfList; ++i)
cout << listOfElements[i];
cout << endl << endl;
// Sort the elements in the list
sort (listOfElements);
// Print the list after the list has been sorted
cout << endl << "Array after sorting: " << endl;
for (int i = 0; i < listOfElements.size(); ++i)
cout << listOfElements[i];
return 0;
}