-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path17.cpp
More file actions
60 lines (48 loc) · 1.48 KB
/
Copy path17.cpp
File metadata and controls
60 lines (48 loc) · 1.48 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
// Selection Sort
#include <iostream>
using namespace std;
void selectionsort(int a[], int lb, int ub)
{
int i, j, n, minindex;
n = ub - lb + 1; //range: calculates the total count of elements within the given boundary [lb, ub]
for (i = 0; i < n - 1; i++) //Outer loop running from the first element up to the second-to-last element (n - 2).
{
minindex = i; //minindex stores min. index; temporarily assumes the first element of the unsorted segment is the minimum.
for (j = i + 1; j < n; j++) //Inner loop scanning the remaining unsorted elements starting at i + 1.
{
if (a[lb + j] < a[lb + minindex])
{
minindex = j;
}
}
swap(a[lb + minindex], a[lb + i]);
}
}
int main()
{
int a[20], n, i;
cout << "Enter the no. of array elements: ";
cin >> n;
cout << "Enter elements:\n";
for (i = 0; i < n; i++)
{
cout << "Element " << i + 1 << ": ";
cin >> a[i];
}
cout << "\nUnsorted Array:\n";
cout << "[";
for (i = 0; i < n; i++)
{
cout << " " << a[i];
}
cout << " ]\n";
selectionsort(a, 0, n - 1); //Calls the sorting function on array a with lower bound 0 and upper bound n - 1.
cout << "\nSorted Array:\n";
cout << "[";
for (i = 0; i < n; i++)
{
cout << " " << a[i];
}
cout << " ]\n";
return 0;
}