Merge Sort algorithm using divide and conquer principle

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite
1












I have implemented merge sort algorithm on divide and conquer principle.



When I pass an array to my program,



  1. that array is divided into sub-arrays,

  2. I apply bubble sort on each of the sub-array. Now I have sorted sub array,

  3. I merge the sorted sub-arrays

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







share|improve this question



















  • 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
















up vote
0
down vote

favorite
1












I have implemented merge sort algorithm on divide and conquer principle.



When I pass an array to my program,



  1. that array is divided into sub-arrays,

  2. I apply bubble sort on each of the sub-array. Now I have sorted sub array,

  3. I merge the sorted sub-arrays

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







share|improve this question



















  • 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












up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I have implemented merge sort algorithm on divide and conquer principle.



When I pass an array to my program,



  1. that array is divided into sub-arrays,

  2. I apply bubble sort on each of the sub-array. Now I have sorted sub array,

  3. I merge the sorted sub-arrays

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







share|improve this question











I have implemented merge sort algorithm on divide and conquer principle.



When I pass an array to my program,



  1. that array is divided into sub-arrays,

  2. I apply bubble sort on each of the sub-array. Now I have sorted sub array,

  3. I merge the sorted sub-arrays

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









share|improve this question










share|improve this question




share|improve this question









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
















  • 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















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Python Lists

Aion

JavaScript Array Iteration Methods