



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




























Take as input a sequence of 2 n real numbers. Design an O(n log n) algorithm that partitions the numbers into n pairs, with the property that the partition minimizes the maximum sum of a pair. For example, say we are given the numbers (1,3,5,9). The possible partitions are ((1,3),(5,9)), ((1,5),(3,9)), and ((1,9),(3,5)). The pair sums for these partitions are (4,14), (6,12), and (10,8). Thus the third partition has 10 as its maximum sum, which is the minimum over the three partitions.
Why don’t CS profs ever stop talking about sorting?
You should have seen most of the algorithms - we will concentrate on the analysis. Docsity.com
Sorting is important because that once a set of items is sorted, many other problems become easy. Further, using O(n log n) sorting algorithms leads naturally to sub-quadratic algorithms for these problems.
n n^2 / 4 n lg n 10 25 33 100 2,500 664 1,000 250,000 9, 10,000 25,000,000 132, 100,000 2,500,000,000 1,660,
Large-scale data processing would be impossible if sorting took Ω(n^2 ) time. Docsity.com
Given n numbers, find the pair which are closest to each other. Once the numbers are sorted, the closest pair will be next to each other in sorted order, so an O(n) linear scan completes the job.
Given a set of n items, are they all unique or are there any duplicates? Sort them and do a linear scan to check all adjacent pairs. This is a special case of closest pair above.
What is the kth largest item in the set? Once the keys are placed in sorted order in an array, the kth largest can be found in constant time by simply looking in the kth position of the array. There is a linear time algorithm for this problem, but the idea comes from partial sorting.
Given n points in two dimensions, find the smallest area polygon which contains them all.
The convex hull is like a rubber band stretched over the points. Convex hulls are the most important building block for more sophisticated geometric algorithms.
Alphabetizing is the sorting of text strings. Libraries have very complete and complicated rules con- cerning the relative collating sequence of characters and punctuation. Is Skiena the same key as skiena? Is Brown-Williams before or after Brown America before or after Brown, John? Explicitly controlling the order of keys is the job of the comparison function we apply to each pair of elements. This is how you resolve the question of increasing or decreasing order.
Elements with equal key values will all bunch together in any total order, but sometimes the relative order among these keys matters. Sorting algorithms that always leave equal items in the same relative order as in the original permutation are called stable. Unfortunately very few fast algorithms are stable, but Stability can be achieved by adding the initial position as a secondary key.
Selection sort scans throught the entire array, repeatedly finding the smallest remaining element.
For i = 1 to n A: Find the smallest of the first n − i + 1 items. B: Pull it out of the array and put it first.
Selection sort takes O(n(T (A) + T (B)) time.
Using arrays or unsorted linked lists as the data structure, operation A takes O(n) time and operation B takes O(1), for an O(n^2 ) selection sort. Using balanced search trees or heaps, both of these operations can be done within O(lg n) time, for an O(n log n) selection sort, balancing the work and achieving a better tradeoff. Key question: “Can we use a different data structure?”
Heaps maintain a partial order on the set of elements which is weaker than the sorted order (so it can be efficient to maintain) yet stronger than random order (so the minimum element can be quickly identified).
4 6 7
1 2
5
10
3
8 9 1941
2001
1918
1963
1804 1945
1865
1492 1783 1776 1783
2001 1941
1865 1918
1492
1804
1776 1945 1963
A heap-labeled tree of important years (l), with the corre- sponding implicit heap representation (r) Docsity.com
The most natural representation of this binary tree would involve storing each key in a node with pointers to its two children. However, we can store a tree as an array of keys, usiing the position of the keys to implicitly satisfy the role of the pointers. The left child of k sits in position 2 k and the right child in 2 k + 1. The parent of k is in position bn/ 2 c.