{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Merge Sort &amp; Quick Sort

# Merge Sort &amp; Quick Sort - Lecture 20 Advanced...

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

1 Lecture 20: 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

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

View Full Document
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 11.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
3 Merge Sort square6 If we have a routine called merge(), the recursive merge sort algorithm is pretty short.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 8

Merge Sort &amp; Quick Sort - Lecture 20 Advanced...

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

View Full Document
Ask a homework question - tutors are online