forked from ankit-kaushal/Lets-go-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort
More file actions
44 lines (34 loc) · 716 Bytes
/
SelectionSort
File metadata and controls
44 lines (34 loc) · 716 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
36
37
38
39
40
41
42
43
44
#include<bits/stdc++.h>
using namespace std;
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
void sort(int* arr, int n)
{
int i, j;
int minIndex = -1;
for (i = 0; i < n-1; i++)
{
minIndex = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[minIndex])
minIndex = j;
swap(&arr[minIndex], &arr[i]);
}
}
void printArray(int arr[], int size)
{
for (int i=0 ; i < size ; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = {80, 15, 19, 90, 11, 13, 32};
int n = sizeof(arr)/sizeof(arr[0]);
sort(arr, n);
printArray(arr, n);
}