Merge Two Sorted Arrays | C Programming Example

Поделиться
HTML-код
  • Опубликовано: 23 янв 2025

Комментарии • 51

  • @PortfolioCourses
    @PortfolioCourses  2 года назад +2

    Want to see a *recursive* solution?
    Check out Merge Two Sorted Arrays Using Recursion: ruclips.net/video/fzW-qP4JdVI/видео.html

  • @xkisix
    @xkisix 2 года назад +14

    6:48 shouldn't it be b[ j ] instead of b[ i ] ?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +4

      I think you're right! I may have to re-do this video, thank you for the heads up.

  • @ClamChowderSoup
    @ClamChowderSoup 10 месяцев назад +2

    what happens if element in array A and array B are both equal? The else statement just assume that is never the case. This will break on special cases like these wouldn't it?

  • @Md.MohaiminulHassan
    @Md.MohaiminulHassan Год назад +3

    great explanation! thank you mr.

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      You’re very welcome, I’m glad you enjoyed it! :-)

  • @fab10barr0s
    @fab10barr0s 3 месяца назад

    This video is really good. But in order to work, both vectors need to be sorted first. In order to get all corner cases, I think that the ideal way to solve it is to just merge them and then sort the r vector internally. Also I think that the code itself would be easier.

  • @stone6326
    @stone6326 2 года назад +1

    Tried the same code here, but these two arrays kept printing the way they inisialised. Can't end up merging like the wsy it's supposed to.

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      The original code from this video is here: github.com/portfoliocourses/c-example-code/blob/main/merge_sort.c. It should work as it is written, if there is a bug I would like to know where so I can fix it, but I don't think so. If you post your code here in a comment maybe we can figure out what's going on.

  • @syronox5357
    @syronox5357 9 месяцев назад

    Can’t you just write the while loop with || (or) instead of && (and), and avoid the other while loops?

  • @sketchupguru
    @sketchupguru 2 года назад +1

    hi, is it possible to merge the two arrays first and then sort them using bubble sort?
    I was trying this out int ar3[] = array1[]+array2[];
    but it didn't work.
    I'm sure this is possible in python. Any thoughts on this?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +2

      Yes, it's possible. You could make an array that has a length equal to the sum of the length of the first array and the length of the second array. You could use a loop to store all the elements from the first array into the new array. And then another loop to store the elements from the second array into the new array AFTER the elements from the first array... this would likely involve using the first array's length as an "offset" into the new array when storing the elements there... something like this:
      for (int i = 0; i < length2; i++)
      newarray[i + length1] = array2[i];
      And then you could apply bubble sort to the new array. Doing this would be less efficient than merging the two sorted arrays as was done in this video, because a sorting algorithm will involve doing more work than this approach. :-)

  • @sketchupguru
    @sketchupguru 2 года назад

    Hi is it possible to make this with for loops instead of while loops?
    Or does it work only with while loops

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      You could do it with for-loops too. Anything you can do with a for loop you can do with a while loop and vice versa. 🙂

  • @prashansapran1602
    @prashansapran1602 2 года назад +1

    Very good explanation. Thank you.

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      You’re welcome Prashansa! :-) I’m glad to hear you enjoyed it.

  • @harisankar5620
    @harisankar5620 Год назад

    Heyy, which is this software? I have tried many sites to download the C software in macbook but it was unsuccessful

    • @PortfolioCourses
      @PortfolioCourses  Год назад

      I use visual studio code with the gcc compiler that comes from installing Xcode with developer tools. Technically it’s the clang compiler I believe, but it runs using “gcc”. :-)

  • @masum0z
    @masum0z 2 года назад

    What's the apps name that you use?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      Great question Mohammad! :-) In this video I am using Visual Studio Code to edit the code, and Terminal with the gcc compiler to compile the code. Terminal is the command-line for Mac OS.

    • @masum0z
      @masum0z 2 года назад

      Thanks

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      You're welcome Mohammad! :-)

  • @DeepakKumar-kw1fc
    @DeepakKumar-kw1fc 2 года назад

    How to do using recursion ?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      I will add this to my list of future video ideas Deepak. :-)

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      I made this video today, I hope you enjoy it: ruclips.net/video/fzW-qP4JdVI/видео.html. :-)

  • @Atrainn234
    @Atrainn234 2 года назад

    What if the two arrays are not sorted

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      Does the merged array still need to be sorted? If so, we could sort both arrays before merging them, using an algorithm such as one of these:
      Bubble Sort: studio.ruclips.net/user/videoYqzNgaFQEh8/edit
      Insertion Sort: studio.ruclips.net/user/videoTz7vBodZqo8/edit
      Selection Sort: studio.ruclips.net/user/videoYepJ7fDmyjI/edit
      Quicksort: studio.ruclips.net/user/video0jDiBM68NGU/edit
      Merge Sort: studio.ruclips.net/user/videoLeWuki7AQLo/edit

    • @Atrainn234
      @Atrainn234 2 года назад

      @@PortfolioCourses okay thank you, guessed as much

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      @@Atrainn234 You're welcome! :-D

  • @ionguzun3952
    @ionguzun3952 2 года назад

    thank u so much...can u do more videos about arrays and string algoritms

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      You’re welcome Ion! :-) And yes I’ll definitely do more string and array algorithms in the future. Is there any in particular that you would like to see?

    • @ionguzun3952
      @ionguzun3952 2 года назад

      @@PortfolioCourses if u can do more videos about problems from leetcode and applying brute force)) beacause i heard that every developer should now how to brute force first and after come with a better ideea ...and ur videos are very clear and well explained

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      @@ionguzun3952 OK interesting, "brute force" is usually not the best way to do something, so maybe in the videos I can show a brute force way and then a better way. Thanks for the thoughts I'll check out leetcode for some ideas.

  • @ivopoljak9386
    @ivopoljak9386 2 года назад

    very nice thank you for helping

  • @gouravchalotravlog
    @gouravchalotravlog 2 года назад

    arr[]= {2,4,6,8}
    Arr[]={1,3,4,5}
    This type of example not fixed in this code

    • @gouravchalotravlog
      @gouravchalotravlog 2 года назад

      Can you solve this problem if you have any idea sir to find the merger of it how we do it

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      @@gouravchalotravlog
      When I modify the code in this example here to use those two arrays, it works. The code in the example is found here: github.com/portfoliocourses/c-example-code/blob/main/merge_sorted_arrays.c.
      So here is the modified code:
      void merge(int a[], int m, int b[], int n, int r[]);
      int main(void)
      {
      // a1 and a2 are the sorted arrays, and we'll store the merged result into r
      int a1[] = {2,4,6,8};
      int a2[] = {1,3,4,5};
      int r[8];
      // merge the arrays
      merge(a1, 4, a2, 4, r);
      // output the result
      for (int i = 0; i < 8; i++)
      printf("r[%d] = %d
      ", i, r[i]);
      return 0;
      }
      // merges sorted arrays a and b of length m and n into result array r
      void merge(int a[], int m, int b[], int n, int r[])
      {
      // we use i to step through the elements of a, j to step through the elements
      // of b, and k to set the elements of r
      int i = 0, j = 0, k = 0;
      // we'll eventually reach the end of one of the arrays
      while (i < m && j < n)
      {
      // i and j keep track of what element we are looking at 'next' in a and b,
      // and so we check to see if the next element in a is less than the next
      // element in b, and if it is we set *that* as the next element of r and
      // increment the relevant counters
      if (a[i] < b[j])
      {
      r[k] = a[i];
      k++;
      i++;
      }
      // otherwise, we set the next element of b as the next element in r and
      // increment the relevant counters
      else
      {
      r[k] = b[j];
      k++;
      j++;
      }
      }
      // one of the two arrays will have at least one element remaining to be
      // stored into r... if i < m that means a has elements remaning and we store
      // those into r
      while (i < m)
      {
      r[k] = a[i];
      k++;
      i++;
      }
      // if j < n that means b has elements remaining and we store those into r
      while (j < n)
      {
      r[k] = b[i];
      k++;
      j++;
      }
      }

    • @gouravchalotravlog
      @gouravchalotravlog 2 года назад

      @@PortfolioCourses thanks 😊 for helping me

    • @gouravchalotravlog
      @gouravchalotravlog 2 года назад

      Given a sorted array arr[] consisting of N distinct integers and an integer K, the task is to find the index of K, if it’s present in the array arr[]. Otherwise, find the index where K must be inserted to keep the array sorted.

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      @@gouravchalotravlog That's an interesting question, I will try to make a video on it one day. :-)

  • @stonedcodingtom9097
    @stonedcodingtom9097 2 года назад

    Interesting program, thx

  • @lkz777
    @lkz777 2 месяца назад

    amazing

  • @n4cer252
    @n4cer252 2 года назад

    god send

  • @justcurious1940
    @justcurious1940 11 месяцев назад

    Nice : 🙃
    // return the merged array
    int *merge(int *array1, int m, int *array2, int n){
    int *merged = (int*)malloc(sizeof(int)*(m+n));
    int i = 0, j = 0, k = 0;
    while(i < m && j < n){
    if(array1[i] < array2[j]){
    merged[k++] = array1[i++];
    }
    else {
    merged[k++] = array2[j++];
    }
    }
    while(i < m){
    merged[k++] = array1[i++];
    }
    while(j < n){
    merged[k++] = array2[j++];
    }
    return merged;
    }