-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5.SelectionSort.cpp
46 lines (39 loc) · 999 Bytes
/
5.SelectionSort.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
/*
* This program takes an array from the user
* sorts it using selection sort
* Prints the sorted array
* For more information about Insertion Sort Algorithm: https://en.wikipedia.org/wiki/Selection_sort
*
* Coded by: Abdurrezak EFE
*
* */
#include <iostream>
#include <algorithm>
using namespace std;
void selection_sort(int arr[],int k) //insertion sort function
{
for(int i=0;i<k;i++)
{
int smallest=999999999;
int smallestIndex=-1;
for(int j=i;j<k;j++)
{
if(arr[j]<=smallest)
smallest = arr[j],smallestIndex=j;
arr[smallestIndex] = arr[i];
arr[i] = smallest;
}
}
}
int main()
{
int k;
cout << "Enter the number of the integers you want to construct the array from: ";
cin >> k;
int arr[k];
for(int i=0;i<k;i++)
cin >> arr[i];
selection_sort(arr,k); //sorting
for(int i=0;i<k;i++) //printing the array
cout << arr[i] << " ";
}