0% found this document useful (0 votes)
13 views211 pages

Cs 1311 Lecture 16 WDL

The document provides an overview of sorting algorithms, explaining the importance of sorting data and introducing various algorithms such as Selection Sort, Insertion Sort, and Bubble Sort. It discusses their methodologies, complexities, and practical applications, highlighting the efficiency of different approaches. Additionally, it touches on recursive sorting methods and the Mergesort algorithm, emphasizing the divide-and-conquer strategy.

Uploaded by

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

Cs 1311 Lecture 16 WDL

The document provides an overview of sorting algorithms, explaining the importance of sorting data and introducing various algorithms such as Selection Sort, Insertion Sort, and Bubble Sort. It discusses their methodologies, complexities, and practical applications, highlighting the efficiency of different approaches. Additionally, it touches on recursive sorting methods and the Mergesort algorithm, emphasizing the divide-and-conquer strategy.

Uploaded by

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

Sorting Algorithms

Sorting
• Sorting is a process that organizes a collection of
data into either ascending or descending order.
• Formally
• Input: A sequence of n numbers <a1,a2,…,an>
• Output: A reordering <a’1,a’2,…,a’n> of the sequence
such that a’1 ≤ a’2 ≤ … ≤ a’n
• Given the input <6, 3, 1, 7>, the algorithm should produce
<1, 3, 6, 7>
•Called an instance of the problem
Sorting
• Sorting is a process that organizes a collection of
data into either ascending or descending order.
• We encounter sorting almost everywhere:
– Sorting prices from lowest to highest
– Sorting flights from earliest to latest
– Sorting grades from highest to lowest
– Sorting songs based on artist, album,
playlist, etc.
Sorting Algorithms
• There are many sorting algorithms (as of
27.10.2014 there are 44 Wikipedia entries)
• In this class we will learn:
– Selection Sort
– Insertion Sort
– Bubble Sort
– Merge Sort
– Quick Sort
–Bogo sort and Sleep sort are some bad/slow
algorithms
• These are among the most fundamental
Sorting Algorithms
• As we learnt in the Analysis lecture (time
complexity), a stupid approach uses up
computing power faster than you might
think.
• Sorting a million numbers:

• Interactive graphics: Algorithms must


terminate in 1/30 of a sec.
Sorting Algorithms
• As we learnt in the Analysis lecture (time
complexity), a stupid approach uses up
computing power faster than you might
think.
• Sorting a million numbers:

• https://youtu.be/kPRA0W1kECg
Selection Sort
• Partition the input list into a sorted and
unsorted part (initially sorted part is empty)
• Select the smallest element and put it to the end
of the sorted part
• Increase the size of the sorted part by one
• Repeat this n-1 times to sort a list of n elements
Sorted Unsorted

23 78 45 8 32 56 Original List

After pass 1
8 78 45 23 32 56

After pass 2
8 23 45 78 32 56
After pass 3
8 23 32 78 45 56
After pass 4

8 23 32 45 78 56
After pass 5

8 23 32 45 56 78
Selection Sort (cont.)
template <class Item>
void selectionSort(Item a[], int n) {
for (int i = 0; i < n-1; i++) {
int min = i;
for (int j = i+1; j < n; j++)
if (a[j] < a[min]) min = j;
swap(a[i], a[min]);
}
}

template < class Object>


void swap( Object &lhs, Object &rhs )
{
Object tmp = lhs;
lhs = rhs;
rhs = tmp;
}
Selection Sort -- Analysis
• What is the complexity of selection sort?
• Does it have different best, average, and worst case
complexities?
Selection Sort – Analysis (cont.)
• Selection sort is O(n2) for all three cases (prove this)
• Therefore it is not very efficient
Insertion Sort
• Insertion sort is a simple sorting algorithm that
is appropriate for small inputs.
– Most common sorting technique used by card
players.
• Again, the list is divided into two parts: sorted
and unsorted.
• In each pass, the first element of the unsorted
part is picked up, transferred to the sorted
sublist, and inserted at the appropriate place.
• A list of n elements will take at most n-1 passes
to sort the data.
Insertion Sort
Sorted Unsorted

23 78 45 8 32 56 Original List

After pass 1
23 78 45 8 32 56

After pass 2
23 45 78 8 32 56
After pass 3
8 23 45 78 32 56
After pass 4

8 23 32 45 78 56
After pass 5

8 23 32 45 56 78
Insertion Sort Algorithm
template <class Item>
void insertionSort(Item a[], int n)
{
for (int i = 1; i < n; i++)
{
Item tmp = a[i]; // the element to be inserted
int j;
for (j=i; j>0 && tmp < a[j-1]; j--)
a[j] = a[j-1]; // shift elements
a[j] = tmp; // insert
}
}
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.

• Worst-case:  O(n2)
– Array is in reverse order:

• Average-case:  O(n2)
– We have to look at all possible initial data organizations.
Analysis of insertion sort
• Which running time will be used to characterize this
algorithm?
– Best, worst or average?
• Worst:
– Longest running time (this is the upper limit for the algorithm)
– It is guaranteed that the algorithm will not be worse than this.
• Sometimes we are interested in average case. But there
are some problems with the average case.
– It is difficult to figure out the average case. i.e. what is average
input?
– Are we going to assume all possible inputs are equally likely?
– In fact for most algorithms average case is same as the worst
case.
Bubble Sort
• Repeatedly swap adjacent elements that are out
of order.
• https://youtu.be/vxENKlcs2Tw

• Sort pairs of elements far apart from each


other, then progressively reduce the gap
between elements to be compared. Starting
with far apart elements, it can move some out-
of-place elements into position faster than a
simple nearest neighbor exchange. This
generalization is called Shell Sort.
Bubble Sort
23 78 45 8 32 56
23 45 78 8 32 56
23 45 8 78 32 56
23 45 8 32 78 56
23 45 8 32 56 78
23 8 45 32 56 78
8 23 45 32 56 78
8 23 32 45 56 78
Bubble Sort Algorithm
template <class Item>
void bubleSort(Item a[], int n)
{
bool sorted = false;
int last = n-1;

for (int i = 0; (i < last) && !sorted; i++){


sorted = true;
for (int j=last; j > i; j--)
if (a[j-1] > a[j]{
swap(a[j],a[j-1]);
sorted = false; // signal exchange
}
}
}
Bubble Sort – Analysis
• Best-case:  O(n)
– Array is already sorted in ascending order.
• Worst-case:  O(n2)
– Array is in reverse order:
• Average-case:  O(n2)
– We have to look at all possible initial data organizations.

• In fact, any sorting algorithm which sorts elements by


swapping adjacent elements can be proved to have an O(n2)
average case complexity
Theorem
• Any sorting algorithm which sorts elements by swapping
adjacent elements can be proved to have an O(n2) average
case complexity
• To understand this, consider the following array:
• [3, 2, 1]
• Here, we have three inversions: (3, 2), (3, 1), (2, 1)
• An inversion is defined as a pair (a, b) where a > b and
index(a) < index(b)
• Note that a single swap of adjacent elements can only remove
one inversion. Thus, for the above example we need 3 swaps
to make the array sorted
Theorem
• Generalizing this, for an array of n elements, there can be
C(n,2) total pairs (where C represents combination)
• C(n,2) = n(n-1) / 2
• We can assume that, on average, half of these pairs are
inversions.
• Average number of inversions = n(n-1) / 4
• As each swap of adjacent elements can remove a single
inversion, we need n(n-1) / 4 swaps to remove all inversions
(that is to make the array sorted)
• The complexity of n(n-1) / 4 swaps is equal to O(n2)
• This is the lower bound on the average case complexity of
sorting algorithms that sort by swapping adjacent elements
Recursive Insertion Sort
• To sort A[1..n], we recursively sort A[1..n-1] and then insert
A[n] into the sorted array A[1..n-1]
• A good practice of recursion
• Code?
Recursive Insertion Sort
• To sort A[1..n], we recursively sort A[1..n-1] and then insert
A[n] into the sorted array A[1..n-1]

insertionSortRec(A, n)
{
if (n>1)
insertionSortRec(A, n-1)
insertKeyIntoSubarray(A[n], A[1..n-1]) //trivial; can
//be done
in //O(n)
}
Recursive Insertion Sort
• To sort A[1..n], we recursively sort A[1..n-1] and then insert
A[n] into the sorted array A[1..n-1]
• A=524613
insertKeyIntoSubarray

12456 3

2456 1

245 6

25 4

5 2
Recursive Insertion Sort
• To sort A[1..n], we recursively sort A[1..n-1] and then insert
A[n] into the sorted array A[1..n-1]
• T(n) = T(n-1) + O(n) is the time complexity of this sorting
Recursive Insertion Sort
• To sort A[1..n], we recursively sort A[1..n-1] and then insert
A[n] into the sorted array A[1..n-1]
• T(n) = T(n-1) + O(n)
• Reduce the problem size to n-1 w/ O(n) extra work
• Seems to be doing O(n) work n-1 times; so O(n2) guess?
• T(n) ≤ cn2 //assume holds
T(n) ≤ c(n-1)2 + dn
= cn2 -2cn + c + dn
= cn2 –c(2n -1) + dn
≤ cn2 //because large values of c dominates d
Recurrences
• How about the complexity of T(n) = T(n/2) + O(1)
• Reduce the problem size to half w/ O(1) extra work
• Seems to be doing O(1) work logn times; so O(logn) guess?
• T(n) ≤ clogn //assume holds
T(n) ≤ c(log(n/2)) + d
= clogn – clog2 + d
= clogn – e //c can always selected to be > constant d
≤ clogn
Recurrences
• How about the complexity of T(n) = 2T(n/2) + O(n)
• Reduce the problem to 2 half-sized problems w/ n extra
work
• Seems to be doing O(n) work logn times; so O(nlogn)
guess?
• T(n) ≤ cnlogn //assume holds
T(n) ≤ 2c(n/2 log(n/2)) + dn
= cnlogn – cnlog2 + dn
= cnlogn – n(c’ + d) //c’ = clog2
≤ cnlogn
Recurrences
• How about the complexity of T(n) = T(n-1) + T(n-2)
• Reduce? the problem to a twice bigger one w/ no extra
work
• T(n-1) + T(n-2) > 2T(n-2)  n replaced by 2n-4 (doubled)
• Or, n-size problem replaced with 2n-3 size problem
(doubled)
• Seems to be doubling the problem size; so O(2n) is good
guess
Fast Algorithm
• Describe an algorithm that, given a set S of n integers and
another integer x, determines whether or not there exists 2
elements in S whose sum is exactly x
• O(n2) //brute force
Fast Algorithm
• Describe an algorithm that, given a set S of n integers and
another integer x, determines whether or not there exists 2
elements in S whose sum is exactly x
• O(nlogn) //sort followed by binary search
Mergesort
• Mergesort algorithm is one of two important divide-and-
conquer sorting algorithms (the other one is quicksort).
• It is a recursive algorithm.
– Divides the list into halves,
– Sort each halve separately (recursively), and
– Then merge the sorted halves into one sorted array.
• https://youtu.be/es2T6KY45cA
Mergesort - Example

6 3 9divide
15472
6 3 9 1 5 4 7 2
divide divide
6 3 9 1 5 4 7 2

divide divide divide divide


6 3 9 1 5 4 7 2
merge merge merge merge

3 6 1 9 4 5 2 7
merge merge

1 3 6 9 2 4 5 7
merge

12345679
Merge
const int MAX_SIZE = maximum-number-of-items-in-array;
void merge(DataType theArray[], int first, int mid, int last)
{
DataType tempArray[MAX_SIZE]; // temporary array
int first1 = first; // beginning of first subarray
int last1 = mid; // end of first subarray
int first2 = mid + 1; // beginning of second subarray
int last2 = last; // end of second subarray
int index = first1; // next available location in tempArray

for ( ; (first1 <= last1) && (first2 <= last2); ++index)


if (theArray[first1] < theArray[first2])
tempArray[index] = theArray[first1++];
else
tempArray[index] = theArray[first2++];
Merge (cont.)
// finish off the first subarray, if necessary
for (; first1 <= last1; ++first1, ++index)
tempArray[index] = theArray[first1];

// finish off the second subarray, if necessary


for (; first2 <= last2; ++first2, ++index)
tempArray[index] = theArray[first2];

// copy the result back into the original array


for (index = first; index <= last; ++index)
theArray[index] = tempArray[index];
}
Mergesort
void mergesort(DataType theArray[], int first, int last) {
if (first < last) { // more than one item
int mid = (first + last)/2; // index of midpoint
mergesort(theArray, first, mid);
mergesort(theArray, mid+1, last);

// merge the two halves


merge(theArray, first, mid, last);
}
} // end mergesort
Analysis of Mergesort
• What is the complexity of the merge operation for merging
two lists of size n/2?
• It is O(n) as we need to copy all elements
• Then the complexity of mergesort can be defined using the
following recurrence relation:
T(n) = 2T(n/2) + n
• Solving this relation gives us O(nlogn) complexity //Slide 30
• The complexity is the same for the best, worst, and average
cases
• The disadvantage of mergesort is that we need to use an
extra array for the merge operation (not memory efficient)
Analysis of Mergesort
• Solving T(n) = 2T(n/2) + n gives us O(nlogn) complexity
Merging Step (Linear time)
• Extra array for merge operation
Merging Step (Linear time)
Demo
• On array = {5, 2, 4, 7, 1, 3, 2, 6}
Quicksort
• Like mergesort, quicksort is also based on
the divide-and-conquer paradigm.
• But it uses this technique in a somewhat opposite manner,
as all the hard work is done before the recursive calls.
• It works as follows:
1. First selects a pivot element,
2. Then it partitions an array into two parts (elements
smaller than and greater than or equal to the pivot)
3. Then, it sorts the parts independently (recursively),
4. Finally, it combines the sorted subsequences by
a simple concatenation.
• https://youtu.be/vxENKlcs2Tw
Partition
• Partitioning places the pivot in its correct place position within the array.

• Arranging the array elements around the pivot p generates two smaller
sorting problems.
– sort the left section of the array, and sort the right section of the array.
– when these two smaller sorting problems are solved recursively, our
bigger sorting problem is solved.
Pivot Selection
• Which array item should be selected as pivot?
– Somehow we have to select a pivot, and we hope that we
will get a good partitioning.
– If the items in the array arranged randomly, we choose a
pivot randomly.
– We can choose the first or last element as the pivot (it may
not give a good partitioning).
– We can choose the middle element as the pivot
– We can use a combination of the above to select the pivot
(in each recursive call a different technique can be used)
Partitioning

Initial state of the array


Partitioning
Partitioning

Moving theArray[firstUnknown] into S1 by swapping it with


theArray[lastS1+1] and by incrementing both lastS1 and firstUnknown.
Partitioning

Moving theArray[firstUnknown] into S2 by incrementing firstUnknown.


Partitioning – An Example
Partition Function
template <class DataType>
void partition(DataType theArray[], int first, int last,
int &pivotIndex) {

int pIndex = choosePivot(theArray, first, last);

// put pivot at position first


swap(theArray[pIndex], theArray[first]);

DataType pivot = theArray[first]; // copy pivot


Partition Function (cont.)
int lastS1 = first; // index of last item in S1
int firstUnknown = first + 1; //index of 1st item in unknown

// move one item at a time until unknown region is empty


for (; firstUnknown <= last; ++firstUnknown) {
if (theArray[firstUnknown] < pivot)
++lastS1;
swap(theArray[firstUnknown], theArray[lastS1]);
}

// place pivot in proper position and mark its location


swap(theArray[first], theArray[lastS1]);
pivotIndex = lastS1;
}
Quicksort Function
void quicksort(DataType theArray[], int first, int last) {

int pivotIndex;
if (first < last) {

partition(theArray, first, last, pivotIndex);

// sort regions S1 and S2


quicksort(theArray, first, pivotIndex-1);
quicksort(theArray, pivotIndex+1, last);
}
}
Quicksort – Analysis
• If we always select the smallest or largest element as the pivot,
we’ll not be able to divide the array into similar sized
partitions
• In that case, the complexity of the quicksort can be defined
by:
T(n) = n + T(1) + T(n-1)
• This gives O(n2) complexity (worst case) //Slide 28
• If our partitions are equal sized we have:
T(n) = n + 2T(n/2) (same as mergesort)
• This gives O(nlogn) complexity (best case)
• On average, quicksort has been proven to have O(nlogn)
complexity as well (based on alternation b/w good/bad
partitions)
C/C++ Language Support
• We can implement sorting algorithms ourselves
• We can also use existing implementations
• In C, the function qsort (part of stdlib.h header) implements
the quicksort algorithm
• In C++ std::sort (part of algorithm header) implements a
mixture of quicksort, heapsort, and insertion sort

• Many sorting algorithms in comparison:


https://youtu.be/ZZuD6iUe3Pc
Sorting vs. Geometry
• A cool reduction of Convex Hull problem into Sorting
problem
• This is the convex hull of the point set P:
•Release a large rubber band around points/nails

• Given unsorted a0, a1, a2, .., an, construct set of points in the
plane via ai=ai2 parabola as shown at left

• Every point must be on the convex hull ‘cos parabolas are


convex
Bubble Sort
Merge Sort
Bubble Sort
Sorting
• Sorting takes an unordered collection and
makes it an ordered one.
1 2 3 4 5 6

77 42 35 12 101 5

1 2 3 4 5 6
5 12 35 42 77 101
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

77 42 35 12 101 5
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6
42Swap77 12 101
77 42 35 5
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35Swap35
77 77 12 101 5
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35 12Swap12
77 77 101 5
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35 12 77 101 5

No need to swap
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35 12 77 5 Swap101
101 5
"Bubbling Up" the Largest Element

• Traverse a collection of elements


– Move from the front to the end
– “Bubble” the largest value to the end using
pair-wise comparisons and swapping

1 2 3 4 5 6

42 35 12 77 5 101

Largest value correctly placed


The “Bubble Up” Algorithm

index <- 1
last_compare_at <- n – 1

loop
exitif(index > last_compare_at)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index <- index + 1
endloop
LB

No, Swap isn’t built in.

Procedure Swap(a, b isoftype in/out Num)


t isoftype Num
t <- a
a <- b
b <- t
endprocedure // Swap
Items of Interest
• Notice that only the largest value is
correctly placed
• All other values are still out of order
• So we need to repeat this process

1 2 3 4 5 6

42 35 12 77 5 101

Largest value correctly placed


Repeat “Bubble Up” How Many Times?
• If we have N elements…

• And if each time we bubble an element,


we place it in its correct location…

• Then we repeat the “bubble up”


process N – 1 times.

• This guarantees we’ll correctly


place all N elements.
“Bubbling” All the Elements
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
N-1

12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
1 2 3 4 5 6
5 12 35 42 77 101
Reducing the Number of Comparisons
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
42 35 12 77 5 101
1 2 3 4 5 6
35 12 42 5 77 101
1 2 3 4 5 6
12 35 5 42 77 101
1 2 3 4 5 6
12 5 35 42 77 101
Reducing the Number of Comparisons
• On the Nth “bubble up”, we only need to
do MAX-N comparisons.

• For example:
– This is the 4th “bubble up”
– MAX is 6
– Thus we have 2 comparisons to do

1 2 3 4 5 6
12 35 5 42 77 101
Putting It All Together
N is … // Size of Array

Arr_Type definesa Array[1..N] of Num

Procedure Swap(n1, n2 isoftype in/out Num)


temp isoftype Num
temp <- n1
n1 <- n2
n2 <- temp
endprocedure // Swap
procedure Bubblesort(A isoftype in/out Arr_Type)
to_do, index isoftype Num
to_do <- N – 1

loop
exitif(to_do = 0)
index <- 1
loop
exitif(index > to_do)

Outer loop
Inner loop
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index <- index + 1
endloop
to_do <- to_do - 1
endloop
endprocedure // Bubblesort
Already Sorted Collections?
• What if the collection was already sorted?
• What if only a few elements were out of place and
after a couple of “bubble ups,” the collection was
sorted?

• We want to be able to detect this


and “stop early”!
1 2 3 4 5 6
5 12 35 42 77 101
Using a Boolean “Flag”

• We can use a boolean variable to determine if any


swapping occurred during the “bubble up.”

• If no swapping occurred, then we know that the


collection is already sorted!

• This boolean “flag” needs to be reset after each


“bubble up.”
did_swap isoftype Boolean
did_swap <- true

loop
exitif ((to_do = 0) OR NOT(did_swap))
index <- 1
did_swap <- false
loop
exitif(index > to_do)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
did_swap <- true
endif
index <- index + 1
endloop
to_do <- to_do - 1
endloop
An Animated Example

N 8 did_swap true
to_do 7
index

98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap false
to_do 7
index 1

98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap false
to_do 7
index 1

Swap

98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 1

Swap

23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 2

23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 2

Swap

23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 2

Swap

23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 3

23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 3

Swap

23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 3

Swap

23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 4

23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 4

Swap

23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 4

Swap

23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 5

23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 5

Swap

23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 5

Swap

23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 6

23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 6

Swap

23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 6

Swap

23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 7

23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 7

Swap

23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example

N 8 did_swap true
to_do 7
index 7

Swap

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
After First Pass of Outer Loop

N 8 did_swap true
to_do 7
index 8 Finished first “Bubble Up”

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap false
to_do 6
index 1

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap false
to_do 6
index 1

No Swap

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap false
to_do 6
index 2

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap false
to_do 6
index 2

Swap

23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 2

Swap

23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 3

23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 3

Swap

23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 3

Swap

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 4

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 4

No Swap

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 5

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 5

Swap

23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 5

Swap

23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 6

23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 6

Swap

23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”

N 8 did_swap true
to_do 6
index 6

Swap

23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
After Second Pass of Outer Loop

N 8 did_swap true
to_do 6
index 7 Finished second “Bubble Up”

23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap false
to_do 5
index 1

23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap false
to_do 5
index 1

Swap

23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 1

Swap

14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 2

14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 2

Swap

14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 2

Swap

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 3

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 3

No Swap

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 4

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 4

Swap

14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 4

Swap

14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 5

14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 5

Swap

14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”

N 8 did_swap true
to_do 5
index 5

Swap

14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Third Pass of Outer Loop

N 8 did_swap true
to_do 5
index 6 Finished third “Bubble Up”

14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap false
to_do 4
index 1

14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap false
to_do 4
index 1

Swap

14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 1

Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 2

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 2

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 3

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 3

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 4

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”

N 8 did_swap true
to_do 4
index 4

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Fourth Pass of Outer Loop

N 8 did_swap true
to_do 4
index 5 Finished fourth “Bubble Up”

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”

N 8 did_swap false
to_do 3
index 1

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”

N 8 did_swap false
to_do 3
index 1

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”

N 8 did_swap false
to_do 3
index 2

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”

N 8 did_swap false
to_do 3
index 2

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”

N 8 did_swap false
to_do 3
index 3

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”

N 8 did_swap false
to_do 3
index 3

No Swap

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Fifth Pass of Outer Loop

N 8 did_swap false
to_do 3
index 4 Finished fifth “Bubble Up”

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
Finished “Early”

N 8 did_swap false
to_do 3
We didn’t do any swapping,
index 4 so all of the other elements
must be correctly placed.

We can “skip” the last two


passes of the outer loop.

6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
Summary
• “Bubble Up” algorithm will move largest
value to its correct location (to the right)
• Repeat “Bubble Up” until all elements are
correctly placed:
– Maximum of N-1 times
– Can finish early if no swapping occurs
• We reduce the number of elements we
compare each time one is correctly placed
LB

Truth in CS Act

• NOBODY EVER USES BUBBLE SORT

• NOBODY

• NOT EVER

• BECAUSE IT IS EXTREMELY INEFFICIENT


Questions?
Mergesort
Sorting
• Sorting takes an unordered collection and
makes it an ordered one.
1 2 3 4 5 6

77 42 35 12 101 5

1 2 3 4 5 6
5 12 35 42 77 101
Divide and Conquer

• Divide and Conquer cuts the problem in


half each time, but uses the result of both
halves:
– cut the problem in half until the problem
is trivial
– solve for both halves
– combine the solutions
Mergesort
• A divide-and-conquer algorithm:
• Divide the unsorted array into 2 halves until the
sub-arrays only contain one element
• Merge the sub-problem solutions together:
– Compare the sub-array’s first elements
– Remove the smallest element and put it into
the result array
– Continue the process until all elements have
been put into the result array

37 23 6 89 15 12 2 19
How to Remember Merge Sort?
That’s easy. Just
remember Mariah
Carey.

As a singing star, Ms.


Carey has perfected
the “wax-on” wave
motion--a clockwise
sweep of her hand
used to emphasize
lyrics.

The Maria
“Wax-on” Angle:
(,t)
The Siren of Subquadratic Sorts
How To Remember Merge Sort?

(,t)

Just as Mariah recursively moves


her hands into smaller circles, so
too does merge sort recursively We need two such recursions,
split an array into smaller one for each half of the split
segments. array.
Algorithm
Mergesort(Passed an array)
if array size > 1
Divide array in half
Call Mergesort on first half.
Call Mergesort on second half.
Merge two halves.

Merge(Passed two arrays)


Compare leading element in each array
Select lower and place in new array.
(If one input array is empty then place
remainder of other array in output array)
LB

More TRUTH in CS

• We don’t really pass in two arrays!

• We pass in one array with indicator variables which


tell us where one set of data starts and finishes and
where the other set of data starts and finishes.

• Honest.

s1 f1 s2 f2
LB

Algorithm
Mergesort(Passed an array)
if array size > 1
Divide array in half
Call Mergesort on first half.
Call Mergesort on second half.
Merge two halves.

Merge(Passed two arrays)


Compare leading element in each array
Select lower and place in new array.
(If one input array is empty then place
remainder of other array in output array)
98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23
Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23

23 98
Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23 45

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

98 23 45 14

23 98 14 45

14 23 45 98

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14

23 98 14 45

14 23 45 98
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45

14 23 45 98
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45 6

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67

23 98 14 45 6 67

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67

14 23 45 98
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98

Merge
98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42

23 98 14 45 6 67 33 42

14 23 45 98 6 33 42 67

6 14 23 33 42 45 67 98
98 23 45 14 6 67 33 42

6 14 23 33 42 45 67 98
Summary

• Divide the unsorted collection into two

• Until the sub-arrays only contain one


element

• Then merge the sub-problem solutions


together
Questions?
Review?

You might also like