-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection.cpp
More file actions
74 lines (61 loc) · 1.66 KB
/
Selection.cpp
File metadata and controls
74 lines (61 loc) · 1.66 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 <iostream>
using namespace std;
void printArray();
void swap(int a, int b);
void selectionSort();
const int arrSize = 10;
int arr[arrSize];
int main()
{
// Filling the array with random numbers...
for (int i = 0; i < arrSize; i++)
arr[i] = rand() % 100;
cout << "Array Before Sorting:\n";
printArray();
cout << "\nSorting...\n";
selectionSort();
cout << "\nArray After Sorting:\n";
printArray();
return 0;
}
void printArray()
{
for (int i = 0; i < arrSize; i++)
cout << arr[i] << "\t";
cout << "\n";
}
void swap(int a, int b)
{
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
void selectionSort()
{
/**
* This outer for loop iterates over the entire array.
* For each iteration, it assumes the first element of the unsorted part is the smallest.
* (Initially, the unsorted part is the whole array, but after each iteration the size of the unsorted part decreases by one)
*/
for (int i = 0; i < arrSize; i++)
{
/**
* The minPos variable is initialized to hold the position of the minimum element.
*/
int minPos = i;
/**
* This inner for loop starts from the current position i and iterates until the end of the array.
* This loop is used to find the actual minimum element in the unsorted part of the array.
* If a smaller element than the current minimum is found, minPos is updated to the index of this new minimum.
*/
for (int j = i; j < arrSize; j++)
{
if (arr[j] < arr[minPos])
minPos = j;
}
/**
* The swap function is called to swap the found minimum element with the first element of the unsorted part.
*/
swap(minPos, i);
}
}