-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSelection_Sort.c
More file actions
48 lines (40 loc) · 900 Bytes
/
Selection_Sort.c
File metadata and controls
48 lines (40 loc) · 900 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
45
46
47
48
// libraries included
#include <stdio.h>
#include <vector>
// main function for sorting
void selection(vector<int> arr, int n){
int i, j, small;
for (i = 0; i < n - 1; i++){
small = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[small])
small = j;
int temp = arr[small];
arr[small] = arr[i];
arr[i] = temp;
}
}
int findPos(int arr[], int n, int ele){
for(int i = 0; i < n; i++) {
if(arr[i] == ele)
return i+1;
}
return -1;
}
// function to print the array
void printArr(int a[], int n){
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
// main function
int main(){
int a[] = { 12, 31, 25, 8, 32, 17 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are : \n");
printArr(a, n);
selection(a, n);
printf("\nAfter sorting array elements are : \n");
printArr(a, n);
return 0;
}