Docsity

Slides on Sorting - Object-Oriented Programming and Data Structures | CS 2110, Study notes of Computer Science

Toggle sidebar
bg1
Sorting
Lecture 12
CS211 – Summer 2008
2
InsertionSort
yMany people sort cards this way
yInvariant: everything to left of iis
already sorted
yWorks especially well when input
is nearly sorted
yWorst-case is O(n2)
Consider reverse-sorted input
yBest-case is O(n)
Consider sorted input
yExpected case is O(n2)
Expected number of inversions is
n(n–1)/4
//sort a[], an array of int
for (int i = 1; i < a.length; i++) {
int temp = a[i];
int k;
for (k = i; 0 < k && temp < a[k–1]; k ––)
a[k] = a[k–1];
a[k] = temp;
}
3
SelectionSort
yTo sort an array of size n:
Examine a[0] to a[n–1]; find
the smallest one and swap it with
a[0]
Examine a[1] to a[n–1]; find
the smallest one and swap it with
a[1]
In general, in step i, examine
a[i] to a[n–1]; find the
smallest one and swap it with
a[i]
yThis is the other common way for
people to sort cards
yRuntime
Worst-case O(n2)
Best-case O(n2)
Expected-case O(n2)
4
Divide & Conquer?
yIt often pays to
Break the problem into smaller subproblems,
Solve the subproblems separately, and then
Assemble a final solution
yThis technique is called divide-and-conquer
Caveat: It won’t help unless the partitioning and assembly
processes are inexpensive
yCan we apply this approach to sorting?
5
MergeSort
yQuintessential divide-and-conquer algorithm
yDivide array into equal parts, sort each part, then merge
yQuestions:
Q1: How do we divide array into two equal parts?
A1: Find middle index: a.length/2
Q2: How do we sort the parts?
A2: call MergeSort recursively!
Q3: How do we merge the sorted subarrays?
A3: We have to write some (easy) code
6
Merging Sorted Arrays Aand B
yCreate an array Cof size = size of A+ size of B
yKeep three indices:
iinto A
jinto B
kinto C
yInitialize all three indices to 0(start of each array)
yCompare element A[i] with B[j], and move the smaller
element into C[k]
yIncrement ior j, whichever one we took, and k
yWhen either Aor Bbecomes empty, cop y remaining elements
from the other array (Bor A, respectively) into C
pf3
pf4

Partial preview of the text

Download Slides on Sorting - Object-Oriented Programming and Data Structures | CS 2110 and more Study notes Computer Science in PDF only on Docsity!

Sorting

Lecture 12 CS211 – Summer 2008

2

InsertionSort

y Many people sort cards this way y Invariant: everything to left of i is already sorted y Works especially well when input is nearly sorted

y Worst-case is O(n^2 ) ƒ Consider reverse-sorted input y Best-case is O(n) ƒ Consider sorted input y Expected case is O(n^2 ) ƒ Expected number of inversions is n(n–1)/

//sort a[], an array of int for (int i = 1; i < a.length; i++) { int temp = a[i]; int k; for (k = i; 0 < k && temp < a[k–1]; k––) a[k] = a[k–1]; a[k] = temp; }

3

SelectionSort

y To sort an array of size n: ƒ Examine a[0] to a[n–1] ; find the smallest one and swap it with a[0] ƒ Examine a[1] to a[n–1] ; find the smallest one and swap it with a[1] ƒ In general, in step i, examine a[i] to a[n–1] ; find the smallest one and swap it with a[i]

y This is the other common way for people to sort cards

y Runtime ƒ Worst-case O(n 2 ) ƒ Best-case O(n 2 ) ƒ Expected-case O(n 2 )

4

Divide & Conquer?

y It often pays to

ƒ Break the problem into smaller subproblems, ƒ Solve the subproblems separately, and then ƒ Assemble a final solution

y This technique is called divide-and-conquer

ƒ Caveat: It won’t help unless the partitioning and assembly processes are inexpensive

y Can we apply this approach to sorting?

MergeSort

y Quintessential divide-and-conquer algorithm

y Divide array into equal parts, sort each part, then merge

y Questions:

ƒ Q1: How do we divide array into two equal parts? ƒ A1: Find middle index: a.length/

ƒ Q2: How do we sort the parts? ƒ A2: call MergeSort recursively!

ƒ Q3: How do we merge the sorted subarrays? ƒ A3: We have to write some (easy) code

Merging Sorted Arrays A and B

y Create an array C of size = size of A + size of B y Keep three indices: ƒ i into A ƒ j into B ƒ k into C y Initialize all three indices to 0 (start of each array) y Compare element A[i] with B[j] , and move the smaller element into C[k] y Increment i or j , whichever one we took, and k y When either A or B becomes empty, copy remaining elements from the other array ( B or A , respectively) into C

7

1 3 4 4 6 7

Merging Sorted Arrays

C = merged array

B

A

1 3 4 6 8

k 4 7 7 8 9

i

j

8

MergeSort Analysis

y Outline (detailed code on the website) ƒ Split array into two halves ƒ Recursively sort each half ƒ Merge the two halves

y Merge = combine two sorted arrays to make a single sorted array ƒ Rule: always choose the smallest item ƒ Time: O(n) where n is the combined size of the two arrays

y Runtime recurrence ƒ Let T(n) be the time to sort an array of size n T(n) = 2T(n/2) + O(n) T(1) = 1

y Can show by induction that T(n) is O(n log n)

y Alternately, can see that T(n) is O(n log n) by looking at tree of recursive calls

9

MergeSort Notes

y Asymptotic complexity: O(n log n)

ƒ Much faster than O(n^2 )

y Disadvantage

ƒ Need extra storage for temporary arrays ƒ In practice, this can be a disadvantage, even though MergeSort is asymptotically optimal for sorting ƒ Can do MergeSort in place, but this is very tricky (and it slows down the algorithm significantly)

y Are there good sorting algorithms that do not use so

much extra storage?

ƒ Yes: QuickSort

10

QuickSort

y Intuitive idea

ƒ Given an array A to sort, choose a pivot value p ƒ Partition A into two subarrays, AX and AY Š AX contains only elements ≤ p Š AY contains only elements ≥ p ƒ Sort subarrays AX and AY separately ƒ Concatenate (not merge!) sorted AX and AY to get sorted A Š Concatenation is easier than merging – O(1)

20 31 24 19 45 56 4 65 5 72 14 99

pivot partition

(^5 ) 14

4

31 72

56

(^65 )

24

99

4 5 14 19 20 24 31 45 56 65 72 99

QuickSort QuickSort

4 5 14 19 20 24 31 45 56 65 72 99

concatenate

QuickSort Questions

y Key problems ƒ How should we choose a pivot? ƒ How do we partition an array in place?

y Partitioning in place ƒ Can be done in O(n) time (next slide)

y Choosing a pivot ƒ Ideal pivot is the median, since this splits array in half ƒ Computing the median of an unsorted array is O(n), but algorithm is quite complicated ƒ Popular heuristics: Š Use first value in array (usually not a good choice) Š Use middle value in array Š Use median of first, last, and middle values in array Š Choose a random element

19

Comparison Trees

y Comparison-based algorithms make decisions based on comparison of data elements

y This gives a comparison tree y If the algorithm fails to terminate for some input, then the comparison tree is infinite

y The height of the comparison tree represents the worst-case number of comparisons for that algorithm

y Can show that any correct comparison-based algorithm must make at least n log n comparisons in the worst case

a[i] < a[j] no yes

20

Lower Bound for Comparison Sorting

y Say we have a correct comparison-based algorithm

y Suppose we want to sort the elements in an array B[]

y Assume the elements of B[] are distinct

y Any permutation of the elements is initially possible

y When done, B[] is sorted

y But the algorithm could not have taken the same path in the comparison tree on different input permutations

y How many input permutations are possible? n! ~ 2n log n

y For a comparison-based sorting algorithm to be correct, it must have at least that many leaves in its comparison tree

y to have at least n! ~ 2n log n^ leaves, it must have height at least n log n (since it is only binary branching, the number of nodes at most doubles at every depth)

y therefore its longest path must be of length at least n log n, and that it its worst-case running time

Lower Bound for Comparison Sorting