Download presentation
Presentation is loading. Please wait.
Published byMichael Paul Modified over 9 years ago
1
Sorts Tonga Institute of Higher Education
2
Introduction - 1 Sorting – The act of ordering data Often, we need to order data. Example: Order a list of names Different sorting techniques work best in different situations Sorting is very important to making efficient programs so a lot of research is done in this area.
3
Introduction - 2 Sorting Algorithms Bubble Sort Selection Sort Insertion Sort All these sorting routines do 2 steps repeatedly 1. Compare 2 items 2. Swap 2 items or copy 1 item
4
Bubble Sort Very Slow Simplest way to sort Rules 1. Start from left side 2. Compare 2 items 3. If the item on the left is bigger, switch them 4. Move cursor one position right 5. Repeat from step 2 until cursor is at the end of the list. At this point, you have put the largest item at the end of the list. This item will no longer be included in this algorithm. 6. Repeat from step 1 until items are sorted
5
Demonstration Bubble Sort Applet
6
Demonstration Bubble Sort Code Project: BubbleSort
7
Bubble Sort Efficiency Number of Comparisons First Pass Number of items - 1 = N - 1 Second Pass Number of items - 2 = N – 2 So total is: (N -1) + (N – 2) + … + 1 = N*(N-1) / 2 = (N 2 – N) / 2 Number of Swaps If the data is random, swaps occur ½ the time when a comparison is made. ((N 2 – N) / 2) / 2 = (N 2 – N) / 4 Both comparisons and swaps have an N 2 We ignore the constant numbers Therefore, the bubble sort compares and swaps in O(N 2 ) time.
8
Selection Sort Has less number of swaps than bubble sorts Rules 1. Check every item to find smallest value 2. Swap the item with the smallest value with the leftmost item. The leftmost item will no longer be included in this algorithm. 3. Repeat from step 1 until items are sorted
9
Demonstration Selection Sort Applet
10
Demonstration Selection Sort Code Project: SelectionSort
11
Selection Sort Efficiency - 1 Number of Comparisons First Pass Number of items - 1 = N - 1 Second Pass Number of items - 2 = N – 2 So total is: (N -1) + (N – 2) + (N – 3) + … + 1 = N*(N-1)/2 Number of Swaps Less than N swaps
12
Selection Sort Efficiency - 2 Number of Comparisons Total is: = N*(N-1)/2 Number of Swaps Total is: Less than N Comparisons have an N 2 We ignore the constant numbers Therefore, the selection sort compares in O(N 2 ) time Swaps have an N Therefore, the selection sort swaps in O(N) time For small N values: Selection sorts run faster than bubble sorts because there are less swaps. For large N values: Selection sorts run faster than bubble sorts because there are less swaps. However, the performance will be close to O(N 2 ) than O(N) because the comparison times will dominate the swap times.
13
Insertion Sort - 1 Partial Sorting – Keeping a sorted list of items in a unsorted list Marked Item – The leftmost item that has not been sorted
14
Insertion Sort - 2 Rules 1. Take out marked item 2. Compare marked item with item on left 3. If the item on left is bigger, then move bigger item 1 place to the right 4. Repeat from step 2 until the item on the left is smaller. Then, put the marked item in the blank space 5. Set marked item 1 place to the right. Repeat from step 1
15
Demonstration Insertion Sort Applet
16
Demonstration Insertion Sort Code Project: InsertionSort
17
Insertion Sort Efficiency - 1 Number of Comparisons First Pass 1 Second Pass 2 Last Pass N - 1 So total is: 1 + 2 + … + N - 1 = N*(N-1)/2 However, on average, only half of the items are compared before the insertion point is found. So the total: = N*(N-1)/4 Number of Copies Almost the same as the number of comparisons
18
Insertion Sort Efficiency - 2 Number of Comparisons Total: = N*(N-1)/4 Number of Copies Total: = N*(N-1)/4 Comparisons have an N 2 Therefore, the insertion sort compares in O(N 2 ) time However, does half as many comparisons as bubble sorts and selection sorts Copies have an N 2 Therefore, the insertion sort copies in O(N 2 ) time A copy is much faster than a swap This is twice as fast as a bubble sort for random data This is slightly faster than a selection sort for random data This is much faster when data is almost sorted This is the same as a bubble sort for inverse sorted order because every possible comparison must be carried out
19
Comparing the 3 Sorts Bubble Sorts Easiest Slowest Selection Sorts Same number of comparisons as bubble sorts Much less number of swaps than bubble sorts Works well when data is small and swapping takes much longer than comparisons Insertion Sorts Best sort we’ve learned about ½ the number of comparisons as bubble sorts and selection sorts Copies instead of swaps, which is much faster Works well when data is small and data is almost sorted
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.