Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add merge sort code in python #11

Open
wants to merge 50 commits into
base: master
Choose a base branch
from

Conversation

priyansh1114
Copy link

No description provided.

yanzv and others added 20 commits November 1, 2016 16:09
This file contains a solution of Algorithm Implementation's Matrix Layer Rotation(Difficulty: Hard) on HackerRank.
Created MatrixLayerRotation.java
This file contains the solution of Algorithm-> String -> Camel Case problem on HackerRank (https://www.hackerrank.com/challenges/camelcase).
This file contains the solution of HackerRank -> Algorithm -> String -> Beautiful Binary String ( https://www.hackerrank.com/challenges/beautiful-binary-string )
This file contains the solution of HackerRank -> Algorithm -> Implementation -> Divisible Sum Pairs ( https://www.hackerrank.com/challenges/divisible-sum-pairs )
This file contains the solution of HackerRank -> Algorithm -> Implementation -> Save The Prisoner ( https://www.hackerrank.com/challenges/save-the-prisoner )
This file contains a solution of HackerRank -> Algorithm -> Implementation -> Strange Counter ( https://www.hackerrank.com/challenges/strange-code )
This file contains a solution of the bomberman game problem on Hackerrank ( https://www.hackerrank.com/challenges/bomber-man )
Added more solutions
Thank you!
Update PatternSyntaxChecker.java
Create GradingStudents.java
@raginismaurya
Copy link

MergeSort in Python

def mergeSort(array):
if len(array) > 1:

    #  r is the point where the array is divided into two subarrays
    r = len(array)//2
    L = array[:r]
    M = array[r:]

    # Sort the two halves
    mergeSort(L)
    mergeSort(M)

    i = j = k = 0

    # Until we reach either end of either L or M, pick larger among
    # elements L and M and place them in the correct position at A[p..r]
    while i < len(L) and j < len(M):
        if L[i] < M[j]:
            array[k] = L[i]
            i += 1
        else:
            array[k] = M[j]
            j += 1
        k += 1

    # When we run out of elements in either L or M,
    # pick up the remaining elements and put in A[p..r]
    while i < len(L):
        array[k] = L[i]
        i += 1
        k += 1

    while j < len(M):
        array[k] = M[j]
        j += 1
        k += 1

Print the array

def printList(array):
for i in range(len(array)):
print(array[i], end=" ")
print()

Driver program

if name == 'main':
array = [6, 5, 12, 10, 9, 1]

mergeSort(array)

print("Sorted array is: ")
printList(array)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants