05. Bestiary

# 05. Bestiary - Maggie Johnson Handout #5 CS103B A Bestiary...

This preview shows pages 1–3. Sign up to view the full content.

Maggie Johnson Handout #5 CS103B A Bestiary of Algorithms Key topics: Selection Sort Bubble Sort Insertion Sort (and Comparisons) Quick Sort Merge Sort Heap Sort (and Comparisons) Radix Sort Linear Search Binary Search Hashing Knapsack Problem Revisited Undecidable Problems Sorting Techniques : Complexity Selection Sort Bubble Sort Insertion Sort Quick Sort Merge Sort Heap Sort Radix Sort Search Techniques : Linear Search Binary Search Hashing Does choice of data structures in the implementation of these techniques make a difference in running time?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Selection Sort The idea of selection sort is rather simple: we repeatedly find the next largest (or smallest) element in the array and move it to its final position in the sorted array. Assume that we wish to sort the array in increasing order, i.e. the smallest element at the beginning of the array and the largest element at the end. We begin by selecting the largest element and moving it to the highest index position. We can do this by swapping the element at the highest index and the largest element. We then reduce the effective size of the array by one element and repeat the process on the smaller (sub)array. The process stops when the effective size of the array becomes 1 (an array of 1 element is already sorted). For example, consider the following array, shown with array elements in sequence separated by commas: The leftmost element is at index zero, and the rightmost element is at the highest array index, in our case, 4 (the effective size of our array is 5). The largest element in this effective array (index 0-4) is at index 2. We then swap the element at index 2 with that at index 4. The result is: We reduce the effective size of the array to 4, making the highest index in the effective array now 3. The largest element in this effective array (index 0-3) is at index 1, so we swap elements at index 1 and 3: The next two steps give us: The last effective array has only one element and needs no sorting. The entire array is now sorted. The algorithm for an array, x , with lim elements is easy to write down: for (eff_size = lim; eff_size > 1; eff_size--) find maxpos, the location of the largest element in the effective array: index 0 to eff_size - 1 swap elements of x at index maxpos and index eff_size – 1 Bubble Sort An alternate way of putting the largest element at the highest index in the array uses an algorithm called bubble sort . While this method is neither as efficient, nor as straightforward, as selection sort, it is popularly used to illustrate sorting. Like selection sort, the idea of bubble sort is to repeatedly move the largest element to the highest index position of the array. As in selection sort, each iteration reduces the effective size of the array. The two algorithms differ in how this is done. Rather than search the entire effective array to find the largest element, bubble sort focuses on successive adjacent pairs of elements in the
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/01/2011 for the course CS 103B taught by Professor Sahami,m during the Winter '08 term at Stanford.

### Page1 / 14

05. Bestiary - Maggie Johnson Handout #5 CS103B A Bestiary...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online