Added merge-sort in MATLAB (#27783)
* Added merge-sort in MATLAB * fix: changed c++ to cpp
This commit is contained in:
committed by
Randell Dawson
parent
787729c910
commit
57d66ef6df
@ -52,15 +52,6 @@ If right > l:
|
|||||||
* <a href='https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html'>USFCA</a>
|
* <a href='https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html'>USFCA</a>
|
||||||
* <a href='https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/visualize/'>HackerEarth</a>
|
* <a href='https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/visualize/'>HackerEarth</a>
|
||||||
|
|
||||||
|
|
||||||
### Relavant videos on freeCodeCamp YouTube channel
|
|
||||||
* <a href='https://youtu.be/TzeBrDU-JaY'>Merge Sort algorithm - MyCodeSchool</a>
|
|
||||||
|
|
||||||
### Other Resources:
|
|
||||||
* <a href='https://en.wikipedia.org/wiki/Merge_sort' target='_blank' rel='nofollow'>Wikipedia</a>
|
|
||||||
* <a href='www.geeksforgeeks.org/merge-sort' target='_blank' rel='nofollow'>GeeksForGeeks</a>
|
|
||||||
* <a href='https://youtu.be/sWtYJv_YXbo' target='_blank' rel='nofollow'>Merge Sort - CS50</a>
|
|
||||||
|
|
||||||
### Implementaion in JS
|
### Implementaion in JS
|
||||||
```js
|
```js
|
||||||
const list = [23, 4, 42, 15, 16, 8, 3]
|
const list = [23, 4, 42, 15, 16, 8, 3]
|
||||||
@ -182,6 +173,32 @@ int main()
|
|||||||
|
|
||||||
### Implementation in C++
|
### Implementation in C++
|
||||||
|
|
||||||
|
Let us consider array A = {2,5,7,8,9,12,13}
|
||||||
|
and array B = {3,5,6,9,15} and we want array C to be in ascending order as well.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
void mergesort(int A[],int size_a,int B[],int size_b,int C[])
|
||||||
|
{
|
||||||
|
int token_a,token_b,token_c;
|
||||||
|
for(token_a=0, token_b=0, token_c=0; token_a<size_a && token_b<size_b; )
|
||||||
|
{
|
||||||
|
if(A[token_a]<=B[token_b])
|
||||||
|
C[token_c++]=A[token_a++];
|
||||||
|
else
|
||||||
|
C[token_c++]=B[token_b++];
|
||||||
|
}
|
||||||
|
|
||||||
|
if(token_a<size_a)
|
||||||
|
{
|
||||||
|
while(token_a<size_a)
|
||||||
|
C[token_c++]=A[token_a++];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while(token_b<size_b)
|
||||||
|
C[token_c++]=B[token_b++];
|
||||||
|
}
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
void merge(int arr[], int l, int m, int r)
|
void merge(int arr[], int l, int m, int r)
|
||||||
{
|
{
|
||||||
@ -391,3 +408,57 @@ public class mergesort {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Implementaion in MATLAB
|
||||||
|
```matlab
|
||||||
|
|
||||||
|
a = [9,4,7,3,8,5,1,6,2];
|
||||||
|
|
||||||
|
[sorted] = mergeSort(a);
|
||||||
|
|
||||||
|
function [sorted] = mergeSort(unsorted)
|
||||||
|
len = length(unsorted);
|
||||||
|
if len == 1
|
||||||
|
sorted = unsorted;
|
||||||
|
return;
|
||||||
|
end
|
||||||
|
mid = ceil((len)/2);
|
||||||
|
left = mergeSort(unsorted(1:mid));
|
||||||
|
right = mergeSort(unsorted(mid+1:end));
|
||||||
|
|
||||||
|
sorted = mergeLR(left, right);
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
function [sorted] = mergeLR(left, right)
|
||||||
|
sorted = [left right]; % add two splited array together
|
||||||
|
j = 1; % index for 'sorted'
|
||||||
|
k = 1; % index for 'left'
|
||||||
|
for i = 1:1:length(right)
|
||||||
|
% going through every element of the 'right'
|
||||||
|
if right(i) < left(k)
|
||||||
|
sorted(j) = right(i); % move the 'right' element before
|
||||||
|
sorted(j+1:(j+length(left(k:end)))) = left(k:end); % shift the left elemts
|
||||||
|
j = j+1;
|
||||||
|
else
|
||||||
|
while (k <= length(left)) && (right(i) > left(k))
|
||||||
|
k = k+1;
|
||||||
|
j = j+1;
|
||||||
|
end
|
||||||
|
sorted(j) = right(i);
|
||||||
|
sorted(j+1:(j+length(left(k:end)))) = left(k:end);
|
||||||
|
j = j+1;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### Relavant videos on freeCodeCamp YouTube channel
|
||||||
|
* <a href='https://youtu.be/TzeBrDU-JaY'>Merge Sort algorithm - MyCodeSchool</a>
|
||||||
|
|
||||||
|
### Other Resources:
|
||||||
|
* <a href='https://en.wikipedia.org/wiki/Merge_sort' target='_blank' rel='nofollow'>Wikipedia</a>
|
||||||
|
* <a href='www.geeksforgeeks.org/merge-sort' target='_blank' rel='nofollow'>GeeksForGeeks</a>
|
||||||
|
* <a href='https://youtu.be/sWtYJv_YXbo' target='_blank' rel='nofollow'>Merge Sort - CS50</a>
|
||||||
|
Reference in New Issue
Block a user