is this python code relevant for merge sort?Merge sort in PythonMerge sort Python implementationMerge sort...

How did the power source of Mar-Vell's aircraft end up with her?

Good allowance savings plan?

Should I take out a loan for a friend to invest on my behalf?

Could you please stop shuffling the deck and play already?

What to do when during a meeting client people start to fight (even physically) with each others?

Exporting list of URLs

Unreachable code, but reachable with exception

Word-Letter Ladder

Do f-stop and exposure time perfectly cancel?

Why is there a voltage between the mains ground and my radiator?

Force user to remove USB token

Making a sword in the stone, in a medieval world without magic

Is there an elementary proof that there are infinitely many primes that are *not* completely split in an abelian extension?

Do Bugbears' arms literally get longer when it's their turn?

Finding algorithms of QGIS commands?

Why do different render engines generate different z pass?

PTIJ: Why can't I eat anything?

Why is this plane circling around the LKO airport every day?

Algorithm to convert a fixed-length string to the smallest possible collision-free representation?

Solving "Resistance between two nodes on a grid" problem in Mathematica

Word for a person who has no opinion about whether god exists

How to pass a string to a command that expects a file?

How did Alan Turing break the enigma code using the hint given by the lady in the bar?

They call me Inspector Morse



is this python code relevant for merge sort?


Merge sort in PythonMerge sort Python implementationMerge sort implementation in PythonMerge Sort Algorithm PythonRecursive merge sort in pythonPython Merge Sort ImplementationPython Merge sort count inversionsSearching the Array Index & Element EqualityMerge sort approach in PythonPython merge sort













1












$begingroup$


This code below is what i think should be the implementation of merge sort in Python and it works as expected. It sorts the given list. But after considering some online implementations i am doubtful about mine. so the base question is "Is this even Merge sort?". As per the requirement of the question it is to be assumed that all values are distinct. Thank you for your time and effort in critiquing this code



def merge(arr):
# base case
length = len(arr)
half = length//2
if length == 1 or length == 0:
return arr
# recursive case
firstHalf = merge(arr[:half]) # sort first half
secondHalf = merge(arr[half:]) # sort second half
length_firstHalf = len(firstHalf)
length_secondHalf = len(secondHalf)
sortedList = []

i, j = 0, 0 # variables for iteration
while i != length_firstHalf and j != length_secondHalf: # add elements into the new array until either of then two sub arrays is completely traversed
if firstHalf[i] > secondHalf[j]:
sortedList.append(secondHalf[j])
j += 1
continue
if firstHalf[i] < secondHalf[j]:
sortedList.append(firstHalf[i])
i += 1
return(sortedList + firstHalf[i:] + secondHalf[j:]) # return the array after adding whats left of sub array which wasnt completely traversed









share|improve this question









New contributor




billi_meow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 1




    $begingroup$
    Please fix your indentation. The easiest way to post code is to paste it into the question editor, highlight it, and press Ctrl-K to mark it as a code block.
    $endgroup$
    – 200_success
    6 mins ago










  • $begingroup$
    @200_success thank you for the advice, the code is edited now
    $endgroup$
    – billi_meow
    3 mins ago










  • $begingroup$
    It's still broken.
    $endgroup$
    – 200_success
    2 mins ago
















1












$begingroup$


This code below is what i think should be the implementation of merge sort in Python and it works as expected. It sorts the given list. But after considering some online implementations i am doubtful about mine. so the base question is "Is this even Merge sort?". As per the requirement of the question it is to be assumed that all values are distinct. Thank you for your time and effort in critiquing this code



def merge(arr):
# base case
length = len(arr)
half = length//2
if length == 1 or length == 0:
return arr
# recursive case
firstHalf = merge(arr[:half]) # sort first half
secondHalf = merge(arr[half:]) # sort second half
length_firstHalf = len(firstHalf)
length_secondHalf = len(secondHalf)
sortedList = []

i, j = 0, 0 # variables for iteration
while i != length_firstHalf and j != length_secondHalf: # add elements into the new array until either of then two sub arrays is completely traversed
if firstHalf[i] > secondHalf[j]:
sortedList.append(secondHalf[j])
j += 1
continue
if firstHalf[i] < secondHalf[j]:
sortedList.append(firstHalf[i])
i += 1
return(sortedList + firstHalf[i:] + secondHalf[j:]) # return the array after adding whats left of sub array which wasnt completely traversed









share|improve this question









New contributor




billi_meow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 1




    $begingroup$
    Please fix your indentation. The easiest way to post code is to paste it into the question editor, highlight it, and press Ctrl-K to mark it as a code block.
    $endgroup$
    – 200_success
    6 mins ago










  • $begingroup$
    @200_success thank you for the advice, the code is edited now
    $endgroup$
    – billi_meow
    3 mins ago










  • $begingroup$
    It's still broken.
    $endgroup$
    – 200_success
    2 mins ago














1












1








1





$begingroup$


This code below is what i think should be the implementation of merge sort in Python and it works as expected. It sorts the given list. But after considering some online implementations i am doubtful about mine. so the base question is "Is this even Merge sort?". As per the requirement of the question it is to be assumed that all values are distinct. Thank you for your time and effort in critiquing this code



def merge(arr):
# base case
length = len(arr)
half = length//2
if length == 1 or length == 0:
return arr
# recursive case
firstHalf = merge(arr[:half]) # sort first half
secondHalf = merge(arr[half:]) # sort second half
length_firstHalf = len(firstHalf)
length_secondHalf = len(secondHalf)
sortedList = []

i, j = 0, 0 # variables for iteration
while i != length_firstHalf and j != length_secondHalf: # add elements into the new array until either of then two sub arrays is completely traversed
if firstHalf[i] > secondHalf[j]:
sortedList.append(secondHalf[j])
j += 1
continue
if firstHalf[i] < secondHalf[j]:
sortedList.append(firstHalf[i])
i += 1
return(sortedList + firstHalf[i:] + secondHalf[j:]) # return the array after adding whats left of sub array which wasnt completely traversed









share|improve this question









New contributor




billi_meow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




This code below is what i think should be the implementation of merge sort in Python and it works as expected. It sorts the given list. But after considering some online implementations i am doubtful about mine. so the base question is "Is this even Merge sort?". As per the requirement of the question it is to be assumed that all values are distinct. Thank you for your time and effort in critiquing this code



def merge(arr):
# base case
length = len(arr)
half = length//2
if length == 1 or length == 0:
return arr
# recursive case
firstHalf = merge(arr[:half]) # sort first half
secondHalf = merge(arr[half:]) # sort second half
length_firstHalf = len(firstHalf)
length_secondHalf = len(secondHalf)
sortedList = []

i, j = 0, 0 # variables for iteration
while i != length_firstHalf and j != length_secondHalf: # add elements into the new array until either of then two sub arrays is completely traversed
if firstHalf[i] > secondHalf[j]:
sortedList.append(secondHalf[j])
j += 1
continue
if firstHalf[i] < secondHalf[j]:
sortedList.append(firstHalf[i])
i += 1
return(sortedList + firstHalf[i:] + secondHalf[j:]) # return the array after adding whats left of sub array which wasnt completely traversed






python






share|improve this question









New contributor




billi_meow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




billi_meow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 48 secs ago







billi_meow













New contributor




billi_meow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 12 mins ago









billi_meowbilli_meow

62




62




New contributor




billi_meow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





billi_meow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






billi_meow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    $begingroup$
    Please fix your indentation. The easiest way to post code is to paste it into the question editor, highlight it, and press Ctrl-K to mark it as a code block.
    $endgroup$
    – 200_success
    6 mins ago










  • $begingroup$
    @200_success thank you for the advice, the code is edited now
    $endgroup$
    – billi_meow
    3 mins ago










  • $begingroup$
    It's still broken.
    $endgroup$
    – 200_success
    2 mins ago














  • 1




    $begingroup$
    Please fix your indentation. The easiest way to post code is to paste it into the question editor, highlight it, and press Ctrl-K to mark it as a code block.
    $endgroup$
    – 200_success
    6 mins ago










  • $begingroup$
    @200_success thank you for the advice, the code is edited now
    $endgroup$
    – billi_meow
    3 mins ago










  • $begingroup$
    It's still broken.
    $endgroup$
    – 200_success
    2 mins ago








1




1




$begingroup$
Please fix your indentation. The easiest way to post code is to paste it into the question editor, highlight it, and press Ctrl-K to mark it as a code block.
$endgroup$
– 200_success
6 mins ago




$begingroup$
Please fix your indentation. The easiest way to post code is to paste it into the question editor, highlight it, and press Ctrl-K to mark it as a code block.
$endgroup$
– 200_success
6 mins ago












$begingroup$
@200_success thank you for the advice, the code is edited now
$endgroup$
– billi_meow
3 mins ago




$begingroup$
@200_success thank you for the advice, the code is edited now
$endgroup$
– billi_meow
3 mins ago












$begingroup$
It's still broken.
$endgroup$
– 200_success
2 mins ago




$begingroup$
It's still broken.
$endgroup$
– 200_success
2 mins ago










0






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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






billi_meow is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215298%2fis-this-python-code-relevant-for-merge-sort%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes








billi_meow is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















billi_meow is a new contributor. Be nice, and check out our Code of Conduct.













billi_meow is a new contributor. Be nice, and check out our Code of Conduct.












billi_meow is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215298%2fis-this-python-code-relevant-for-merge-sort%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...