-
Notifications
You must be signed in to change notification settings - Fork 18
/
mergearrayascending.c
37 lines (31 loc) · 965 Bytes
/
mergearrayascending.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
#include <stdio.h>
int main()
{
//Initialize array
int arr[] = {5, 2, 8, 7, 1};
int temp = 0;
//Calculate length of array arr
int length = sizeof(arr)/sizeof(arr[0]);
//Displaying elements of original array
printf("Elements of original array: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
//Sort the array in ascending order
for (int i = 0; i < length; i++) {
for (int j = i+1; j < length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("\n");
//Displaying elements of array after sorting
printf("Elements of array sorted in ascending order: \n");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
return 0;
}