0% found this document useful (0 votes)
52 views42 pages

The Art of Data Structures: Sorting

The document discusses different sorting algorithms including bubble sort, selection sort, insertion sort, and shell sort. It provides pseudocode implementations and examples of how each algorithm sorts a sample list of numbers. Shell sort is explained as sorting with increments larger than one, which moves items closer to their final positions before a standard insertion sort with an increment of one.

Uploaded by

ritesh sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views42 pages

The Art of Data Structures: Sorting

The document discusses different sorting algorithms including bubble sort, selection sort, insertion sort, and shell sort. It provides pseudocode implementations and examples of how each algorithm sorts a sample list of numbers. Shell sort is explained as sorting with increments larger than one, which moves items closer to their final positions before a standard insertion sort with an increment of one.

Uploaded by

ritesh sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

The Art of Data Structures

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

• This is the process of organizing data in some


particular order

• Numbers, increasing order

• Words, alphabetically

• etc

• Some algorithms benefit with pre-sorted data, e.g.


binary search
Sorting

• Sorting is an important area of computer science

• Many sorting algorithms have been developed, and


analyzed

• Sorting can take significant time, and is related to


the number of items to process
Sorting

• Sorting requires two main operations:

• Comparisons of items to see if they are out of


order; comparisons will be an important metric

• Exchange of items can be a costly operation,


and also an important metric
Bubble Sort
Sorting
The Merge Sort
The Quick Sort

bbleSort: The Bubble Sort


First Pass
bubble_sort: The First Pass
First pass

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

changing Two Values in Python


Exchanging Two Values in Python
Most programming languages require a 3-step
process with an extra storage location.

2nd
i j

93 44

1st 3rd

temp

93 44

In Python, exchange can be done as


two simultaneous assignments.
Bubble Sort
Implementation

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

while position > 0 and alist[position-1] > currentvalue:


alist[position] = alist[position-1]
position = position-1

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

Figure 5.18: A Shell Sort with Increments of Three


Shell Sort
216 After Sorting
Chapter Each
5. Searching Sublist
and Sorting

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

after sorting sublists


17 26 20 44 55 31 54 77 93
at increment 3
after sorting sublists
17 26 20 44 55 31 54 77 93
at increment 3

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

Figure 5.21: Initial Sublists for a Shell Sort


Shell Sort
Implementation

def shell_sort(alist):
sublistcount = len(alist)//2
while sublistcount > 0:
for startposition in range(sublistcount):
gap_insertion_sort(alist, startposition, sublistcount)

print("After increments of size", sublistcount,


"The list is", alist)

sublistcount = sublistcount // 2
Shell Sort
Implementation

def gap_insertion_sort(alist, start, gap):


for i in range(start+gap, len(alist), gap):
currentvalue = alist[i]
position = i

while position >= gap and \


alist[position-gap] > currentvalue:
alist[position] = alist[position-gap]
position = position-gap

alist[position] = currentvalue
Merge Sort
Searching
The Shell Sort
Sorting
The Merge Sort

Merge Sort The Quick Sort

litting and Merging in a Merge Sort


Splitting and Merging

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

while i < len(lefthalf):


alist[k] = lefthalf[i]
i = i+1
k = k+1

while j < len(righthalf):


alist[k] = righthalf[j]
j = j+1
k = k+1
print("Merging ", alist)
Quick Sort
The Bubble Sort
The Selection Sort
What Is Algorithm Analysis?

Quick Sort
The Insertion Sort
Searching
The Shell Sort
Sorting
The Merge Sort
The Quick Sort

The First Pivot Value


The First Pivot Value for a Quick Sort

54 will be the
54 26 93 17 77 31 44 55 20
first pivot value

Algorithm Analysis
The Quick Sort

ng the Split Point for 54


Quick Sort
Finding the Split Point for 54
leftmark and rightmark
54 26 93 17 77 31 44 55 20
will converge on split point

leftmark rightmark

26<54 move to right


54 26 93 17 77 31 44 55 20
93>54 stop

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

now continue moving leftmark and rightmark


77>54 stop
54 26 20 17 77 31 44 55 93 44<54 stop
exchange 77 and 44

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

until they cross


The Selection Sort
What Is Algorithm Analysis?

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

quicksort left half quicksort right half

Algorithm Analysis
Quick Sort
Implementation

def quick_sort(alist):
quick_sort_helper(alist, 0, len(alist)-1)

def quick_sort_helper(alist, first, last):


if first<last:

splitpoint = partition(alist,first,last)

quick_sort_helper(alist, first, splitpoint-1)


quick_sort_helper(alist, splitpoint+1, last)
Quick Sort
Implementation (cont.)
def partition(alist, first, last):
pivotvalue = alist[first]

leftmark = first+1
rightmark = last

done = False
while not done:
while leftmark <= rightmark and \
alist[leftmark] <= pivotvalue:
leftmark = leftmark + 1

while alist[rightmark] >= pivotvalue and \


rightmark >= leftmark:
rightmark = rightmark -1

# Continued on next slide...


Quick Sort
Implementation (cont.)

if rightmark < leftmark:


done = True
else:
temp = alist[leftmark]
alist[leftmark] = alist[rightmark]
alist[rightmark] = temp

temp = alist[first]
alist[first] = alist[rightmark]
alist[rightmark] = temp

return rightmark
Analysis
Analysis

• A bubble sort, a selection sort, and an insertion sort


are O(n2) algorithms

• A shell sort improves on the insertion sort by


sorting incremental sub-lists

• It falls between O(n) and O(n2)

• A merge sort is O(n log n), but requires additional


space for the merging process
Analysis

• A quick sort is O(n log n), but may degrade to O(n2)


if the split points are not near the middle of the list

• It does not require additional space


Questions?

You might also like