-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.c
More file actions
49 lines (43 loc) · 764 Bytes
/
SelectionSort.c
File metadata and controls
49 lines (43 loc) · 764 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
49
#include <stdio.h>
#include <stdlib.h>
void display(int*, int);
void selectionSort(int *, int);
void swap(int* , int*);
int count = 0;
int main() {
int n;
int *a;
printf("Enter the number of elements: ");
scanf("%d",&n);
a = (int *)malloc(n * sizeof(int));
for(int i = 0; i < n; i++) {
scanf("%d",&a[i]);
}
display(a,n);
selectionSort(a,n);
display(a,n);
printf("%d\n",count);
}
void display(int *a, int n) {
for(int i = 0; i < n; i++) {
printf("%d ",a[i]);
}
printf("\n");
}
void selectionSort(int *a, int n) {
for(int i = 0; i < n; i++) {
int min = i;
for(int j = i+1; j < n; j++){
if(a[j] < a[min]) {
min = j;
}
}
swap(&a[min], &a[i]);
}
}
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
count++;
}