-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselectionSort.c
More file actions
44 lines (38 loc) · 916 Bytes
/
selectionSort.c
File metadata and controls
44 lines (38 loc) · 916 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
#include<stdio.h>
void swap(int array[], int i, int p){
int temp = array[i];
array[i] = array[p];
array[p]= temp;
}
void selectionSort(int array[], int arraySize){
for(int i=0; i<(arraySize-1);i++){
int p =i;
for(int j=i+1; j < arraySize; j++){
if(array[p]>array[j]){
p=j;
}
}
if(p!=i){
swap(array, i, p);
}
}
}
void printArray(int array[], int size){
printf("Here is the sorrted array: ");
for(int i = 0; i < size; i++){
printf("%d ",array[i]);
}
}
int main(){
int arraySize;
printf("Enter the array size: ");
scanf("%d",&arraySize);
int data[arraySize];
printf("Enter the array elements: ");
for(int i=0;i<arraySize;i++){
scanf("%d",&data[i]);
}
selectionSort(data, arraySize);
printArray(data, arraySize);
}
//arifulislamat@gmail.com