forked from anish-mohanty/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergesorted.c
More file actions
44 lines (39 loc) · 1.09 KB
/
mergesorted.c
File metadata and controls
44 lines (39 loc) · 1.09 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
// C program to merge two sorted arrays with O(1) extra
// space.
#include <stdio.h>
// Merge ar1[] and ar2[] with O(1) extra space
void merge(int ar1[], int ar2[], int m, int n)
{
// Iterate through all elements
// of ar2[] starting from the last element
for (int i = n - 1; i >= 0; i--) {
// Find the smallest element greater than ar2[i].
// Move all elements one position ahead till the
// smallest greater element is not found */
int j, last = ar1[m - 1];
for (j = m - 2; j >= 0 && ar1[j] > ar2[i]; j--)
ar1[j + 1] = ar1[j];
// If there was a greater element
if (last > ar2[i]) {
ar1[j + 1] = ar2[i];
ar2[i] = last;
}
}
}
// Driver program
int main()
{
int ar1[] = { 1, 5, 9, 10, 15, 20 };
int ar2[] = { 2, 3, 8, 13 };
int m = sizeof(ar1) / sizeof(ar1[0]);
int n = sizeof(ar2) / sizeof(ar2[0]);
merge(ar1, ar2, m, n);
printf("After Merging \nFirst Array: ");
for (int i = 0; i < m; i++)
printf("%d ", ar1[i]);
printf("\nSecond Array: ");
for (int i = 0; i < n; i++)
printf("%d ", ar2[i]);
return 0;
}
// This code is contributed by Aditya Kumar (adityakumar129)