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?
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.
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.
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?
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. :-)
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”. :-)
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.
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
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?
@@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
@@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.
@@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++; } }
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.
Want to see a *recursive* solution?
Check out Merge Two Sorted Arrays Using Recursion: ruclips.net/video/fzW-qP4JdVI/видео.html
6:48 shouldn't it be b[ j ] instead of b[ i ] ?
I think you're right! I may have to re-do this video, thank you for the heads up.
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?
great explanation! thank you mr.
You’re very welcome, I’m glad you enjoyed it! :-)
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.
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.
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.
Can’t you just write the while loop with || (or) instead of && (and), and avoid the other while loops?
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?
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. :-)
Hi is it possible to make this with for loops instead of while loops?
Or does it work only with while loops
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. 🙂
Very good explanation. Thank you.
You’re welcome Prashansa! :-) I’m glad to hear you enjoyed it.
Heyy, which is this software? I have tried many sites to download the C software in macbook but it was unsuccessful
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”. :-)
What's the apps name that you use?
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.
Thanks
You're welcome Mohammad! :-)
How to do using recursion ?
I will add this to my list of future video ideas Deepak. :-)
I made this video today, I hope you enjoy it: ruclips.net/video/fzW-qP4JdVI/видео.html. :-)
What if the two arrays are not sorted
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
@@PortfolioCourses okay thank you, guessed as much
@@Atrainn234 You're welcome! :-D
thank u so much...can u do more videos about arrays and string algoritms
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?
@@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
@@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.
very nice thank you for helping
You're very welcome Ivo! :-)
arr[]= {2,4,6,8}
Arr[]={1,3,4,5}
This type of example not fixed in this code
Can you solve this problem if you have any idea sir to find the merger of it how we do it
@@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++;
}
}
@@PortfolioCourses thanks 😊 for helping me
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.
@@gouravchalotravlog That's an interesting question, I will try to make a video on it one day. :-)
Interesting program, thx
You're welcome! :-D
amazing
god send
Haha thank you! :-D
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;
}