sorting introduction to systems programming - comp 1005, 1405 instructor : behnam hajian...

44
SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian [email protected]

Upload: blaze-richard

Post on 13-Jan-2016

223 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca

SORTING

Introduction to Systems Programming - COMP 1005, 1405

Instructor : Behnam Hajian

[email protected]

Page 2: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 3: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 4: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 5: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 6: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 7: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca

Sorting algorithms

Page 8: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca

Sorting algorithms cont.

Page 9: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca

Bubble Sort

Page 10: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 11: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 12: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 13: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 14: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 15: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 16: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 17: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 18: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 19: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 20: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 21: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 22: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 23: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 24: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca

Notice how the left side of the list always contains items in sorted order, although additional items still need to be inserted in there, so that sorted list is not complete until the last pass.

Notice in all except the 3rd pass that there was a need to search backwards through the sorted portion in order to find the correct place to insert the key item.

Page 25: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 26: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca

Bucket Sort & Counting Sort

Page 27: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 28: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 29: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 30: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 31: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 32: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 33: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 34: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 35: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 36: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 37: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca

In summary, with a large number of items and enough bin space (i.e., storage space), then a Counting Sort is the best that we can hope for since it minimizes the number of steps needed to be made in order to sort. However, remember that it only works well if there are a lot of items that are equal. The more general Bucket Sort is used when the bin size is greater than one and this can also be very efficient when there are many equal items.

Page 38: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 39: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 40: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 41: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 42: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 43: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca
Page 44: SORTING Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian bhajian@scs.carleton.ca