forked from manavdoda7/CPP-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection Sort.cpp
More file actions
35 lines (34 loc) · 745 Bytes
/
Selection Sort.cpp
File metadata and controls
35 lines (34 loc) · 745 Bytes
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
#include <bits/stdc++.h>
using namespace std;
// function for selection sort
void selectionSort(int arr[], int n)
{
int i, j, min;
for (i = 0; i < n-1; i++)
{
// Finding the smallest element in array
min = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min])
min = j;
// swap the minimum element and first element of unsorted subarray
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = {23, 56, 9, 103, 77};
int n = 5;
selectionSort(arr, n);
printArray(arr, n);
return 0;
}