Download presentation
Presentation is loading. Please wait.
1
Sorting
2
What is Sorting? An list A of N numbers is sorted in ascending order if the array entries increase (or never decrease) as indices increase:
3
} } Sorting Algorithms There are many strategies for sorting arrays.
Among them: Bubble sort Selection sort Insertion sort Shell sort Merge sort Quicksort } Horrible } Not too bad } Among the very best
4
Notation Let A be an list of length N
Let last be an index in the range of the list: A[0..last] denotes the portion of the array consisting of A[0], A[1], …, A[last]
5
A Simple Sorting Strategy
for (last = N -1; last >= 1; last --) { Move the largest entry in A[0…last] to A[last] }
6
A Simple Sorting Strategy
How it works on Portion of array already sorted in orange. Largest value in unsorted portion of array in bright blue. Move largest of A[0..4] to A[4]: Move largest of A[0..3] to A[3]: Move largest of A[0..2] to A[2]: Move largest of A[0..1] to A[1]: for (last = 4; last >= 1; last--) { Move largest of A[0..last] to A[last] }
7
Selection and Bubble Sort
Selection and Bubble Sort are similar: both use the Simple Sorting Strategy of the previous slide. Selection and Bubble Sort differ in how they implement the step: Move the largest of A[0..last] to A[last]
8
Bubble Sort Bubble Sort moves the largest entry to the end of A[0..last] by comparing and swapping adjacent elements as an index sweeps through the unsorted portion of the array: //Compare A[0], A[1], no swap //Compare A[1], A[2], swap //Compare A[2], A[3], swap //Compare A[3], A[4], swap //Largest is at A[4]
9
Bubble Sort 3 7 5 2 6 1 4
10
BubbleSort 3 7 5 2 6 1 4 ^ ^
11
3 7 5 2 6 1 4 3 7 5 2 6 1 4 ^ ^
12
3 7 5 2 6 1 4 3 7 5 2 6 1 4 3 5 7 2 6 1 4 ^ ^
13
3 7 5 2 6 1 4 3 7 5 2 6 1 4 3 5 7 2 6 1 4 3 5 2 7 6 1 4 ^ ^
14
3 7 5 2 6 1 4 3 7 5 2 6 1 4 3 5 7 2 6 1 4 3 5 2 7 6 1 4 3 5 2 6 7 1 4 ^ ^
15
3 7 5 2 6 1 4 3 7 5 2 6 1 4 3 5 7 2 6 1 4 3 5 2 7 6 1 4 3 5 2 6 7 1 4 3 5 2 6 1 7 4 ^ ^
16
End of Pass 1 3 7 5 2 6 1 4 Note that the last element must be the biggest 3 7 5 2 6 1 4 3 5 7 2 6 1 4 3 5 2 7 6 1 4 3 5 2 6 7 1 4 3 5 2 6 1 7 4 3 5 2 6 1 4 7
17
Bubble Sort: Pass 2 3 5 2 6 1 4 7 ^ ^
18
Bubble Sort: Pass 2 3 5 2 6 1 4 7 3 5 2 6 1 4 7 ^ ^
19
Bubble Sort: Pass 2 3 5 2 6 1 4 7 3 5 2 6 1 4 7 3 2 5 6 1 4 7 ^ ^
20
Bubble Sort: Pass 2 3 5 2 6 1 4 7 3 5 2 6 1 4 7 3 2 5 6 1 4 7 3 2 5 6 1 4 7 ^ ^
21
Bubble Sort: Pass 2 3 5 2 6 1 4 7 3 5 2 6 1 4 7 3 2 5 6 1 4 7 3 2 5 6 1 4 7 3 2 5 1 6 4 7 ^ ^
22
Bubble Sort: Pass 2 3 5 2 6 1 4 7 3 5 2 6 1 4 7 3 2 5 6 1 4 7 3 2 5 6 1 4 7 3 2 5 1 6 4 7 3 2 5 1 4 6 7 Note that the last 2 elements are the largest and in order
23
Bubble Sort: Pass 3 3 2 5 1 4 6 7 ^ ^
24
Bubble Sort: Pass 3 3 2 5 1 4 6 7 2 3 5 1 4 6 7 ^ ^
25
Bubble Sort: Pass 3 3 2 5 1 4 6 7 2 3 5 1 4 6 7 2 3 5 1 4 6 7 ^ ^
26
Bubble Sort: Pass 3 3 2 5 1 4 6 7 2 3 5 1 4 6 7 2 3 5 1 4 6 7 2 3 1 5 4 6 7 ^ ^
27
Bubble Sort: Pass 3 3 2 5 1 4 6 7 2 3 5 1 4 6 7 2 3 5 1 4 6 7 2 3 1 5 4 6 7 2 3 1 4 5 6 7 The last 3 elements are the largest and in order
28
Bubble Sort: Pass 4 2 3 1 4 5 6 7 ^ ^
29
Bubble Sort: Pass 4 2 3 1 4 5 6 7 2 3 1 4 5 6 7 ^ ^
30
Bubble Sort: Pass 4 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 ^ ^
31
Bubble Sort: Pass 4 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 The last 4 elements are the largest and in order
32
Bubble Sort: Pass 5 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 ^ ^
33
Bubble Sort: Pass 5 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 1 2 3 4 5 6 7 ^ ^
34
Bubble Sort: Pass 5 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 1 2 3 4 5 6 7 The last 5 elements are the largest and in order
35
Bubble Sort: Pass 6 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 ^ ^
36
Bubble Sort: Pass 6 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 The last 6 elements are the largest and in order
37
Bubble Sort is Done 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 What is the complexity of this algorithm?
38
Bubble Sort def bubbleSort(alist):
for passnum in range(len(alist)-1,0,-1): for i in range(passnum): if alist[i]>alist[i+1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp
39
Selection Sort Selection sort, like Bubble sort, is based on a strategy that repeatedly executes the step: Move the largest of A[0..last] to A[last]
40
Selection Sort Selection Sort moves the largest entry to the end of A[0..last] by determining the position positionofMax of the largest entry, and then swapping A[positionOfMax] with A[last]: Selection Sort uses 2 variables: fillslot: keeps track of the portion A[0.. fillslot] that has already been examined: fillslot will range from 0 to last. positionOfMax: keeps track of the position of the largest entry in A[0.. fillslot]. When fillslot equals last, the variable positionOfMax will be the position of the largest entry in A[0..last].
41
Selection Sort def selectionSort(alist):
for fillslot in range(len(alist)-1,0,-1): positionOfMax=0 for location in range(1,fillslot+1): if alist[location]>alist[positionOfMax]: positionOfMax = location temp = alist[fillslot] alist[fillslot] = alist[positionOfMax] alist[positionOfMax] = temp
42
Why is selection sort typically faster than bubble sort?
Bubble sort has to do O(n2) comparisons and it may do up to O(n2) swaps. Selection sort has to do O(n2) comparisons but only O(n) swaps.
43
Insertion Sort
44
Insertion Sort Note that for any array A[0..N-1], the portion A[0..0] consisting of the single entry A[0] is already sorted. Insertion Sort works by extending the length of the sorted portion one step at a time: A[0] is sorted A[0..1] is sorted A[0..2] is sorted A[0..3] is sorted, and so on, until A[0..N-1] is sorted.
45
Insertion Sort The strategy for Insertion Sort: //A[0..0] is sorted
for (index = 1; index <= N -1; index ++) { // A[0..index-1] is sorted insert A[index] at the right place in A[0..index] // Now A[0..index] is sorted } // Now A[0..N -1] is sorted, so entire array is sorted
46
Insertion Sort 3 4 5 1 2 5 2 4 6 1 3
47
Insertion Sort 3 4 5 1 2 5 4 6 1 3 2
48
Insertion Sort 3 4 5 1 2 5 4 6 1 3 2
49
Insertion Sort 3 4 5 1 2 2 5 4 6 1 3
50
Insertion Sort 3 4 5 1 2 2 5 6 1 3 4
51
Insertion Sort 3 4 5 1 2 2 5 6 1 3 4
52
Insertion Sort 3 4 5 1 2 2 4 5 6 1 3
53
Insertion Sort 3 4 5 1 2 2 4 5 1 3 6
54
Insertion Sort 3 4 5 1 2 2 4 5 6 1 3
55
Insertion Sort 3 4 5 1 2 2 4 5 6 3 1
56
Insertion Sort 3 4 5 1 2 2 4 5 6 3 1
57
Insertion Sort 3 4 5 1 2 2 4 5 6 3 1
58
Insertion Sort 3 4 5 1 2 2 4 5 6 3 1
59
Insertion Sort 3 4 5 1 2 2 4 5 6 3 1
60
Insertion Sort 3 4 5 1 2 1 2 4 5 6 3
61
Insertion Sort 3 4 5 1 2 1 2 4 5 6 3
62
Insertion Sort 3 4 5 1 2 1 2 4 5 6 3
63
Insertion Sort 3 4 5 1 2 1 2 4 5 6 3
64
Insertion Sort 3 4 5 1 2 1 2 4 5 6 3
65
Insertion Sort 3 4 5 1 2 1 2 3 4 5 6
66
Insertion Sort: def insertionSort(alist):
for index in range(1,len(alist)): currentvalue = alist[index] position = index while position>0 and alist[position-1]>currentvalue: alist[position]=alist[position-1] position = position-1 alist[position]=currentvalue
67
Why Insertion Sort tends to be better than either Bubble sort or Selection
Bubble sort MUST do O(n2) comparisons and may do O(n2) swaps. Selection sort MUST do O(n2) comparisons. Insertion sort MAY do O(n2) comparisons in the worst case.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.