GOEDUHUB Tutorials    ||    AI    ||    ML  ||    NLP    ||    OpenCV    ||    Python    ||    OOPS    ||    DBMS    ||    DSA    ||    JAVA    ||    Linux    ||    C Programming
7976731765 Go To Your University       ||       Placement Preparation       ||        Technical-MCQ and Interview Questions
Online Live Classes Youtube Live Link
0 like 0 dislike
30 views
in Python Programming by Goeduhub's Expert (8.1k points)

1 Answer

0 like 0 dislike
by Goeduhub's Expert (8.1k points)
 
Best answer

Merge Sort is a Divide and Conqure 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. 

Auxiliary Space  :   O(n)

Time complexity :  θ(nlog(n))

merge-sort-python-program

Python program for implementation of Merge Sort:

def mergeSort(arr):

    if len(arr) >1:

        mid = len(arr)//2 

        L = arr[:mid]

        R = arr[mid:]

         mergeSort(L)

        mergeSort(R)

         i = j = k = 0

         # Copy data to temp arrays L[] and R[]

        while i < len(L) and j < len(R):

            if L[i] < R[j]:

                arr[k] = L[i]

                i+=1

            else:

                arr[k] = R[j]

                j+=1

            k+=1

        while i < len(L):

            arr[k] = L[i]

            i+=1

            k+=1

         

        while j < len(R):

            arr[k] = R[j]

            j+=1

            k+=1        

a=[12,11,8,5,3,20]

mergeSort(a)

print(a)

 

Related questions

0 like 0 dislike
0 answers 29 views
0 like 0 dislike
1 answer 55 views
0 like 0 dislike
1 answer 225 views
0 like 0 dislike
1 answer 93 views

 Goeduhub:

About Us | Contact Us || Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 
 Placements:   List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Important Lists: List of NITsList of IITsList of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG)College ReviewsCollege Fest, Events & WorkshopsKnowledge ShareTrainees/Interns After 15-04-2020
Exams & Cutoffs: JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs || Cutoff-GATECutoff_IIT-JEECS-ScopeECE ScopeEE-Scope
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
...