

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The analysis of selection algorithm, focusing on choosing a pivot and partitioning, and the ideal rank of x for efficient elimination. It also introduces the concept of sorting, its importance, and various slow o(n^2) sorting algorithms like bubble sort, insertion sort, and selection sort. The document further explains the concept of heaps and heapsort as efficient o(n log n) sorting algorithms.
Typology: Study notes
1 / 3
This page cannot be seen from the preview
Don't miss anything!


We will discuss how to choose a pivot and the partitioning later. For the moment, we will assume that they both take _(n) time. How many elements do we eliminate in each time? If x is the largest or the smallest then we may only succeed in eliminating one element.
5, 9, 2, 6, 4, , 3, 7 pivot is 1
1 , 5, 9, 2, 6, 4, 3, 7 after partition
Ideally, x should have a rank that is neither too large nor too small. Suppose we are able to choose a pivot that causes exactly half of the array to be eliminated in each phase. This means that we recurse on the remaining n/2 elements. This leads to the following recurrence:
If we expand this recurrence, we get
Recall the formula for infinite geometric series; for any |c| < 1,
Using this we have T(n) ≤ 2n 2 Θ(n)
Let’s think about how we ended up with a _(n) algorithm for selection. Normally, a _(n) time algorithm would make a single or perhaps a constant number of passes of the data set. In this algorithm, we make a number of passes. In fact it could be as many as log n.
However, because we eliminate a constant fraction of the array with each phase, we get the convergent geometric series in the analysis. This shows that the total running time is indeed linear in n. This lesson is well worth remembering. It is often possible to achieve linear running times in ways that you would not expect.
Sorting
For the next series of lectures, we will focus on sorting. There a number of reasons for sorting. Here are a few important ones. Procedures for sorting are parts of many large software systems. Design of efficient sorting algorithms is necessary to achieve overall efficiency of these systems.
Sorting is well studied problem from the analysis point of view. Sorting is one of the few problems where provable lower bounds exist on how fast we can sort. In sorting, we are given an array A[1..n] of n numbers We are to reorder these elements into increasing (or decreasing) order.
More generally, A is an array of objects and we sort them based on one of the attributes - the key value. The key value need not be a number. It can be any object from a totally ordered domain. Totally ordered domain means that for any two elements of the domain, x and y, either x < y, x = y or x > y.
4.1 Slow Sorting Algorithms There are a number of well-known slow O(n 2 ) sorting algorithms. These include the following:
Bubble sort: Scan the array. Whenever two consecutive items are found that are out of order, swap them. Repeat until all consecutive items are in order.
Insertion sort: Assume that A[1..i - 1] have already been sorted. Insert A[i] into its proper position in this sub array. Create this position by shifting all larger elements to the right. Selection sort: Assume that A[1..i - 1] contain the i - 1 smallest elements in sorted order. Find the smallest element in A[i..n] Swap it with A[i].
These algorithms are easy to implement. But they run in Θ(n 2 ) time in the worst case.
4.2 Sorting in O(n log n) time We have already seen that Mergesort sorts an array of numbers in _(n log n) time. We will study two others: Heapsort and Quicksort.