Merge Sort algorithm using divide and conquer principle

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
I have implemented merge sort algorithm on divide and conquer principle.
When I pass an array to my program,
- that array is divided into sub-arrays,
- I apply bubble sort on each of the sub-array. Now I have sorted sub array,
- I merge the sorted sub-arrays
- I do step 2 and 3, till I have an array of length 1, i.e an array with only one element which itself is an array.
The program for the above steps is as follows,
(function()
'use strict';
var swap = swap;
var getCommandLineVariables = getCommandLineVariables;
var mergeSort = mergeSort;
var divide = divide;
var bubbleSort = bubbleSort;
var merge = merge;
var arrayOfArrays = ; // main output array
mergeSort();
/*
Merge sort main function.
The below merge-sort is based on divide and conquor.
*/
function mergeSort() recheck);
console.log(arrayOfArrays[0]);
// end of mergeSort
/*
Capture the numbers passed as commandline arguments
*/
function getCommandLineVariables()
var array = process.argv.slice(2);
array = array.map(function(num)
return parseInt(num);
);
return array;
// end of getCommandLineVariables
/*
Swap numbers accross the position
*/
function swap(currentPosition,nextPosition,array)
var temp = array[currentPosition];
array[currentPosition] = array[nextPosition];
array[nextPosition] = temp;
// end of swap
/*
Dividing the array into sub arrays
*/
function divide(theArray)
var pivot = theArray.length/2,
leftArray = undefined,
rightArray = undefined;
pivot = parseInt(pivot);
if(pivot >= 1)
leftArray = theArray.slice(0,pivot);
rightArray = theArray.slice(pivot,theArray.length);
if(leftArray.length > 2)
divide(leftArray)
else
arrayOfArrays.push(leftArray);
if(rightArray.length > 2)
divide(rightArray);
else
arrayOfArrays.push(rightArray);
else
arrayOfArrays.push(theArray);
// end of divide
/*
Used bubble sort for
sorting the sub arrays
*/
function bubbleSort(array)
var totalElement = array.length,
currentElement = 0,
nextElement = 0,
isSwapped = false;
do
isSwapped = false;
for(var x = 0; x < totalElement; x++)
currentElement = x;
nextElement = x + 1;
if(nextElement <= totalElement)
if(array[currentElement] > array[nextElement])
swap(currentElement,nextElement,array);
isSwapped = true;
// end of if
// end of if
// end of for
while(isSwapped === true);
// end of bubbleSort
/*
Merging the arrays in one array
*/
function merge(theArray)
var new_Array = , currentElement = undefined, nextElement = undefined;
for(var x = 0; x < theArray.length; x++)
currentElement = theArray[x];
nextElement = (theArray[x + 1] === undefined) ? undefined : theArray[x + 1];
if(currentElement.constructor.name === "Array" && nextElement !== undefined && nextElement.constructor.name === "Array")
new_Array.push(currentElement.concat(nextElement));
else
new_Array.push(currentElement);
// end of if
x++;
// end of for
return new_Array;
// end of merge
)();
I run the above code from command line,
and I pass numbers in commandline arguments.
As below,
E:DataStructuresAndAlgorithmssorting>node mergeSort01.js 12 43 1 4 3 32 100 192 5
Divided [ [ 12, 43 ], [ 1, 4 ], [ 3, 32 ], [ 100 ], [ 192, 5 ] ]
Merged [ [ 12, 43 ], [ 1, 4 ], [ 3, 32 ], [ 100 ], [ 5, 192 ] ]
Merged [ [ 1, 4, 12, 43 ], [ 3, 32, 100 ], [ 5, 192 ] ]
Merged [ [ 1, 3, 4, 12, 32, 43, 100 ], [ 5, 192 ] ]
Merged [ [ 1, 3, 4, 5, 12, 32, 43, 100, 192 ] ]
[ 1, 3, 4, 5, 12, 32, 43, 100, 192 ]
Please review my code above
Please check whether I have achieved divide and conquer in my merge sort algorithm properly ?
Thanks
javascript algorithm sorting mergesort divide-and-conquer
add a comment |Â
up vote
0
down vote
favorite
I have implemented merge sort algorithm on divide and conquer principle.
When I pass an array to my program,
- that array is divided into sub-arrays,
- I apply bubble sort on each of the sub-array. Now I have sorted sub array,
- I merge the sorted sub-arrays
- I do step 2 and 3, till I have an array of length 1, i.e an array with only one element which itself is an array.
The program for the above steps is as follows,
(function()
'use strict';
var swap = swap;
var getCommandLineVariables = getCommandLineVariables;
var mergeSort = mergeSort;
var divide = divide;
var bubbleSort = bubbleSort;
var merge = merge;
var arrayOfArrays = ; // main output array
mergeSort();
/*
Merge sort main function.
The below merge-sort is based on divide and conquor.
*/
function mergeSort() recheck);
console.log(arrayOfArrays[0]);
// end of mergeSort
/*
Capture the numbers passed as commandline arguments
*/
function getCommandLineVariables()
var array = process.argv.slice(2);
array = array.map(function(num)
return parseInt(num);
);
return array;
// end of getCommandLineVariables
/*
Swap numbers accross the position
*/
function swap(currentPosition,nextPosition,array)
var temp = array[currentPosition];
array[currentPosition] = array[nextPosition];
array[nextPosition] = temp;
// end of swap
/*
Dividing the array into sub arrays
*/
function divide(theArray)
var pivot = theArray.length/2,
leftArray = undefined,
rightArray = undefined;
pivot = parseInt(pivot);
if(pivot >= 1)
leftArray = theArray.slice(0,pivot);
rightArray = theArray.slice(pivot,theArray.length);
if(leftArray.length > 2)
divide(leftArray)
else
arrayOfArrays.push(leftArray);
if(rightArray.length > 2)
divide(rightArray);
else
arrayOfArrays.push(rightArray);
else
arrayOfArrays.push(theArray);
// end of divide
/*
Used bubble sort for
sorting the sub arrays
*/
function bubbleSort(array)
var totalElement = array.length,
currentElement = 0,
nextElement = 0,
isSwapped = false;
do
isSwapped = false;
for(var x = 0; x < totalElement; x++)
currentElement = x;
nextElement = x + 1;
if(nextElement <= totalElement)
if(array[currentElement] > array[nextElement])
swap(currentElement,nextElement,array);
isSwapped = true;
// end of if
// end of if
// end of for
while(isSwapped === true);
// end of bubbleSort
/*
Merging the arrays in one array
*/
function merge(theArray)
var new_Array = , currentElement = undefined, nextElement = undefined;
for(var x = 0; x < theArray.length; x++)
currentElement = theArray[x];
nextElement = (theArray[x + 1] === undefined) ? undefined : theArray[x + 1];
if(currentElement.constructor.name === "Array" && nextElement !== undefined && nextElement.constructor.name === "Array")
new_Array.push(currentElement.concat(nextElement));
else
new_Array.push(currentElement);
// end of if
x++;
// end of for
return new_Array;
// end of merge
)();
I run the above code from command line,
and I pass numbers in commandline arguments.
As below,
E:DataStructuresAndAlgorithmssorting>node mergeSort01.js 12 43 1 4 3 32 100 192 5
Divided [ [ 12, 43 ], [ 1, 4 ], [ 3, 32 ], [ 100 ], [ 192, 5 ] ]
Merged [ [ 12, 43 ], [ 1, 4 ], [ 3, 32 ], [ 100 ], [ 5, 192 ] ]
Merged [ [ 1, 4, 12, 43 ], [ 3, 32, 100 ], [ 5, 192 ] ]
Merged [ [ 1, 3, 4, 12, 32, 43, 100 ], [ 5, 192 ] ]
Merged [ [ 1, 3, 4, 5, 12, 32, 43, 100, 192 ] ]
[ 1, 3, 4, 5, 12, 32, 43, 100, 192 ]
Please review my code above
Please check whether I have achieved divide and conquer in my merge sort algorithm properly ?
Thanks
javascript algorithm sorting mergesort divide-and-conquer
I don't understand how your step 4 makes sense. How can you, in step 4, do step 2 and 3 again, if dividing into sub-arrays is step 1? Also, why do you use bubble-sort if you are already trying to implement an efficient algorithm? I think you shouldn't need to sort the subarrays, and instead just keep dividing with a pivot.
â Raimund Krämer
Jan 19 at 10:25
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have implemented merge sort algorithm on divide and conquer principle.
When I pass an array to my program,
- that array is divided into sub-arrays,
- I apply bubble sort on each of the sub-array. Now I have sorted sub array,
- I merge the sorted sub-arrays
- I do step 2 and 3, till I have an array of length 1, i.e an array with only one element which itself is an array.
The program for the above steps is as follows,
(function()
'use strict';
var swap = swap;
var getCommandLineVariables = getCommandLineVariables;
var mergeSort = mergeSort;
var divide = divide;
var bubbleSort = bubbleSort;
var merge = merge;
var arrayOfArrays = ; // main output array
mergeSort();
/*
Merge sort main function.
The below merge-sort is based on divide and conquor.
*/
function mergeSort() recheck);
console.log(arrayOfArrays[0]);
// end of mergeSort
/*
Capture the numbers passed as commandline arguments
*/
function getCommandLineVariables()
var array = process.argv.slice(2);
array = array.map(function(num)
return parseInt(num);
);
return array;
// end of getCommandLineVariables
/*
Swap numbers accross the position
*/
function swap(currentPosition,nextPosition,array)
var temp = array[currentPosition];
array[currentPosition] = array[nextPosition];
array[nextPosition] = temp;
// end of swap
/*
Dividing the array into sub arrays
*/
function divide(theArray)
var pivot = theArray.length/2,
leftArray = undefined,
rightArray = undefined;
pivot = parseInt(pivot);
if(pivot >= 1)
leftArray = theArray.slice(0,pivot);
rightArray = theArray.slice(pivot,theArray.length);
if(leftArray.length > 2)
divide(leftArray)
else
arrayOfArrays.push(leftArray);
if(rightArray.length > 2)
divide(rightArray);
else
arrayOfArrays.push(rightArray);
else
arrayOfArrays.push(theArray);
// end of divide
/*
Used bubble sort for
sorting the sub arrays
*/
function bubbleSort(array)
var totalElement = array.length,
currentElement = 0,
nextElement = 0,
isSwapped = false;
do
isSwapped = false;
for(var x = 0; x < totalElement; x++)
currentElement = x;
nextElement = x + 1;
if(nextElement <= totalElement)
if(array[currentElement] > array[nextElement])
swap(currentElement,nextElement,array);
isSwapped = true;
// end of if
// end of if
// end of for
while(isSwapped === true);
// end of bubbleSort
/*
Merging the arrays in one array
*/
function merge(theArray)
var new_Array = , currentElement = undefined, nextElement = undefined;
for(var x = 0; x < theArray.length; x++)
currentElement = theArray[x];
nextElement = (theArray[x + 1] === undefined) ? undefined : theArray[x + 1];
if(currentElement.constructor.name === "Array" && nextElement !== undefined && nextElement.constructor.name === "Array")
new_Array.push(currentElement.concat(nextElement));
else
new_Array.push(currentElement);
// end of if
x++;
// end of for
return new_Array;
// end of merge
)();
I run the above code from command line,
and I pass numbers in commandline arguments.
As below,
E:DataStructuresAndAlgorithmssorting>node mergeSort01.js 12 43 1 4 3 32 100 192 5
Divided [ [ 12, 43 ], [ 1, 4 ], [ 3, 32 ], [ 100 ], [ 192, 5 ] ]
Merged [ [ 12, 43 ], [ 1, 4 ], [ 3, 32 ], [ 100 ], [ 5, 192 ] ]
Merged [ [ 1, 4, 12, 43 ], [ 3, 32, 100 ], [ 5, 192 ] ]
Merged [ [ 1, 3, 4, 12, 32, 43, 100 ], [ 5, 192 ] ]
Merged [ [ 1, 3, 4, 5, 12, 32, 43, 100, 192 ] ]
[ 1, 3, 4, 5, 12, 32, 43, 100, 192 ]
Please review my code above
Please check whether I have achieved divide and conquer in my merge sort algorithm properly ?
Thanks
javascript algorithm sorting mergesort divide-and-conquer
I have implemented merge sort algorithm on divide and conquer principle.
When I pass an array to my program,
- that array is divided into sub-arrays,
- I apply bubble sort on each of the sub-array. Now I have sorted sub array,
- I merge the sorted sub-arrays
- I do step 2 and 3, till I have an array of length 1, i.e an array with only one element which itself is an array.
The program for the above steps is as follows,
(function()
'use strict';
var swap = swap;
var getCommandLineVariables = getCommandLineVariables;
var mergeSort = mergeSort;
var divide = divide;
var bubbleSort = bubbleSort;
var merge = merge;
var arrayOfArrays = ; // main output array
mergeSort();
/*
Merge sort main function.
The below merge-sort is based on divide and conquor.
*/
function mergeSort() recheck);
console.log(arrayOfArrays[0]);
// end of mergeSort
/*
Capture the numbers passed as commandline arguments
*/
function getCommandLineVariables()
var array = process.argv.slice(2);
array = array.map(function(num)
return parseInt(num);
);
return array;
// end of getCommandLineVariables
/*
Swap numbers accross the position
*/
function swap(currentPosition,nextPosition,array)
var temp = array[currentPosition];
array[currentPosition] = array[nextPosition];
array[nextPosition] = temp;
// end of swap
/*
Dividing the array into sub arrays
*/
function divide(theArray)
var pivot = theArray.length/2,
leftArray = undefined,
rightArray = undefined;
pivot = parseInt(pivot);
if(pivot >= 1)
leftArray = theArray.slice(0,pivot);
rightArray = theArray.slice(pivot,theArray.length);
if(leftArray.length > 2)
divide(leftArray)
else
arrayOfArrays.push(leftArray);
if(rightArray.length > 2)
divide(rightArray);
else
arrayOfArrays.push(rightArray);
else
arrayOfArrays.push(theArray);
// end of divide
/*
Used bubble sort for
sorting the sub arrays
*/
function bubbleSort(array)
var totalElement = array.length,
currentElement = 0,
nextElement = 0,
isSwapped = false;
do
isSwapped = false;
for(var x = 0; x < totalElement; x++)
currentElement = x;
nextElement = x + 1;
if(nextElement <= totalElement)
if(array[currentElement] > array[nextElement])
swap(currentElement,nextElement,array);
isSwapped = true;
// end of if
// end of if
// end of for
while(isSwapped === true);
// end of bubbleSort
/*
Merging the arrays in one array
*/
function merge(theArray)
var new_Array = , currentElement = undefined, nextElement = undefined;
for(var x = 0; x < theArray.length; x++)
currentElement = theArray[x];
nextElement = (theArray[x + 1] === undefined) ? undefined : theArray[x + 1];
if(currentElement.constructor.name === "Array" && nextElement !== undefined && nextElement.constructor.name === "Array")
new_Array.push(currentElement.concat(nextElement));
else
new_Array.push(currentElement);
// end of if
x++;
// end of for
return new_Array;
// end of merge
)();
I run the above code from command line,
and I pass numbers in commandline arguments.
As below,
E:DataStructuresAndAlgorithmssorting>node mergeSort01.js 12 43 1 4 3 32 100 192 5
Divided [ [ 12, 43 ], [ 1, 4 ], [ 3, 32 ], [ 100 ], [ 192, 5 ] ]
Merged [ [ 12, 43 ], [ 1, 4 ], [ 3, 32 ], [ 100 ], [ 5, 192 ] ]
Merged [ [ 1, 4, 12, 43 ], [ 3, 32, 100 ], [ 5, 192 ] ]
Merged [ [ 1, 3, 4, 12, 32, 43, 100 ], [ 5, 192 ] ]
Merged [ [ 1, 3, 4, 5, 12, 32, 43, 100, 192 ] ]
[ 1, 3, 4, 5, 12, 32, 43, 100, 192 ]
Please review my code above
Please check whether I have achieved divide and conquer in my merge sort algorithm properly ?
Thanks
javascript algorithm sorting mergesort divide-and-conquer
asked Jan 19 at 6:10
Rahul Shivsharan
35328
35328
I don't understand how your step 4 makes sense. How can you, in step 4, do step 2 and 3 again, if dividing into sub-arrays is step 1? Also, why do you use bubble-sort if you are already trying to implement an efficient algorithm? I think you shouldn't need to sort the subarrays, and instead just keep dividing with a pivot.
â Raimund Krämer
Jan 19 at 10:25
add a comment |Â
I don't understand how your step 4 makes sense. How can you, in step 4, do step 2 and 3 again, if dividing into sub-arrays is step 1? Also, why do you use bubble-sort if you are already trying to implement an efficient algorithm? I think you shouldn't need to sort the subarrays, and instead just keep dividing with a pivot.
â Raimund Krämer
Jan 19 at 10:25
I don't understand how your step 4 makes sense. How can you, in step 4, do step 2 and 3 again, if dividing into sub-arrays is step 1? Also, why do you use bubble-sort if you are already trying to implement an efficient algorithm? I think you shouldn't need to sort the subarrays, and instead just keep dividing with a pivot.
â Raimund Krämer
Jan 19 at 10:25
I don't understand how your step 4 makes sense. How can you, in step 4, do step 2 and 3 again, if dividing into sub-arrays is step 1? Also, why do you use bubble-sort if you are already trying to implement an efficient algorithm? I think you shouldn't need to sort the subarrays, and instead just keep dividing with a pivot.
â Raimund Krämer
Jan 19 at 10:25
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185454%2fmerge-sort-algorithm-using-divide-and-conquer-principle%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I don't understand how your step 4 makes sense. How can you, in step 4, do step 2 and 3 again, if dividing into sub-arrays is step 1? Also, why do you use bubble-sort if you are already trying to implement an efficient algorithm? I think you shouldn't need to sort the subarrays, and instead just keep dividing with a pivot.
â Raimund Krämer
Jan 19 at 10:25