Skip to content

Instantly share code, notes, and snippets.

@sumeet
Created April 17, 2022 21:59
Show Gist options
  • Select an option

  • Save sumeet/bc95a3944c4a4ee17eb85ac0b1b95251 to your computer and use it in GitHub Desktop.

Select an option

Save sumeet/bc95a3944c4a4ee17eb85ac0b1b95251 to your computer and use it in GitHub Desktop.
#include <stdio.h>
// [a b c d]
// [a a a b c]
void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){
static int nums1_copy[200];
memcpy(nums1_copy, nums1, m * sizeof(int));
int input_cursor = 0;
int i = 0;
int j = 0;
while(1) {
if ((i < m) && (j < n)) {
if (nums1_copy[i] < nums2[j]) {
nums1[input_cursor++] = nums1_copy[i++];
} else {
nums1[input_cursor++] = nums2[j++];
}
} else if (i < m) {
nums1[input_cursor++] = nums1_copy[i++];
} else if (j < n) {
nums1[input_cursor++] = nums2[j++];
} else {
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment