Download presentation
Presentation is loading. Please wait.
1
CS2420: Lecture 9 Vladimir Kulyukin Computer Science Department Utah State University
2
Outline Sorting Algorithms (Chapter 7)
3
Insertion Sort template void insertionSort(vector & a) { int right, left; T current_card; for(right = 1; right < a.size( ); right++) { left = right; current_card = a[left]; while (left > 0 && current_card < a[left-1] ) { a[left] = a[left-1]; left--; } a[left] = current_card; }
4
Insertion Sort: Asymptotic Analysis
5
Insertion Sort: Average Analysis Let A be an array of distinct elements. If i A[j], then (A[i], A[j]) is an inversion. Let A = [8, 5, 9, 2, 6, 3]. A has 10 inversions: (8, 5), (8, 2), (8, 6), (8, 3), (5, 2), (5, 3), (9, 2), (9, 6), (9, 3), (6, 3).
6
Insertion Sort: Average Analysis One swap operation in the inner for-loop removes exactly one inversion. If there are I inversions, the swap line will be executed I times, i.e., O(I). The outer loop goes through the entire array once. In other words, it spends O(N) amount of time. Thus, the insertion sort runs in O(I) + O(N) = O(I + N).
7
InsertSort: Average Analysis We assume that all arrays of N distinct elements are equiprobable. The sample space is the set of all arrays of N distinct elements. The size of the sample space is N!. For any array A of N distinct elements, we know that its reverse is also a point in the sample space.
8
Theorem
9
Proof
10
Corollary
11
The Lessons of Insertion Sort We cannot do any better than quadratic time if we swap adjacent elements. If we want to improve the running time of comparison sorts we must start comparing elements that are far apart.
12
Divide and Conquer Design Divide: A problem is divided into independent sub-problems. Conquer: Each sub-problem is solved. Combine: Solutions to the sub-problems are combined into one solution to the problem.
13
Divide and Conquer: Sorting Divide: An array of numbers is divided into 2 sub-arrays. Conquer: The 2 sub-arrays are sorted independently. Combine: The 2 sorted sub-arrays are merged into one sorted sub-array.
14
Merge Sort Let A be an array of numbers. Divide: A is divided into two sub-arrays Left and Right. Conquer: The sub-arrays are sorted recursively. Combine: The sub-arrays are merged into one sorted array.
15
2 Stages of MergeSort 5, 2, 4, 6, 1, 3, 2, 6 5, 2, 4, 6 1, 3, 2, 6 5, 24, 62, 61, 3 5 1326 2 46 2, 54, 61, 32, 6 2, 4, 5, 61, 2, 3, 6 1, 2, 2, 3, 4, 5, 6, 6 Stage 1 Stage 2
16
Merging Two Sorted Arrays Allocate a new array big enough to hold the numbers from the two arrays. Put a cursor on the 1 st element of the left array and another cursor on 1 st element of the right array. Compare the two numbers the cursors are pointing to. Put the smaller number into the result array and advance the appropriate cursor. Take care of the leftovers.
17
MergeSort Code template void mergeSort(vector &a, vector &tmp, int left, int right) { if ( left < right ) { mid = (left + right)/2; mergeSort(a, tmp, left, mid); mergeSort(a, tmp, mid+1, right); merge(a, tmp, left, mid+1, right); }
18
MergeSort Recurrence Given an array of 1 element, mergeSort runs in constant time. Given an array of N > 1 elements, – mergeSort divides the array into two subarrays containing N/2 elements each, – merges the two subarrays into the result array of N elements.
19
MergeSort Recurrence
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.