-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexercise2.c
46 lines (38 loc) · 971 Bytes
/
exercise2.c
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
//documentation section
/* Exercise 2 - Selection Sort*/
//pre-processor section
#include<stdio.h>
//global variable section
void swap(int* xp, int* yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
}
}
//main function section
int main(){
//write here your program
int i;
int UA[] = {5,1,4,2,8};
int n = sizeof(UA) / sizeof(UA[0]);
selectionSort(UA, n);
printf("Sorted Element using Selection Sort Technique is: ");
for (i = 0; i < n; i++)
printf("%d ", UA[i]);
return 0;
}