sorting algorithms v01

83
Sorting Algorithms 2

Upload: dusan-vuckovic

Post on 21-Jan-2015

880 views

Category:

Education


10 download

DESCRIPTION

MCAST example . Sorting algorithms

TRANSCRIPT

Page 1: Sorting algorithms v01

Sorting Algorithms

2

Page 2: Sorting algorithms v01

Instructions

• For the embedded lessons look for the Icon

Click on it to present embedded lessons

Page 3: Sorting algorithms v01

Lecture Objectives

• Become aware of what is sorting. • Become aware of sorting algorithms.• Explore the types of sorting algorithms. • Learn about Bubble Sort.• Learn about Insertion Sort.• Learn about Selection Sort.• Learn about Merge Sort.

Page 4: Sorting algorithms v01

What is sorting?

An operation that puts (organizes) elements of a list (a collection of data) into certain order (ascending or descending order).

Sorting = ordering.Sorted = ordered based on a particular

way. 3 1 6 2 1 3 4 5 9 0

0 1 1 2 3 3 4 5 6 9

9 6 5 4 3 3 2 1 1 0

Ascending or descending order

Page 5: Sorting algorithms v01

Sorting – formal definition

Input: A sequence of n numbers: a1, a2, ..., an

Output: A permutation (reordering):

ak1, ak2, ..., akn

of the input sequence such that

f(ak1) f(ak2) ... f(akn)

where f is an ordering function

Permutation

Page 6: Sorting algorithms v01

Examples of Sorting

– Words in a dictionary are sorted. – Files in a directory are often listed in sorted order.– In a newspaper, the calendar of events in a schedule is generally

sorted by date.– In a record store musical compact disks are generally sorted by

recording artist.

Page 7: Sorting algorithms v01

What is a Sorting Algorithm?An algorithm for sorting a set of elements.

3 1 6 2 1 3 4 5 9 0

0 1 1 2 3 3 4 5 6 9

9 6 5 4 3 3 2 1 1 0

Page 8: Sorting algorithms v01

Types of Sorting Algorithms

There are many, many different types of sorting algorithms, but the primary ones are:

• Bubble Sort• Selection Sort• Insertion Sort• Merge Sort• Quick Sort• Shell Sort

Page 9: Sorting algorithms v01

Selection Sort

One of the simplest sorting algorithms works as follows:

– Find the smallest element and exchange it with the element in the first position

– Find the second smallest element and exchange it with the element in the second position

– Continuing in this way until the entire array is sorted.

Name “selection sort” because it works by repeatedly “selecting“ the smallest remaining

element and exchanging it with the i-th element in i-th iteration.

Meaning of words

Page 10: Sorting algorithms v01

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Selection Sort

Page 11: Sorting algorithms v01

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Selection Sort

Page 12: Sorting algorithms v01

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Selection Sort

Page 13: Sorting algorithms v01

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Selection Sort

Page 14: Sorting algorithms v01

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Selection Sort

Page 15: Sorting algorithms v01

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Selection Sort

Page 16: Sorting algorithms v01

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Selection Sort

Page 17: Sorting algorithms v01

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Largest

Selection Sort

Page 18: Sorting algorithms v01

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Selection Sort

Page 19: Sorting algorithms v01

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Selection Sort

Page 20: Sorting algorithms v01

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Selection Sort

Page 21: Sorting algorithms v01

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Selection Sort

Page 22: Sorting algorithms v01

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Selection Sort

Page 23: Sorting algorithms v01

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Selection Sort

Page 24: Sorting algorithms v01

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Selection Sort

Page 25: Sorting algorithms v01

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Largest

Selection Sort

Page 26: Sorting algorithms v01

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

Page 27: Sorting algorithms v01

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

Page 28: Sorting algorithms v01

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

Page 29: Sorting algorithms v01

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

Page 30: Sorting algorithms v01

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

Page 31: Sorting algorithms v01

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

Page 32: Sorting algorithms v01

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Largest

Selection Sort

Page 33: Sorting algorithms v01

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

Page 34: Sorting algorithms v01

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

Page 35: Sorting algorithms v01

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

Page 36: Sorting algorithms v01

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

Page 37: Sorting algorithms v01

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

Page 38: Sorting algorithms v01

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Largest

Selection Sort

Page 39: Sorting algorithms v01

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

Page 40: Sorting algorithms v01

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

Page 41: Sorting algorithms v01

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

Page 42: Sorting algorithms v01

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

Page 43: Sorting algorithms v01

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Largest

Selection Sort

Page 44: Sorting algorithms v01

1 2 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

Page 45: Sorting algorithms v01

1 2 3 4 5 6

Comparison

Data Movement

Sorted

DONE!

Selection Sort

Page 46: Sorting algorithms v01

46

start

i:=1,n-1

min:=a(i)ind:=i

for j:=i+1,n

a(j) < min

min:=a(j)ind:=j

a(ind):=a(i)a(i):=min

end

false

Selection sort

Page 47: Sorting algorithms v01

Code of selection sortpublic static void selectionSort(int[] a) {

int outer, inner, min; for (outer = 0; outer < a.length - 1; outer++)

{ min = outer;

for (inner = outer + 1; inner < a.length; inner++) {

if (a[inner] < a[min]) min = inner;} // Invariant: for all i, if outer <= i <= inner, then a[min] <= a[i]

// a[min] is least among a[outer]..a[a.length - 1] int temp = a[outer]; a[outer] = a[min]; a[min] = temp;// Invariant: for all i <= outer, if i < j then a[i] <= a[j] }

}

Page 48: Sorting algorithms v01

1. We have two group of items:–sorted group, and–unsorted group

2. Initially, all items in the unsorted group and the sorted group is empty. –We assume that items in the unsorted group unsorted. –We have to keep items in the sorted group sorted.

3. Pick any item from, then insert the item at the right position in the sorted group to maintain sorted property.4. Repeat the process until the unsorted group becomes empty.

Insertion sort

This method is often used for playing cards (used by people to sort bridge hands).

Page 49: Sorting algorithms v01

Like sorting a hand of playing cards:–Start with an empty left hand and the cards facing down on the table.–Remove one card at a time from the table, and insert it into the correct position in the left hand

Compare it with each of the cards already in the hand, from right to left.–The cards held in the left hand are sorted

These cards were originally the top cards of the pile on the table.

Insertion sort

Page 50: Sorting algorithms v01

50

6 1024

12

36

Insertion sort

1. To insert 12, we need to make room for it.

Page 51: Sorting algorithms v01

51

6 1024 36

12

Insertion sort

2. Moving 36.

Page 52: Sorting algorithms v01

52

6 10 24 36

12

Insertion sort

3. Moving 24.

Page 53: Sorting algorithms v01

53

6 10 243612

Insertion sort

4. Place 12 into right position.

Page 54: Sorting algorithms v01

for(i=1; i<n; i++)for(j=i; (j>0) && (a[j]>a[j-1]); j--){

pom=a[j];a[j]=a[j-1];a[j-1]=pom;

}

Code for insertion sort

Page 55: Sorting algorithms v01

Insertion sortwith Romanian folk dance

Page 56: Sorting algorithms v01

• Compare each element (execept 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 (execept the last two) 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 two elements is now in the correct and final place.

• Compare each element (execept the last three) with its neighbor to the right.– Continue as above until you have no unsorted elements on the left.

Bubble sort

Page 57: Sorting algorithms v01

9, 6, 2, 12, 11, 9, 3, 7

6, 9, 2, 12, 11, 9, 3, 7

6, 2, 9, 12, 11, 9, 3, 7In the third comparison, the 9 is not larger than the 12 so no exchange is made. We move on to compare the next pair without any change to the list.

In the third comparison, the 9 is not larger than the 12 so no exchange is made. We move on to compare the next pair without any change to the list.

The next pair of numbers are compared. 9 is the larger and this pair is also exchanged.The next pair of numbers are compared. 9 is the larger and this pair is also exchanged.

Compares the numbers in pairs from left to right exchanging when necessary. The first number is compared to the second and as it is larger they are exchanged.

Compares the numbers in pairs from left to right exchanging when necessary. The first number is compared to the second and as it is larger they are exchanged.

Bubble sort

Page 58: Sorting algorithms v01

6, 2, 9, 12, 11, 9, 3, 7

6, 2, 9, 11, 12, 9, 3, 7

6, 2, 9, 11, 9, 12, 3, 7The 12 is greater than the 3 so they are exchanged.The 12 is greater than the 3 so they are exchanged.

The 12 is greater than the 9 so they are exchangedThe 12 is greater than the 9 so they are exchanged

The 12 is larger than the 11 so they are exchanged.The 12 is larger than the 11 so they are exchanged.

Bubble sort

Page 59: Sorting algorithms v01

6, 2, 9, 11, 9, 3, 12, 7

6, 2, 9, 11, 9, 3, 7, 12

The 12 is greater than the 7 so they are exchanged.The 12 is greater than the 7 so they are exchanged.

The end of the list has been reached so this is the end of the first pass. The twelve at the end of the list must be largest number in the list and so is now in the correct position. We now start a new pass from left to right.

The end of the list has been reached so this is the end of the first pass. The twelve at the end of the list must be largest number in the list and so is now in the correct position. We now start a new pass from left to right.

Bubble sort

Page 60: Sorting algorithms v01

6, 2, 9, 11, 9, 3, 7, 122, 6, 9, 11, 9, 3, 7, 122, 6, 9, 9, 11, 3, 7, 122, 6, 9, 9, 3, 11, 7, 122, 6, 9, 9, 3, 7, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

This time we do not have to compare the last two numbers as we know the 12 is in position. This pass therefore only requires 6 comparisons.This time we do not have to compare the last two numbers as we know the 12 is in position. This pass therefore only requires 6 comparisons.

First Pass

Second Pass

Bubble sort

Page 61: Sorting algorithms v01

2, 6, 9, 9, 3, 7, 11, 122, 6, 9, 3, 9, 7, 11, 122, 6, 9, 3, 7, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

2, 6, 9, 9, 3, 7, 11, 12Second Pass

First Pass

Third Pass

This time the 11 and 12 are in position. This pass therefore only requires 5 comparisons.This time the 11 and 12 are in position. This pass therefore only requires 5 comparisons.

Bubble sort

Page 62: Sorting algorithms v01

2, 6, 9, 3, 7, 9, 11, 122, 6, 3, 9, 7, 9, 11, 122, 6, 3, 7, 9, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

2, 6, 9, 9, 3, 7, 11, 12Second Pass

First Pass

Third Pass

Each pass requires fewer comparisons. This time only 4 are needed.Each pass requires fewer comparisons. This time only 4 are needed.

2, 6, 9, 3, 7, 9, 11, 12Fourth Pass

Bubble sort

Page 63: Sorting algorithms v01

2, 6, 3, 7, 9, 9, 11, 122, 3, 6, 7, 9, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

2, 6, 9, 9, 3, 7, 11, 12Second Pass

First Pass

Third Pass

The list is now sorted but the algorithm does not know this until it completes a pass with no exchanges.The list is now sorted but the algorithm does not know this until it completes a pass with no exchanges.

2, 6, 9, 3, 7, 9, 11, 12Fourth Pass2, 6, 3, 7, 9, 9, 11, 12

Fifth Pass

Bubble sort

Page 64: Sorting algorithms v01

2, 3, 6, 7, 9, 9, 11, 12

6, 2, 9, 11, 9, 3, 7, 12

2, 6, 9, 9, 3, 7, 11, 12Second Pass

First Pass

Third Pass2, 6, 9, 3, 7, 9, 11, 12Fourth Pass2, 6, 3, 7, 9, 9, 11, 12

Fifth Pass

Sixth Pass

2, 3, 6, 7, 9, 9, 11, 12This pass no exchanges are made so the algorithm knows the list is sorted. It can therefore save time by not doing the final pass. With other lists this check could save much more work.

This pass no exchanges are made so the algorithm knows the list is sorted. It can therefore save time by not doing the final pass. With other lists this check could save much more work.

Bubble sort

Page 65: Sorting algorithms v01

65

start

i=2,n

j=n,i,-1

a(j-1) > a(j)

temp=a(j-1)a(j-1)=a(j)a(j)=temp

end

false

Bubble sort

Page 66: Sorting algorithms v01

Code for bubble sort

public static void bubbleSort(int[] a) { int outer, inner; for (outer = a.length - 1; outer > 0; outer--)

{ // counting down for (inner = 0; inner < outer; inner++)

{ // bubbling up if (a[inner] > a[inner + 1])

{ // if out of order... int temp = a[inner];

// ...then swap a[inner] = a[inner + 1]; a[inner + 1] = temp; } } }}

Page 67: Sorting algorithms v01

67

Bubble sort with Hungarian ("Csángó") folk dance

Page 68: Sorting algorithms v01

• Divide array into two halves.• Recursively sort each half.• Merge two halves to make sorted whole.

merge

sort

A L G O R I T H M S

divideA 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

Merge sort

Page 69: Sorting algorithms v01

auxiliary array

smallest smallest

A G L O R H I M S T

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

A

Merge sort

Page 70: Sorting algorithms v01

auxiliary array

smallest smallest

A G L O R H I M S T

A G

Merge sort

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

Page 71: Sorting algorithms v01

auxiliary array

smallest smallest

A G L O R H I M S T

A G H

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

Merge sort

Page 72: Sorting algorithms v01

auxiliary array

smallest smallest

A G L O R H I M S T

A G H I

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

Merge sort

Page 73: Sorting algorithms v01

auxiliary array

smallest smallest

A G L O R H I M S T

A G H I L

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

Merge sort

Page 74: Sorting algorithms v01

auxiliary array

smallest smallest

A G L O R H I M S T

A G H I L M

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

Merge sort

Page 75: Sorting algorithms v01

auxiliary array

smallest smallest

A G L O R H I M S T

A G H I L M O

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

Merge sort

Page 76: Sorting algorithms v01

auxiliary array

smallest smallest

A G L O R H I M S T

A G H I L M O R

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

Merge sort

Page 77: Sorting algorithms v01

auxiliary array

first halfexhausted smallest

A G L O R H I M S T

A G H I L M O R S

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

Merge sort

Page 78: Sorting algorithms v01

auxiliary array

first halfexhausted smallest

A G L O R H I M S T

A G H I L M O R S T

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

Merge sort

Page 79: Sorting algorithms v01

Merge sort

Page 80: Sorting algorithms v01

We learnt!

• What is sorting. • Sorting algorithms.• Types of sorting algorithms. • Bubble Sort.• Insertion Sort.• Selection Sort.• Merge Sort.

Page 81: Sorting algorithms v01

Quiz

A

B

C

D

1. One of the simplest sorting algorithms is called bubble sort. Do you know why?

It encases each element in a 'bubble' before sorting them.

It's a mystery. Why is anything called what it is, really?The designer hoped more people would use his sort if it had a cute nameSmaller elements 'bubble' to the top

Teacher Guide - Quiz Question

Teacher Guide - Quiz Solution

Page 82: Sorting algorithms v01

Quiz

A

B

C

D

2.In which case would an insertion sort perform best, assuming it was reading the array to be sorted from beginning to end (as opposed to randomly)?

If the array was sorted in reverse order.

If the array was already sorted.

If the array was in a random order.

These will all perform equally well.

Page 83: Sorting algorithms v01

Sort the following array using each of the four sorting algorithms.

26 48 12 92 28 6 33

Homework

a) Bubble Sortb) Selection Sortc) Insertion Sortd) Merge Sort