cs32w09dis7

cs32w09dis7 - CS32: Introduction to Computer Science II...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Sorting Algorithms A sorting algorithm puts elements of a lit in a certain order. We will examine some popular sorting algorithms. For algorithms below, assume that our goal is to sort the elements of a list of size n in the ascending order (either numerically or lexicographically). Selection Sort In selection sort, the smallest element is successively found at each iteration of the algorithm. At first, you search for the smallest element in the whole list of size n (0 to n-1). Then you take this smallest element and swap with the first element in the list. After this step, the first one is at the right place. Now we look at elements 1 to n-1 to find the second smallest element. When this element is found, we swap this element with the second element. Can you tell the complexity of this algorithm in a big-O notation? When we’re analyzing sorting algorithms, we are mainly interested in the number of swaps and comparisons. best case = average case = worst case: Insertion Sort Insertion sort is an efficient “human-like” algorithm. Insertion sort works the way many people sort a hand of playing cards. The strategy is that, in your left hand you keep a sorted hand of cards, pick up a new card, and place it in the right place in your left hand, pick up a new card, and so forth, until you run out of cards to pick up. Here is a pseudocode for insertion sort: insertion_sort ( array arr, size n ) { for(i=1 to n-1) { value = arr[i] j = i-1 { arr[j+1] = a[j] j-- } arr[j+1] = value } } CS32: Introduction to Computer Science II Winter 2009 Week 7, Page 5/7 4 3 1 5 2 1 3 4 5 2 1 2 4 5 3 1 2 3 5 4 1 2 3 4 5 4 3 1 5 2 3 4 1 5 2 1 3 4 5 2 1 3 4 5 2 1 2 3 4 5
Background image of page 1

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

View Full DocumentRight Arrow Icon
Can you evaluate the complexity of insertion sort? Best case: Worst case: Average case: Merge Sort Merge sort is one of the representative “divide and conquer” algorithms. You might have seen this when you learned recursion. The idea is to divide the array in half, sort one half of the array, sort another half of the array, and merge the two. We do this recursively, as seen in the following diagram: Very rough pseudocode goes like this: merge_sort ( array arr ) { if( arr’s size = 1 ) return arr create arrays “left” and “right” split arr in half and let “left” = left half “right” = right half return merge(merge_sort(left), merge_sort(right)) } CS32: Introduction to Computer Science II Winter 2009 Week 7, Page 6/7 3 7 6 5 8 2 1 4 3 7 6 5 8 2 1 4 3 7 6 5 8 2 1 4 3 7 6 5 8 2 1 4 3 7 5 6 2 8 1 4 3 5 6 7 1 2 4 8 1 2 3 4 5 6 7 8 split split split merge merge merge
Background image of page 2
merge ( array left (size m), array right (size n) ) { create an array “result” of size m+n i = 0; j = 0; while(i < m and j < n) { if(left[i] < right[j]) { result[i+j] = left[i] i++ } else { result[i+j] = right[j] j++ } } if(i < m) append rest of left array to result else append rest of right array to result return result } It turns out merge is a linear (O(n)) operation (can you see it?), and both splitting and merging need to be
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/07/2009 for the course CS 32 taught by Professor Davidsmallberg during the Winter '08 term at UCLA.

Page1 / 10

cs32w09dis7 - CS32: Introduction to Computer Science II...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online