Se ha denunciado esta presentación.
Se está descargando tu SlideShare. ×
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Anuncio
Próximo SlideShare
Merge sort algorithm
Merge sort algorithm
Cargando en…3
×

Eche un vistazo a continuación

1 de 9 Anuncio

Más Contenido Relacionado

Similares a advanced algo (20)

Más reciente (20)

Anuncio

advanced algo

  1. 1. NETAJI SUBASH ENGINEERING COLLEGE Topic:- Merge Sort  PRESENTED BY: PRADEEP KUMAR  SUBJECT: ADVANCED ALGORITHMS UNIVERSITY ROLL NO. :10900220021 CLASS ROLL NO. :21 STREAM :IT SEC :'A' SEM:-(6TH)
  2. 2. Merge Sort INTRODUCTION Merge Sort is a Divide and Conquer algorithm. It divides the 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. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one in a sorted manner.
  3. 3. Merge Sort Algorithm Merge Sort Algorithm works in the following steps-  It divides the given unsorted array into two halves- left and right sub arrays.  The sub arrays are divided recursively.  This division continues until the size of each sub array becomes 1.  After each sub array contains only a single element, each sub array is sorted trivially.  Then, the above discussed merge procedure is called.  The merge procedure combines these trivially sorted arrays to produce a final sorted array.
  4. 4.  Merge Sort Code  // A : Array that needs to be sorted  MergeSort(A)  {  n = length(A)  if n<2 return  mid = n/2  left = new_array_of_size(mid) // Creating temporary array for left  right = new_array_of_size(n-mid) // and right sub arrays  for(int i=0 ; i<=mid-1 ; ++i)  {  left[i] = A[i] // Copying elements from A to left  }  for(int i=mid ; i<=n-1 ; ++i)  {  right[i-mid] = A[i] // Copying elements from A to right  }  MergeSort(left) // Recursively solving for left sub array  MergeSort(right) // Recursively solving for right sub array  merge(left, right, A) // Merging two sorted left/right sub array to final array  }
  5. 5. The Merge Sort Algorithm Works As-(Example) • The following diagram from shows the complete merge sort process for an example array {38, 27, 43, 3, 9, 82, 10}. If we take a closer look at the diagram, we can see that the array is recursively divided in two halves till the size becomes 1. Once the size becomes 1, the merge processes comes into action and starts merging arrays back till the complete array is merged.
  6. 6. Time Complexity Analysis-  In merge sort, we divide the array into two (nearly) equal halves and solve them recursively using merge sort only. So we have: Finally, we merge these two sub arrays using merge procedure which takes Θ(n) time If T(n) is the time required by merge sort for sorting an array of size n, then the recurrence relation for time complexity of merge sort is- On solving this recurrence relation, we get T(n) = Θ(nlogn). Thus, time complexity of merge sort algorithm is T(n) = Θ(nlogn).
  7. 7. Time complexity of Merge Sort is Θ(nLogn) in all 3 cases (worst, average and best) as merge sort always divides the array in two halves and take linear time to merge two halves. Space Complexity Analysis-  Merge sort uses additional memory for left and right sub arrays.  Hence, total Θ(n) extra memory is needed.
  8. 8. Some of the important properties of merge sort algorithm are Merge sort uses a divide and conquer paradigm for sorting. Merge sort is a recursive sorting algorithm. Merge sort is a stable sorting algorithm. Merge sort is not an in-place sorting algorithm. The time complexity of merge sort algorithm is Θ(nlogn). The space complexity of merge sort algorithm is Θ(n).
  9. 9. THANK YOU

×