Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4470486

Browse files
committed
merge sorting algo first simplest implementation
1 parentbfb9cb1 commit4470486

File tree

4 files changed

+3406
-303
lines changed

4 files changed

+3406
-303
lines changed

‎Sorting/insertion_sort-with-swap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function insertion_sort(array) {
3030
3131
3. Compare new element in sorted array with previous elements to determine correct destination index in sorted array. Say I get 12, 11 as the second and third element. Since 11 is smaller than 12, move 12 and insert 11 before 12
3232
33-
4. In the below,at the last step, I had to decrement j by one, to stopthewhile loop. Because,with thedecremented value of j, in the next iteration of the loop, one of the while loop condition will fail
33+
4. In the below,I have donetheinsertion operationwith theswap function.
3434
*/
3535

3636
for(i=1;i<array.length;i++){

‎Sorting/mergeSort-Basic-Recursive.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// ROHAN'S NOTE IS TO REFER HERE IN FUTURE - Implementation of merge-sort algorithm, to sort an array of numbers in O(N×log(N)) time.
2+
3+
/* https://www.geeksforgeeks.org/merge-sort/
4+
5+
Like QuickSort, Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one. See following pseudo-code.
6+
7+
MergeSort(arr[], l, r)
8+
If r > l
9+
1. Find the middle point to divide the array into two halves:
10+
middle m = (l+r)/2
11+
2. Call mergeSort for first half:
12+
Call mergeSort(arr, l, m)
13+
3. Call mergeSort for second half:
14+
Call mergeSort(arr, m+1, r)
15+
4. Merge the two halves sorted in step 2 and 3:
16+
Call merge(arr, l, m, r)
17+
18+
ANOTHER GOOD EXPLANATION - https://www.hackerearth.com/practice/algorithms/sorting/merge-sort/tutorial/
19+
20+
In other words - Merge sort is a divide-and-conquer algorithm based on the idea of breaking down a list into several sub-lists until each sublist consists of a single element and merging those sublists in a manner that results into a sorted list.
21+
22+
Steps implemented below :-
23+
A> The array is recursively divided in two halves till the size becomes 1.
24+
25+
B> Once the size becomes 1, the merge processes comes into action, meaning I invoke the merge function on the 2 single-element arrays. That is, take adjacent pairs of two singleton lists, sort the elements of the 2 list, and merge them to form a list (ie array) of 2 elements .
26+
27+
C> So the merge() function below is a completely independent function and will just take 2 sorted-arrays ( the individual arrays MUST BE SORTED ), and then concat the elements to forma a single SORTED array. That is, within the merge() function before pushing the elements to the final result array (which will be the output from merge ) it checks for proper-sorting between the 2 elements on the 2 arrays.
28+
29+
merge( [1, 3, 5, 7 ] , [ 2, 4, 6, 8] ) >> will produce the below
30+
31+
[ 1, 2, 3, 4, 5, 6, 7, 8 ]
32+
33+
D) And then starts merging arrays back till the complete array is merged.
34+
*/
35+
36+
merge2SortedArrays=(leftSorted,rightSorted)=>{
37+
38+
letleftIndex=0,rightIndex=0,mergedArray=[];
39+
40+
while(leftIndex<leftSorted.length&&rightIndex<rightSorted.length){
41+
mergedArray.push(
42+
leftSorted[leftIndex]<rightSorted[rightIndex]
43+
?leftSorted[leftIndex++]
44+
:rightSorted[rightIndex++]
45+
)
46+
// Note, I am using post-increment syntax leftIndex++ . So the way it will work is, it will push the current leftIndex and only then it will increment it and pass that incremented value for the next iteration.
47+
}
48+
49+
// And now I have to concat the left-over elements of the longer array. Because, the while loop will stop as soon as I have traversed all the sorter array's elements.
50+
returnmergedArray
51+
.concat(leftSorted.slice(leftIndex))
52+
.concat(rightSorted.slice(rightIndex))
53+
}
54+
55+
// And now write the mergeSort algo
56+
mergeSort=(arr)=>{
57+
58+
// First specify the terminal condition
59+
if(arr.length<2){
60+
returnarr
61+
}
62+
63+
letmid=Math.floor(arr.length/2);
64+
letleftArr=arr.slice(0,mid);
65+
letrightArr=arr.slice(mid);
66+
67+
returnmerge2SortedArrays(mergeSort(leftArr),mergeSort(rightArr));
68+
}
69+
70+
console.log(mergeSort([-4,1,Infinity,3,3,0]));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp