-
Notifications
You must be signed in to change notification settings - Fork 59
/
selectionsort.cpp
58 lines (50 loc) · 987 Bytes
/
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
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n - 1; i++)
{
min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
if (min_idx != i)
swap(&arr[min_idx], &arr[i]);
}
}
void display(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int get()
{
int size;
printf("Enter Size of Array : ");
scanf("%d", &size);
int array[size];
printf("Enter Elements of Array\n");
for (int i = 0; i < size; i++)
{
scanf("%d", &array[i]);
}
printf("Given Array is\n");
display(array, size);
selectionSort(array, size);
cout << "\nSorted Array : ";
display(array, size);
return 0;
}
int main()
{
get();
}