Simple Sorting
Simple Sorting
• Introduction
• Bubble Sort
• Selection Sort
• Insertion Sort
• Comparison
Sorting
Introduction
Eventually
Bubble Sort
• ..\ReaderPrograms\ReaderFiles\Chap03\
BubbleSort\bubbleSort.java
Example
• 572693
• 526793
• 526739
• ================= End of pass 1
• 2 5 6 7 3| 9
• 2 5 6 3 7| 9
• ================= End of pass 2
• 2 5 3 6| 7 9
• ================= End of pass 3
• 2 3 5| 6 7 9
• ================= End of pass 4
• 527693
• 526793
• 526739
• ================= End of pass 1
• 2 5 6 7 3| 9
• 2 5 6 3 7| 9
• ================= End of pass 2
• 2 5 3 6| 7 9
• ================= End of pass 3
• 2 3 5| 6 7 9
• ================= End of pass 4
Bubble Sort: Analysis
• ..\ReaderPrograms\ReaderFiles\Chap03\
SelectSort\selectSort.java
Selection Sort
• Efficiency:
-- Number of comparisons is same,
N*(N-1)/2.
-- runs in O(N2) time.
• How do you compare it to Bubble sort?
– Faster than bubble sort because of fewer
swaps.
References
• http://www.cs.yorku.ca/~utn/c2011/
bubble_sort.pdf
Insertion Sort
Marked
player
Insertion Sort
• ..\ReaderPrograms\ReaderFiles\Chap03\
InsertSort\insertSort.java
Insertion Sort Analysis