Lec5 - 2023
Lec5 - 2023
and Computability
Sorting Algorithms
The figure in the next slide shows the first pass of a bubble sort. The
shaded items are being compared to see if they are out of order. If
there are n items in the list, then there are n−1 pairs of items that need
to be compared on the first pass. It is important to note that once the
largest value in the list is part of a pair, it will continually be moved
along until the pass is complete.
Visualize the Bubble Sort:
https://visualgo.net/en/sorting
Visualize the Bubble Sort:
https://visualgo.net/en/sorting
The selection sort improves on the bubble sort by making only one
exchange for every pass through the list. In order to do this, a
selection sort looks for the largest value as it makes a pass and, after
completing the pass, places it in the proper location. As with a bubble
sort, after the first pass, the largest item is in the correct place. After
the second pass, the next largest is in place. This process continues
and requires n−1passes to sort n items, since the final item must be in
place after the (n−1)st pass.
Visualize the Selection Sort:
https://visualgo.net/en/sorting
temp = alist[fillslot]
alist[fillslot] = alist[positionOfMax]
alist[positionOfMax] = temp
alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)
Press here to see
the run
Step by step
Selection Sort -- Analysis
We begin by assuming
that a list with one item
(position 0) is already
sorted. On each pass, one
for each item 1
through n−1, the current
item is checked against
those in the already sorted
sublist. As we look back
into the already sorted
sublist, we shift those
items that are greater to
the right. When we reach
a smaller item or the end
of the sublist, the current
item can be inserted.
The figure shows the fifth
pass in detail. At this point in
the algorithm, a sorted
sublist of five items
consisting of 17, 26, 54, 77,
and 93 exists. We want to
insert 31 back into the
already sorted items. The
first comparison against 93
causes 93 to be shifted to
the right. 77 and 54 are also
shifted. When the item 26 is
encountered, the shifting
process stops and 31 is
placed in the open position.
Now we have a sorted
sublist of six items.
Insertion Sort Algorithm
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
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)
Press here to see the
run
Step by step
Insertion Sort – Analysis
• Running time depends on not only the size of the array but
also the contents of the array.
• Best-case: ➔ O(n)
• Array is already sorted in ascending order.
• Inner loop will not be executed.
• The number of moves: 2*(n-1) ➔ O(n)
• The number of key comparisons: (n-1) ➔ O(n)
• Worst-case: ➔ O(n2)
• Array is in reverse order:
• Inner loop is executed i-1 times, for i = 2,3, …, n
• The number of moves: 2*(n-1)+(1+2+...+n-1)= 2*(n-1)+ n*(n-1)/2 ➔ O(n2)
• The number of key comparisons: (1+2+...+n-1)= n*(n-1)/2 ➔ O(n2)
• Average-case: ➔ O(n2)
• We have to look at all possible initial data organizations.
• So, Insertion Sort is O(n2)
Analysis of sorting algorithms:
Best, worst or average?
Recall that the slicing operator is O(k) where k is the size of the slice. In order to
guarantee that mergeSort will be O(nlogn) we will need to remove the slice
operator. Again, this is possible if we simply pass the starting and ending indices
along with the list when we make the recursive call. We leave this as an exercise.
It is important to notice that the mergeSort function requires extra space to hold the
two halves as they are extracted with the slicing operations. This additional space
can be a critical factor if the list is large and can make this sort problematic when
working on large data sets.
Quicksort
The quick sort uses divide and conquer to gain the same
advantages as the merge sort, while not using additional storage. As
a trade-off, however, it is possible that the list may not be divided in
half. When this happens, we will see that performance is diminished.
A quick sort first selects a value, which is called the pivot value.
Although there are many different ways to choose the pivot value, we
will simply use the first item in the list. The role of the pivot value is to
assist with splitting the list. The actual position where the pivot value
belongs in the final sorted list, commonly called the split point, will
be used to divide the list for subsequent calls to the quick sort.
Quicksort
Visualize the Bubble Sort:
(How it works) https://visualgo.net/en/sorting
The following figure shows that 54 will serve as our first pivot value. Since we
have looked at this example a few times already, we know that 54 will
eventually end up in the position currently holding 31. The partition process
will happen next. It will find the split point and at the same time move other
items to the appropriate side of the list, either less than or greater than the
pivot value.
Quicksort
(How it works)
Partitioning begins by locating
two position markers—let’s call
them leftmark and rightmark—at
the beginning and end of the
remaining items in the list
(positions 1 and 8 in the
following figure).