COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June...

18
COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011

Transcript of COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June...

Page 1: COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.

COMP 1001: Introduction to Computers for Arts and Social Sciences

Sorting Algorithms

Wednesday, June 1, 2011

Page 2: COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.

COMP 1001: Introduction to Computers for Arts and Social Sciences

How do you sort when you

can only compare two

things?

Page 3: COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.

COMP 1001: Introduction to Computers for Arts and Social Sciences

Sorting Demo

http://gailcarmichael.com/processing/bottlesort/

Page 4: COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.

COMP 1001: Introduction to Computers for Arts and Social Sciences

Sort

• Part One:–Find the lightest bottle using the scale–What is the easiest way to do this?

Page 5: COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.

COMP 1001: Introduction to Computers for Arts and Social Sciences

Sort

• Part Two:–Choose three bottles at random–Sort them using the scale–What is the fewest number of

comparisons you can do?

Page 6: COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.

COMP 1001: Introduction to Computers for Arts and Social Sciences

Sort

• Part Three:–Sort all the bottles from lightest to

heaviest by repeatedly finding the lightest bottle that’s left, and moving it into the sorted list at the top–This is known as Selection Sort

Page 7: COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.

COMP 1001: Introduction to Computers for Arts and Social Sciences

Selection Sort Dance

http://www.youtube.com/watch?v=Ns4TPTC8whw

Page 8: COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.

COMP 1001: Introduction to Computers for Arts and Social Sciences

Selection Sort Demo

http://www.sorting-algorithms.com/selection-sort

Page 9: COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.

COMP 1001: Introduction to Computers for Arts and Social Sciences

Selection Sort Demo (II)

http://www.cosc.canterbury.ac.nz/mukundan/dsal/SSort.html

Page 10: COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.

COMP 1001: Introduction to Computers for Arts and Social Sciences

Insertion Sort

• Take the next item in the unsorted list• Insert it into the correct location in the sorted

list– Do this by comparing the new item with each of

the existing items until the correct place is found

Page 11: COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.

COMP 1001: Introduction to Computers for Arts and Social Sciences

Insertion Sort Dance

http://www.youtube.com/watch?v=ROalU379l3U

Page 12: COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.

COMP 1001: Introduction to Computers for Arts and Social Sciences

Insertion Sort Demo

http://www.sorting-algorithms.com/insertion-sort

Page 13: COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.

COMP 1001: Introduction to Computers for Arts and Social Sciences

Quick Sort

Page 14: COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.

COMP 1001: Introduction to Computers for Arts and Social Sciences

Quick Sort Demo

http://www.sorting-algorithms.com/quick-sort

Page 15: COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.

COMP 1001: Introduction to Computers for Arts and Social Sciences

Quicksort Demo (II)

http://www.cosc.canterbury.ac.nz/mukundan/dsal/QSort.html

Page 16: COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.

COMP 1001: Introduction to Computers for Arts and Social Sciences

Quicksorting a Stack of Graded Papers

http://www.youtube.com/watch?v=FSyr8o8jjwM

Page 17: COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.

COMP 1001: Introduction to Computers for Arts and Social Sciences

Efficiency

• Which sorting algorithm should be fastest in most cases?

• What would happen to quicksort if you picked the lightest bottle to

arrange around each time?

Page 18: COMP 1001: Introduction to Computers for Arts and Social Sciences Sorting Algorithms Wednesday, June 1, 2011.

COMP 1001: Introduction to Computers for Arts and Social Sciences

Selection Sort vs. Quicksort

http://www.youtube.com/watch?v=cVMKXKoGu_Y