Merge Sort is an efficient sorting algorithm that utilizes a divide and conquer strategy to sort an array by dividing it into halves, recursively sorting them, and then merging the sorted halves. It has a consistent time complexity of O(n log n) across best, average, and worst cases, with a space complexity of O(n). Merge Sort is stable, not in-place, and widely used in practical applications.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as KEY, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
20 views10 pages
Merge Sort Presentation
Merge Sort is an efficient sorting algorithm that utilizes a divide and conquer strategy to sort an array by dividing it into halves, recursively sorting them, and then merging the sorted halves. It has a consistent time complexity of O(n log n) across best, average, and worst cases, with a space complexity of O(n). Merge Sort is stable, not in-place, and widely used in practical applications.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as KEY, PDF, TXT or read online on Scribd
You are on page 1/ 10
Merge Sort
Efficient Sorting Algorithm
Presented by: [Your Name/Class] Introduction to Merge Sort
- Merge Sort is a Divide and Conquer algorithm.
- It divides the input array into two halves, sorts them, and then merges them. - It is known for its efficiency and predictable performance. Merge Sort Theory
- Divide: Split the array into halves.
- Conquer: Recursively sort the subarrays. - Combine: Merge the sorted subarrays to produce the sorted result. - Merge Sort is stable and not in-place. Merge Sort Algorithm (Step-by-step)
1. If the array has 1 or 0 elements, it is already sorted.
2. Divide the array into two halves. 3. Recursively apply merge sort on both halves. 4. Merge the two sorted halves into one sorted array. Dry Run of Merge Sort
Input: [38, 27, 43, 3, 9, 82, 10]
- Divide: [38, 27, 43] and [3, 9, 82, 10] - Further divide until single elements: [38], [27], [43], etc. - Merge: [27, 38], [27, 38, 43]... - Final Merge: [3, 9, 10, 27, 38, 43, 82] Time Complexity Analysis
Case | Time Complexity
------------|---------------- Best Case | O(n log n) Average Case| O(n log n) Worst Case | O(n log n) - Space Complexity: O(n) - Consistent performance regardless of input order Merge Sort Code (in C) void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m; int L[n1], R[n2]; for (int i = 0; i < n1; i++) L[i] = arr[l + i]; for (int j = 0; j < n2; j++) R[j] = arr[m + 1 + j]; int i = 0, j = 0, k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) arr[k++] = L[i++]; else arr[k++] = R[j++]; } while (i < n1) arr[k++] = L[i++]; while (j < n2) arr[k++] = R[j++]; } void mergeSort(int arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; mergeSort(arr, l, m); mergeSort(arr, m + 1, r); merge(arr, l, m, r); } } Example with All Cases
Best Case: Already sorted: [1, 2, 3, 4, 5] → Still O(n log n)
Average Case: Random order: [3, 5, 1, 4, 2] Worst Case: Reverse order: [5, 4, 3, 2, 1] - In all cases, time complexity remains the same! Summary
- Merge Sort is efficient and predictable.
- Uses divide and conquer strategy. - Performs well in all scenarios. - Widely used in practical applications. WELCOME YOU ALL !