Top Banner
ISOM MIS 215 Module 7 – Sorting
23

ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

Dec 17, 2015

Download

Documents

Eric McDowell
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

MIS 215 Module 7 – Sorting

Page 2: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Where are we?

2

Intro toJava, Course

Java lang.basics

Arrays

Introduction

Newbie Programmers Developers ProfessionalsDesigners

MIS215

Binary Search

SearchTechniques Sorting Techniques

Bubblesort

Basic Algorithms

Fast Sorting algos(quicksort, mergesort)

Hashtables

Graphs,Trees

LinkedLists

Stacks,Queues

List Structures Advanced structures

Page 3: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Today’s buzzwords• Bubblesort

The sorting method in which the big elements “bubble” up to the top

• Selection sort The sorting method where you “select” the smallest item to be placed in the appropriate place

• Insertion sort The sorting method where an item from the unsorted part of the array is “inserted” in the

sorted part

• Divide-and-conquer techniques Techniques where the problem space is divided, solved separately, and merged Typically results in more efficient solutions

• Shellsort An advanced version of insertion sort where elements are inserted into their appropriate

intervals

• Mergesort The divide-and-conquer sorting technique that divides the array into two, sorts them

separately and merges them

• Partitioning The process of dividing an array into two parts based on the value of a “pivot” element

• Quicksort The sorting technique where the array is partitioned and sorted separately

Page 4: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Basic Sorting Techniques

• Bubble Sort

• Selection Sort

• Insertion Sort

• Sorting Objects

• Comparing the simple sorts

Page 5: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Bubble sort – what is it?

• The simplest sorting strategy – at most 5-6 lines of code

• The large elements “bubble up” to the end of the list

Page 6: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Bubble sort – basic strategy

• Start at left end• Compare two adjacent items, put bigger one in

the right positionany reason why not the smaller one?what to do if they’re equal?

• Move to the right, repeat the comparison till you get to the (right) end

• What have you got?biggest one at the right end

• Now, start at the left end again, but move how far? Only to the right end minus 1. Repeating..

• Why is this called a “bubble” sort?

Page 7: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Bubble sort - analysis

• How many comparisons?

• How many swaps?

• What’s the best case?

• What’s the worst case?

Page 8: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOMSelection sort

• Start at left end• Keep track of smallest; when you find a new

smaller, swap it to left endany reason why not the biggest?what to do if equal?

• Move to the right, repeating...• What have you got?

smallest at the left end

• Now start at left end plus 1, repeating…

Page 9: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Selection sort - analysis

• How many comparisons?

• How many copies?

• Best case?

• Worst case?

Page 10: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Insertion sort

• Start at left end plus one• put this item in its proper place

among the ones on its left• now move one position to the right,

put this item in its proper place among the ones the left, repeat....

Page 11: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Insertion sort - analysis

• How many comparisons?

• How many swaps?

• Best case?

• Worst case?

Page 12: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Efficiencies

• For random data (which means what?), all 3 run in O(N2)so if it took 20 sec to sort 10,000 items, how

long will it take to sort 20,000?

• Remember this ignores the constants...so...under the right conditions, one runs faster

than another for smaller N, selection sort may be faster than

bubble sort for data already nearly in order, insertion runs

in (nearly) O(N)

Page 13: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Divide-and-Conquer techniques

• Take a problem space, divide into two, solve each, merge solution

• Why is this better?Often you can disregard one of the partitions!You can only divide into 2 log n timesSo, if merging takes O(1), we get total O(lg n)!And if merging takes O(n), we get total O(n lg n)!

• O(lg n) is way better than O(n)• O(n lg n) is way better than O(n2)

Page 14: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Mergesort – a divide-conquer sorting strategy

• Divide the array into two equal (almost) halves

• Sort each half separatelyCan call mergesort recursively!

• Merge the two halvesO(n) time complexity if we use O(n) extra spaceO(n) complexity even if we do not use extra

space – how?

• Total runtime is O(n lg n) – average and optimal case.

Page 15: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Recursion Tree for Mergesort:An Example of 7 numbers

26 33 35 29 19 12 22 12 19 22 26 29 33 35

26 33 35 29 26 29 33 35

26 33 26 33 35 29

3326 2935

22

1219

29 35 19 12 12 19

12 19 2219 12 22

Start Finish

Page 16: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Divide and Conquer Sorting:Quicksort

In Quicksort

We first choose some key from the list for which, we hope, about half the keys will come before and half after. Call this key the pivot. Then we partition the items so that all those with keys less than the pivot come in one sublist, and all those with greater keys come in another. Then we sort the two reduced lists separately, put the sublists together, and the whole list will be in order.

Page 17: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Trace of Quicksort:Example of the Same 7 Numbers

Partition into (12) and (22); pivot=19Sort (12)Sort (22)Combine into (12, 19, 22)

Partition into (19, 12, 22) and (33, 35, 29); pivot=26

Sort(19, 12, 22)

Sort(19, 12, 22)Partition into (29) and (35); pivot=33Sort (29)Sort (35)Combine into (29, 33, 35)

Combine into (12, 19, 22, 26, 29, 33, 35)

Sort (26,33,35,29,19,12,22)

Page 18: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Recursion Tree for Quicksort:Example of the Same 7 Numbers

26

35292212

3319

Page 19: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Quicksort:Contiguous Implementation

<p p > = p

low pivotlovation high

p

low lastsmall i

<p > = p ?

Goal (Postcondition):

Loop Invariant:

Page 20: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Quicksort Implementation:(Contd.)

p

Restore the Invariant:

Final Position:

p

lastsmall i

<p > = p ?

p

Swap

lastsmall i

<p > = p <p ?

<p > = p

low lastsmall high

Page 21: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Analysis of Quicksort

• Worst-case analysis:If the pivot is chosen poorly, one of the partitioned sublists may be empty and others reduced by only one entry. In this case, quicksort is slower than either insertion sort or selection sort.

• Average-case analysis:In its average case, quicksort performs

O(n lg n)

comparisons of keys in sorting a list of n entries in random order.

Page 22: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Quicksort: Choice of Pivot

• First or last entry: Worst case appears for a list already sorted or in reverse order.

• Median of three entry: Poor cases are rare.

• Random entry: Poor cases are very unlikely to occur. Ensure that the random number generation algorithm is fast.

Page 23: ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.

ISOM

Pointers and Pitfalls

• Many computer systems have a general-purpose sorting utility. If you can access this utility and it proves adequate for your application, then use it rather than writing a sorting program from scratch.

• In choosing a sorting method, take into account the ways in which the keys will usually be arranged before sorting, the size of the application, the amount of time available for programming, the need to save computer time and space, the way in which the data structures are implemented, the cost of moving data and cost of comparing keys.

• Divide-and-conquer is one of the most widely applicable and most powerful methods for designing algorithms. When faced with programming problem, see if its solution can be obtained by first solving the problem for two (or more) problems of the same general form but of a smaller size. If so, you may be able to formulate an algorithm that uses the divide-and-conquer method and program it using recursion.