Simple Sorting Algorithms Review of Quick Sort Pick a pivot, - - PowerPoint PPT Presentation

simple sorting algorithms review of quick sort
SMART_READER_LITE
LIVE PREVIEW

Simple Sorting Algorithms Review of Quick Sort Pick a pivot, - - PowerPoint PPT Presentation

Simple Sorting Algorithms Review of Quick Sort Pick a pivot, arrange other elements based on if theyre greater or less than the pivot Repeat for each smaller group until the whole thing is sorted Usually O(nlogn), absolute worst case is


slide-1
SLIDE 1

Simple Sorting Algorithms

slide-2
SLIDE 2

Review of Quick Sort

2

Pick a pivot, arrange other elements based on if they’re greater or less than the pivot Repeat for each smaller group until the whole thing is sorted Usually O(nlogn), absolute worst case is O(n2)

slide-3
SLIDE 3

3

Bubble Sort

Compare each element (except the last one) with its neighbor to the right

If they are out of order, swap them This puts the largest element at the very end The last element is now in the correct and final place

Compare each element (except the last two) with its neighbor to the right

If they are out of order, swap them This puts the second largest element next to last The last two elements are now in their correct and final places

Compare each element (except the last three) with its neighbor to the right

Continue as above until you have no unsorted elements on the left

slide-4
SLIDE 4

4

Example of Bubble Sort

7 2 8 5 4 2 7 8 5 4 2 7 8 5 4 2 7 5 8 4 2 7 5 4 8 2 7 5 4 8 2 5 7 4 8 2 5 4 7 8 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8 2 5 4 7 8 2 4 5 7 8 2 4 5 7 8 (done)

slide-5
SLIDE 5

5

Can you guess its Big-O notation?

O(n2)

slide-6
SLIDE 6

6

Selection sort

Given a list of length n, Search elements 0 through n-1 and select the smallest Swap it with the element in location 0 Search elements 1 through n-1 and select the smallest Swap it with the element in location 1 Search elements 2 through n-1 and select the smallest Swap it with the element in location 2 Search elements 3 through n-1 and select the smallest Swap it with the element in location 3 Continue in this fashion until there’s nothing left to search

slide-7
SLIDE 7

7

Analysis of Selection Sort

7 2 8 5 4 2 7 8 5 4 2 4 8 5 7 2 4 5 8 7 2 4 5 7 8 Analysis: The outer loop executes n-1 times The inner loop executes about n/2 times

  • n average (from n to 2 times)

Work done in the inner loop is constant (swap two array elements) Time required is roughly (n-1)*(n/2) You should recognize this as O(n2)

slide-8
SLIDE 8

8

Invariants for Selection Sort

For the inner loop:

This loop searches through the array, incrementing inner from its initial value of outer+1 up to a.length-1 As the loop proceeds, min is set to the index of the smallest number found so far Our invariant is:

for all i such that outer <= i <= inner, a[min] <= a[i]

For the outer (enclosing) loop:

The loop counts up from outer = 0 Each time through the loop, the minimum remaining value is put in a[outer] Our invariant is:

for all i <= outer, if i < j then a[i] <= a[j]

slide-9
SLIDE 9

9

Insertion sort

From left to right, go through each element in the list. If it is smaller than the element to its left, check all the elements you’ve already done to see where it belongs. When you find the right place, insert it between the element that is bigger than it and the element that is smaller.

slide-10
SLIDE 10

10

slide-11
SLIDE 11

11

Analysis of insertion sort

We have to check each of the n elements On average, there are n/2 elements already sorted The inner loop looks at (and moves) half of these This gives a second factor of n/4 Hence, the time required for an insertion sort of an array of n elements is proportional to n2/4 Discarding constants, we find that insertion sort is O(n2)

slide-12
SLIDE 12

Mergesort

A L G O R I T H M S

divide

A L G O R I T H M S Divide array into two halves.

slide-13
SLIDE 13

Mergesort

sort

A L G O R I T H M S

divide

A L G O R I T H M S A G L O R H I M S T Divide array into two halves. Recursively sort each half.

slide-14
SLIDE 14

Mergesort

merge sort

A L G O R I T H M S

divide

A L G O R I T H M S A G L O R H I M S T A G H I L M O R S T Divide array into two halves. Recursively sort each half. Merge two halves to make sorted whole.

slide-15
SLIDE 15

auxiliary array smallest smallest

A G L O R H I M S T

Merging

A Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

slide-16
SLIDE 16

auxiliary array smallest smallest

A G L O R H I M S T A

Merging

G Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

slide-17
SLIDE 17

auxiliary array smallest smallest

A G L O R H I M S T A G

Merging

H Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

slide-18
SLIDE 18

auxiliary array smallest smallest

A G L O R H I M S T A G H

Merging

I Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

slide-19
SLIDE 19

auxiliary array smallest smallest

A G L O R H I M S T A G H I

Merging

L Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

slide-20
SLIDE 20

auxiliary array smallest smallest

A G L O R H I M S T A G H I L

Merging

M Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

slide-21
SLIDE 21

auxiliary array smallest smallest

A G L O R H I M S T A G H I L M

Merging

O Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

slide-22
SLIDE 22

auxiliary array smallest smallest

A G L O R H I M S T A G H I L M O

Merging

R Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

slide-23
SLIDE 23

auxiliary array first half exhausted smallest

A G L O R H I M S T A G H I L M O R

Merging

S Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

slide-24
SLIDE 24

auxiliary array first half exhausted smallest

A G L O R H I M S T A G H I L M O R S

Merging

T Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.