Week 03 SimpleSorting
Week 03 SimpleSorting
Introductory Remarks
Bubble Sort
Selection Sort
Insertion Sort
Sorting Objects
Comparing Sorts
Introduction
4
89 58 29 40 12 42 10 1
1 32 12 53 11 76 23 89
13
BubbleSort procedure
Homework
14
Result:
partially-ordered list is one item larger and
the unsorted list is one item smaller.
Marked item moves one slot to the right, so once more it is
again in front of the leftmost unsorted item.
24 19 42 12 41 29 50
Efficiency of the Insertion Sort
35
Comparisons:
On pass one, max of one;
pass two, max of two, etc.
Up to a max of n-1 comparisons.
1+2+3+…+n-1 = n*(n-1)/2 comparisons.
But, on average
n*(n-1)/4.
Efficiency of the Insertion Sort (2 of 3)
36
Copy:
Have lots of ‘copies’
(same as number of comparisons – about)
Remember:
Doing Experiments
Doing Programming Projects
Sorting algorithms
46