-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.c
More file actions
71 lines (54 loc) · 1.16 KB
/
Copy pathSelectionSort.c
File metadata and controls
71 lines (54 loc) · 1.16 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdlib.h>
void display(int*,int);
void input(int*,int);
int MIN(int *,int,int);
void selectionSort(int*,int);
int main(void){
int size=0;
printf("Input the size :\n");
scanf("%d",&size);
int *p;
p=(int*)malloc(sizeof(int)*size);
input(p,size);
selectionSort(p,size);
display(p,size);
free(p);
}
void display(int *array,int size){
printf("The sorted array is :\n");
for (size_t i = 0; i < size; i++)
{
printf("%d ",*(array+i));
}
}
void input(int *array,int size){
printf("Input the values :\n");
for (size_t i = 0; i < size; i++)
{
scanf("%d",array+i);
}
}
void swap(int *array,int x,int y){
int temp;
temp=*(array+x);
*(array+x)=*(array+y);
*(array+y)=temp;
}
int MIN(int *array,int start,int end){
int min=*(array+start);
int index=start;
int i=start;
for(i;i<end;++i){
if(*(array+i)<min){
min=*(array+i);
index=i;
}
}
return index;
}
void selectionSort(int *array,int size){
for(int i=0;i<size-1;++i){
swap(array,MIN(array,i,size),i);
}
}