The Art of Data Structures: Sorting
The Art of Data Structures: Sorting
Sorting
Richard E Sarkis
CSC 162: The Art of Data Structures
Class Administrivia
Agenda
• To be able to explain and implement various
sorting algorithms
• Bubble
• Selection
• Insertion
• Shell
• Merge
• Quick
Sorting
Sorting
• Words, alphabetically
• etc
54 26 93 17 77 31 44 55 20 Exchange
26 54 93 17 77 31 44 55 20 No Exchange
26 54 93 17 77 31 44 55 20 Exchange
26 54 17 93 77 31 44 55 20 Exchange
26 54 17 77 93 31 44 55 20 Exchange
26 54 17 77 31 93 44 55 20 Exchange
26 54 17 77 31 44 93 55 20 Exchange
26 54 17 77 31 44 55 93 20 Exchange
26 54 17 77 31 44 55 20 93 93 in place
after first pass
Searching
The Shell Sort
Sorting
The Merge Sort
Bubble Sort
The Quick Sort
2nd
i j
93 44
1st 3rd
temp
93 44
def bubble_sort(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
Bubble Sort
Modified Implementation (cont.)
def bubble_sorted(alist):
exchanges = True
passnum = len(alist)-1
while passnum > 0 and exchanges:
exchanges = False
for i in range(passnum):
if alist[i] > alist[i+1]:
exchanges = True
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
passnum = passnum-1
Selection Sort
The Quick Sort
ectionSort
Selection Sort
54 26 93 17 77 31 44 55 20 93 is largest
26 54 20 17 77 31 44 55 93 77 is largest
26 54 20 17 55 31 44 77 93 55 is largest
26 54 20 17 44 31 55 77 93 54 is largest
26 31 20 17 44 54 55 77 93 44 is largest
stays in place
26 31 20 17 44 54 55 77 93 31 is largest
26 17 20 31 44 54 55 77 93 26 is largest
20 17 26 31 44 54 55 77 93 20 is largest
17 20 26 31 44 54 55 77 93 17 ok
list is sorted
Selection Sort
Implementation
def selection_sort(alist):
for fillslot in range(len(alist)-1, 0, -1):
position_of_max = 0
for location in range(1, fillslot+1):
if alist[location] > alist[position_of_max]:
position_of_max = location
temp = alist[fillslot]
alist[fillslot] = alist[position_of_max]
alist[position_of_max] = temp
Insertion Sort
rtionSort
Insertion Sort
Assume 54 is a sorted
54 26 93 17 77 31 44 55 20
list of 1 item
26 54 93 17 77 31 44 55 20 inserted 26
26 54 93 17 77 31 44 55 20 inserted 93
17 26 54 93 77 31 44 55 20 inserted 17
17 26 54 77 93 31 44 55 20 inserted 77
17 26 31 54 77 93 44 55 20 inserted 31
17 26 31 44 54 77 93 55 20 inserted 44
17 26 31 44 54 55 77 93 20 inserted 55
17 20 26 31 44 54 55 77 93 inserted 20
The Shell Sort
Sorting
The Merge Sort
The Quick Sort
nsertionSort:Insertion
Fifth Pass Sort
of the Sort
insertion_sort: The Fifth Pass
Need to insert 31
17 26 54 77 93 31 44 55 20 back into the sorted list
93>31 so shift it
17 26 54 77 93 44 55 20
to the right
77>31 so shift it
17 26 54 77 93 44 55 20
to the right
54>31 so shift it
17 26 54 77 93 44 55 20
to the right
26<31 so insert 31
17 26 31 54 77 93 44 55 20
in this position
Insertion Sort
Implementation (cont.)
def insertion_sort(alist):
for index in range(1, len(alist)):
currentvalue = alist[index]
position = index
alist[position] = currentvalue
Shell Sort
This can be seen in Figure 5.18. This list has nine items. If we use
an increment of three, there are three sublists, each of which can be sorted
Shell Sort
by an insertion sort. After completing these sorts, we get the list shown
in Figure 5.19. Although this list is not completely sorted, something very
With Increments of Three
interesting has happened. By sorting the sublists, we have moved the items
closer to where they actually belong.
54 26 93 17 77 31 44 55 20 sublist 1
54 26 93 17 77 31 44 55 20 sublist 2
54 26 93 17 77 31 44 55 20 sublist 3
17 26 93 44 77 31 54 55 20 sublist 1 sorted
54 26 93 17 55 31 44 77 20 sublist 2 sorted
54 26 20 17 77 31 44 55 93 sublist 3 sorted
Shell Sort
A Final Insertion Sort with Increment of 1
Figure 5.19: A Shell Sort after Sorting Each Sublist
17 26 20 44 55 31 54 77 93 1 shift for 20
17 20 26 44 55 31 54 77 93 2 shifts for 31
17 20 26 31 44 55 54 77 93 1 shift for 54
17 20 26 31 44 54 55 77 93 sorted
Shell Sort
Initial Sublists for
5.3. Sorting
a Shell Sort 217
54 26 93 17 77 31 44 55 20 sublist 1
54 26 93 17 77 31 44 55 20 sublist 2
54 26 93 17 77 31 44 55 20 sublist 3
54 26 93 17 77 31 44 55 20 sublist 4
def shell_sort(alist):
sublistcount = len(alist)//2
while sublistcount > 0:
for startposition in range(sublistcount):
gap_insertion_sort(alist, startposition, sublistcount)
sublistcount = sublistcount // 2
Shell Sort
Implementation
alist[position] = currentvalue
Merge Sort
Searching
The Shell Sort
Sorting
The Merge Sort
54 26 93 17 77 31 44 55 20
54 26 93 17 77 31 44 55 20
54 26 93 17 77 31 44 55 20
54 26 93 17 77 31 44 55 20
55 20
The Quick Sort
ng and MergingMerge
in a Merge
Sort Sort
Splitting and Merging
54 26 93 17 77 31 44 55 20
26 54 17 93 31 77 20 55
17 26 54 93 20 44 55
20 31 44 55 77
17 20 26 31 44 54 55 77 93
Merge Sort
Implementation
def merge_sort(alist):
print("Splitting ", alist)
if len(alist) > 1:
mid = len(alist)//2
lefthalf = alist[:mid]
righthalf = alist[mid:]
merge_sort(lefthalf)
merge_sort(righthalf)
i = 0
j = 0
k = 0
Merge Sort
Implementation (cont.)
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
alist[k] = lefthalf[i]
i = i+1
else:
alist[k] = righthalf[j]
j = j+1
k = k+1
Quick Sort
The Insertion Sort
Searching
The Shell Sort
Sorting
The Merge Sort
The Quick Sort
54 will be the
54 26 93 17 77 31 44 55 20
first pivot value
Algorithm Analysis
The Quick Sort
leftmark rightmark
leftmark rightmark
now rightmark
54 26 93 17 77 31 44 55 20
20<54 stop
leftmark rightmark
54 26 20 17 77 31 44 55 93 exchange 20 and 93
leftmark rightmark
leftmark rightmark
77>54 stop
54 26 20 17 44 31 77 55 93 31<54 stop
rightmark<leftmark
split point found
exchange 54 and 31
rightmark leftmark
Quick Sort
The Insertion Sort
Searching
The Shell Sort
Sorting
The Merge Sort
The Quick Sort
Completing
Completing the Partition
the Partition Process
Process to to Split
Find the Find
Point for 54 the Split Point for 54
31 26 20 17 44 54 77 55 93 54 is in place
<54 >54
31 26 20 17 44 77 55 93
Algorithm Analysis
Quick Sort
Implementation
def quick_sort(alist):
quick_sort_helper(alist, 0, len(alist)-1)
splitpoint = partition(alist,first,last)
leftmark = first+1
rightmark = last
done = False
while not done:
while leftmark <= rightmark and \
alist[leftmark] <= pivotvalue:
leftmark = leftmark + 1
temp = alist[first]
alist[first] = alist[rightmark]
alist[rightmark] = temp
return rightmark
Analysis
Analysis