-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path# 34 - sortingOfArray.c
More file actions
52 lines (52 loc) · 1.39 KB
/
Copy path# 34 - sortingOfArray.c
File metadata and controls
52 lines (52 loc) · 1.39 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
// Program: To sort an array.
// Author: Dev Mehta
// Date: 30th July 2023.
#include<stdio.h>//this program is done using bubble sort method there are several method of doing it we will learn it in dsa see chat gpt.
int swap(int *arr , int *arr1){
int t = *arr;
*arr = *arr1;
*arr1 = t;
}
int asc(int *arr , int size){
for(int j = 0 ; j < size ; ++j ){
for(int h = j ; h < size ; ++h){
if(arr[j] < arr[h]){
swap(&arr[j],&arr[h]);
}
}
}
printf("Array in ascending order is: ");
for (int a = 0; a < size; ++a) {
printf("%d ", arr[a]);
}
}
int dsc(int *arr ,int size){
for(int j = 0 ; j < size ; ++j ){
for(int h = j ; h < size ; ++h){
if(arr[j] > arr[h]){
swap(&arr[j],&arr[h]);
}
}
}
printf("\nArray in descending order is: ");
for (int a = 0; a < size; ++a) {
printf("%d ", arr[a]);
}
}
int main(){
int size;
printf("Enter the size: ");
scanf("%d" , &size);
int arr[size];
printf("Enter the elements of an array: ");
for(int i = 0 ; i < size ; ++i){
scanf("%d" , &arr[i]);
}
printf("Array befrore rearrangment: ");
for (int d = 0; d < size; ++d) {
printf("%d ", arr[d]);
}
printf("\n");
asc(arr , size);
dsc(arr , size);
}