Lec19 - Lecture 19: Advanced Sorting PIC 10B Todd Wittman...

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

View Full Document Right Arrow Icon
1 Lecture 19: Advanced Sorting PIC 10B Todd Wittman Basic Sorting Algorithms s Last class, we went over 2 simple sorting algorithms: selection and insertion sort. s Both algorithms are O(N 2 ) on average. s Today we're going to look at some faster and also more complicated algorithms. s When you choose an algorithm, you have to balance the time it will take to write/debug the program with the time it will take to run the program. O(N 2 ) O(N 2 ) O(N) Insertion Sort O(N 2 ) O(N 2 ) O(N 2 ) Selection Sort Worst Average Best
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Divide-And-Conquer s We're first going to look at a sorting algorithm which falls under the category of a divide-and-conquer approach. s Divide-and-conquer has 3 steps: 1. Divide : Break the problem down into smaller sub-problems. 2. Conquer : Solve each sub-problem. 3. Merge : Combine the solutions to the sub- problems into a solution for the whole problem. s For example, our recursive permutation generator is a divide-and-conquer algorithm. To permute N items, we permute N-1 items recursively. Sec 15.4 : Merge Sort s The idea behind merge sort (or mergesort ) is to repeatedly divide the vector in half and sort each smaller piece. s Ex Sort the following vector using merge sort. s To do this, we need a method that can merge two sorted lists together into one vector. s The merge operation should only take O(N) time. s Ex Merge the two sorted lists below. 10 9 3 5 12 1 6 4 5 9 11 13 1 8 10
Background image of page 2
3 Merge Sort s If we have a routine called merge(), the recursive merge sort algorithm is pretty short.
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/19/2011 for the course PIC 10B taught by Professor Wittman during the Winter '08 term at UCLA.

Page1 / 8

Lec19 - Lecture 19: Advanced Sorting PIC 10B Todd Wittman...

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