Merge Two Sorted Arrays | C Programming Example

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ส.ค. 2024
  • An example of merging two sorted arrays in C. Source code: github.com/por.... Check out www.portfolioc... to build a portfolio that will impress employers!

ความคิดเห็น • 49

  • @PortfolioCourses
    @PortfolioCourses  2 ปีที่แล้ว +2

    Want to see a *recursive* solution?
    Check out Merge Two Sorted Arrays Using Recursion: th-cam.com/video/fzW-qP4JdVI/w-d-xo.html

  • @ClamChowderSoup
    @ClamChowderSoup 5 หลายเดือนก่อน +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?

  • @xkisix
    @xkisix 2 ปีที่แล้ว +9

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

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว +1

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

  • @user-zr3sz5ux5l
    @user-zr3sz5ux5l ปีที่แล้ว +3

    great explanation! thank you mr.

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

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

  • @prashansapran1602
    @prashansapran1602 2 ปีที่แล้ว +1

    Very good explanation. Thank you.

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว

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

  • @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 4 หลายเดือนก่อน

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

  • @harisankar5620
    @harisankar5620 7 หลายเดือนก่อน

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

    • @PortfolioCourses
      @PortfolioCourses  7 หลายเดือนก่อน

      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”. :-)

  • @SketchupGuru
    @SketchupGuru ปีที่แล้ว

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

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

      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. 🙂

  • @ivopoljak9386
    @ivopoljak9386 ปีที่แล้ว

    very nice thank you for helping

  • @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.

  • @stonedcodingtom9097
    @stonedcodingtom9097 2 ปีที่แล้ว

    Interesting program, thx

  • @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: th-cam.com/video/fzW-qP4JdVI/w-d-xo.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.th-cam.com/users/videoYqzNgaFQEh8/edit
      Insertion Sort: studio.th-cam.com/users/videoTz7vBodZqo8/edit
      Selection Sort: studio.th-cam.com/users/videoYepJ7fDmyjI/edit
      Quicksort: studio.th-cam.com/users/video0jDiBM68NGU/edit
      Merge Sort: studio.th-cam.com/users/videoLeWuki7AQLo/edit

    • @Atrainn234
      @Atrainn234 2 ปีที่แล้ว

      @@PortfolioCourses okay thank you, guessed as much

    • @PortfolioCourses
      @PortfolioCourses  2 ปีที่แล้ว

      @@Atrainn234 You're welcome! :-D

  • @masum0z
    @masum0z ปีที่แล้ว

    What's the apps name that you use?

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว +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 ปีที่แล้ว

      Thanks

    • @PortfolioCourses
      @PortfolioCourses  ปีที่แล้ว

      You're welcome Mohammad! :-)

  • @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. :-)

  • @n4cer252
    @n4cer252 2 ปีที่แล้ว

    god send

  • @SketchupGuru
    @SketchupGuru ปีที่แล้ว

    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

      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. :-)

  • @justcurious1940
    @justcurious1940 6 หลายเดือนก่อน

    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;
    }