-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectionsort.c
53 lines (40 loc) · 896 Bytes
/
selectionsort.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
51
52
53
#include<stdio.h>
void readArray(int arr[], int n) {
for( int i=0; i<n ; i++){
scanf("%d",&arr[i]);
}
}
void printArray( int arr[], int n ){
for(int i=0; i<n; i++ )
printf(" %d ",arr[i]);
}
void swap( int *p, int *q ){
int temp = *p;
*p = *q;
*q= temp;
}
void insertionSort( int arr[] , int n){
int i, j,min;
for( i=0; i<n-1; i++ ){
min = i;
for( j=i+1; j<n; j++ ){
if( arr[min]>arr[j]){
min=j;
}
}
swap( &arr[min], &arr[i] );
}
}
int main() {
int n, arr[10];
printf("Enter the size of the array:");
scanf("%d",&n);
printf("Enter the elements of the array:");
readArray( arr, n);
printf("Before Sorting:");
printArray( arr , n);
insertionSort( arr , n);
printf("\nAfter Sorting:");
printArray(arr , n);
return 0;
}