-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection.c
50 lines (47 loc) · 1 KB
/
selection.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
47
48
49
50
#include <stdio.h>
int ss=0;
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void selectionSort(int array[], int size,int k) {
for (int step = 0; step < size - 1; step++) {
if(k==step){
printf("Array unsorted till %d pass\n",k);
printArray(array,size);
}
int min_idx = step;
for (int i = step + 1; i < size; i++) {
if (array[i] < array[min_idx])
min_idx = i;
ss++;
}
swap(&array[min_idx], &array[step]);
}
}
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}
int main() {
int size;
int data[50],k;
printf("Enter the size of array: ");
scanf("%d",&size);
printf("Elements in the array are : ");
for(int i=0;i<size;i++)
{
scanf("%d",&data[i]);
}
printf("Enter the value of k: ");
scanf("%d",&k);
selectionSort(data, size,k);
printf("Sorted array in ascending order:\n");
printArray(data, size);
printf("No of step count: %d",ss);
printf("\n");
return 0;
}