-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection sort function.c
More file actions
72 lines (69 loc) · 1.89 KB
/
Copy pathSelection sort function.c
File metadata and controls
72 lines (69 loc) · 1.89 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
72
// writing a c programe to sort the array in both ascending and descending order:
#include<stdio.h>
//function declaration
void ascend(int a[], int n);
void descend(int a[], int n);
int main(){
int n, opt;
printf("Enter the no. of elements to be added: \n ");
scanf("%d",&n);
// Entering the elements in the array :
int a[n];
printf("Enter the %d elements in the array : \n",n);
for(int i=0; i<n;i++){
scanf("%d",&a[i]);
}
// If else code for options:
printf(" Kindly press 1 for ascending and press 2 for descending: \n");
scanf("%d",&opt);
if(opt==1){
printf("The sorted ascending order is :- \n");
ascend(a,n);
}
else if(opt == 2){
printf("The sorted descending order is :- \n");
descend(a,n);
}
else{
printf("Kindly read the instruction carefully and then choose : \n");
}
return 0;
}
void ascend(int a[], int n){
int swap,small;
for(int i=0 ; i< n-1 ; i++){
small = i ;
for(int j=i+1; j<n ;j++){
if(a[j]<a[small]){
small = j ;
}
}
swap= a[i];
a[i] =a[small];
a[small]= swap;
}
//Printing the array sorted in an order :-
printf("Printing the sorted array in ascending order: \n ");
for(int i=0 ; i<n ; i++){
printf("%d \t",a[i]);
}
}
void descend(int a[], int n){
int swap,small;
for(int i=0 ; i< n-1 ; i++){
small = i ;
for(int j=i+1; j<n ;j++){
if(a[j]>a[small]){
small = j ;
}
}
swap= a[i];
a[i] =a[small];
a[small]= swap;
}
//Printing the array sorted in an order :-
printf("Printing the sorted array in descending order: \n ");
for(int i=0 ; i<n ; i++){
printf("%d \t",a[i]);
}
}