Skip to main content

Merge Sort in Python

     

Merge Sort in Python 


    In computer science merge sort is a most efficient sorting algorithm with time complexity O(nlogn). It famous algorithm to sort elements by using divide and conquer method.    

     Input:

        25 45 30 60 98
    Output: 
       [25,30,45,60,98]

      Explanation:
                [25 45 30 60 98]
                       /     \
       [25,45]       [30,60,98]
        /    \             /      \ 
    [25] [45]   [30]   [60,98]
      \       /          /      /    \
        \    /          /     [60] [98]
        [25,45] [30]   [60,98]
          \             \        /
           \          [30,60,98]
            \            /
          [25,30,45,60,98] {sorted list}

      Code:     

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
   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
  return arr
array=list(map(int,input().split()))
print(mergeSort(array))


Note:

    Hello Guys, Don't Stop Learning keep Going.............. 






Popular posts from this blog

Python Program to Convert Hectare to Acre

             Python Program to Convert Hectare to Acre       Generally 1 hectare = 2.471 acres. It is used find the area of the lands or farming. It is comes under unit conversions.       Input:          1      Out put:           2.471044            Explanation :                    Generally 1 hectare = 2.471044 acres                       Code:          hectare=int(input())           print(hectare*2.471044)           Note:     Hello Guys, Don't Stop Learning keep Going..............

Python Program to check given number is Prime Number or Not

     Python Program to check given number is Prime Number or Not       A number Greater than 1 which has only two factors. They are 1 and itself. In computer science prime number concept is very interesting and most important in cryptography.          Input:          7      Out put:          prime         Explanation :           Factors for 7 is 1 and itself then it is prime number.      Code:         x=int(input()) c=0 for i in range(2,x):      if(x%i==0):           c=1           break if(c==1):      print("not a prime") else:      print("prime") Note:     Hello Guys, Don't Stop Learning keep Going..............  

Python Program to Check whether a character is a Vowel or Consonant

                  Python Program to Check whether a character is a Vowel or Consonant   In this we are going to learn given character is vowel or Consonant. Generally there are 5 vowels they are a,e,i,o,u and remaining are the consonants in alphabets of 26. we can write this program by using strings. string is a collection of characters.         Input:        k      Out put:           Consonant                Explanation :             vowels={a,e,i,o,u,A,E,I,O,U}          if input character in the above list then vowels, else consonant                   Code:         r=input()       # input string         s="aeiou"    ...