-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exp10_SelectionSort
41 lines (39 loc) · 890 Bytes
/
Exp10_SelectionSort
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
//Srushti Patil IT-A-43
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int smallest(int arr[], int k, int n);
void selection_sort(int arr[], int n);
void main(int argc, char *argv[])
{
int arr[10], i, n;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter the elements of the array: ");
for(i=0;i<n;i++) { scanf("%d", &arr[i]); }
selection_sort(arr, n);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++) printf(" %d\t", arr[i]);
}
int smallest(int arr[], int k, int n)
{ int pos = k, small=arr[k], i;
for(i=k+1;i<n;i++)
{
if(arr[i]< small)
{ small = arr[i]; pos = i; }
}
return pos;
}
void selection_sort(int arr[],int n)
{
int k,
pos,
temp;
for(k=0;k<n;k++)
{
pos = smallest(arr, k, n);
temp = arr[k];
arr[k] = arr[pos];
arr[pos] = temp;
}
}