-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectionSorting.cpp
53 lines (42 loc) · 1.47 KB
/
selectionSorting.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
// A program to illustrate selection sorting written in cpp.
// You can simply run the code from the terminal(OSX and Ubuntu):
// $ g++ selectionSorting.cpp -o output
// $ ./output
// then, the prompt will ask the number of elements and the elements themselves.
#include<iostream>
using namespace std;
int main() {
// Taking the size of array as input.
int length;
cout<<"\nEnter the length of your array : ";
cin>>length;
// Taking the elements of the array as input.
int arr[length];
cout<<"\nEnter the numbers you want to insert into your array:\n";
for(int i=0; i < length; i++)
{
cin>>arr[i];
}
// Sorting Operation Begins.
for (int i = 0; i < length; i++) //The outer loop travels through all the array.
{
int smallest = i; //smallest variable represents lastly located sorted element.
for (int j = i + 1; j < length; j++) //traveling through unordered tail of the array
{
if(arr[j] < arr[smallest])
{
smallest = j; //comparing all the elements of unordered tail with lastly added ordered element.
}
// Swapping Operation
int temp = arr[i];
arr[i] = arr[smallest];
arr[smallest] = temp;
}
}
cout<<"\n Sorting Operation Completed!\n The sorted array is : \n";
for(int k=0; k<length; k++)
{
cout<< arr[k] << "\n";
}
return 0;
}