{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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 square6 Last class, we went over 2 simple sorting algorithms: selection and insertion sort. square6 Both algorithms are O(N 2 ) on average. square6 Today we're going to look at some faster and also more complicated algorithms. square6 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 Document Right Arrow Icon
2 Divide-And-Conquer square6 We're first going to look at a sorting algorithm which falls under the category of a divide-and-conquer approach. square6 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. square6 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 square6 The idea behind merge sort (or mergesort ) is to repeatedly divide the vector in half and sort each smaller piece. square6 Ex Sort the following vector using merge sort. square6 To do this, we need a method that can merge two sorted lists together into one vector. square6 The merge operation should only take O(N) time. square6 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 square6 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 Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}