Intro To AlgorithmsIntro To AlgorithmsSearching and SortingSearching and Sorting
SearchingSearching A common task for a computer is to A common task for a computer is to
find a block of datafind a block of data We’re going to look at the two most We’re going to look at the two most
common and basic of the searching common and basic of the searching algorithms:algorithms:– Linear SearchLinear Search– Binary SearchBinary Search
Linear SearchLinear Search Linear search is performed in the order in Linear search is performed in the order in
which the data appears:which the data appears: Ex. Below is an animation of a linear search Ex. Below is an animation of a linear search
for the value 7:for the value 7:datadata 33 99 55 22 77 11 88 1212 7575indeindexx 00 11 22 33 44 55 66 77 88
Found at index 4, search stopped
Linear Search AlgorithmLinear Search Algorithm Here is the pseudo code:Here is the pseudo code: For each index in the array, compare For each index in the array, compare
the object/data to the item you are the object/data to the item you are looking for.looking for.– If the item is found, returnIf the item is found, return– Else, go to the next indexElse, go to the next index
IssuesIssues Linear search is great if the data you want is Linear search is great if the data you want is
at the front of the arrayat the front of the array It’s fairly robust in that it works on any data It’s fairly robust in that it works on any data
in any orderin any order If the data you’re trying to find is in the last If the data you’re trying to find is in the last
position, you have to look at every other position, you have to look at every other one firstone first
What do you do if the data is not in the What do you do if the data is not in the array?array?– Usually you return a null object or the index -1Usually you return a null object or the index -1
Linear Searching the Linear Searching the DictionaryDictionary
Suppose you’re going to find Suppose you’re going to find ZebraZebra in the dictionary using linear searchin the dictionary using linear search
You have to look at each word, one You have to look at each word, one at a time until you arrive at at a time until you arrive at ZebraZebra
In real life you wouldn’t do this (I In real life you wouldn’t do this (I hope)hope)
How do you search the dictionary in How do you search the dictionary in real life?real life?
Binary Search AlgorithmBinary Search Algorithm Much like the way you search a dictionary in Much like the way you search a dictionary in
real life, we can improve our linear searching real life, we can improve our linear searching algorithm for algorithm for sorted datasorted data
Open the dictionary in the middle, check which Open the dictionary in the middle, check which word you’re looking at, (probably something like word you’re looking at, (probably something like M).M).
Zebra is after so throw away the first half of the Zebra is after so throw away the first half of the dictionary (no need to look there)dictionary (no need to look there)
Now with the remaining half, look in the Now with the remaining half, look in the middle… middle…
Repeat until you find the word you’re looking forRepeat until you find the word you’re looking for
Binary Search ExampleBinary Search Example Suppose you have the values 1 to 100 in Suppose you have the values 1 to 100 in
ascending orderascending order If you were looking for the number 71, what If you were looking for the number 71, what
numbers would you encounter along the way?numbers would you encounter along the way?– Interval (1,100) half Interval (1,100) half (1 + 100)/2 = 50 (1 + 100)/2 = 50– 50 is not the target, the target is larger50 is not the target, the target is larger– Interval (51,100) half Interval (51,100) half (51 + 100)/2 = 75 (51 + 100)/2 = 75– 75 is not the target, the target is smaller75 is not the target, the target is smaller– Interval (51,74) half Interval (51,74) half (51 + 74)/2 = 62 (51 + 74)/2 = 62– 62 is not the target, the target is larger62 is not the target, the target is larger– Interval (63,74) half Interval (63,74) half (63 + 74)/2 = 68 (63 + 74)/2 = 68– 68 is not the target, the target is larger68 is not the target, the target is larger– Interval (69,74) half Interval (69,74) half (69 + 74)/2 = 71 (69 + 74)/2 = 71– 71 is the target, stop71 is the target, stop
In this case, it took us 5 comparisons vs. 71 with In this case, it took us 5 comparisons vs. 71 with linear searchlinear search
Binary Search AlgorithmBinary Search Algorithm Here is the pseudo code for the algorithm:Here is the pseudo code for the algorithm: Calculate the size of the array (number of elements)Calculate the size of the array (number of elements) mid = size/2, first = 0, last = size – 1mid = size/2, first = 0, last = size – 1 While first is less than lastWhile first is less than last
– If target is in index midIf target is in index mid Stop, return object (or index mid)Stop, return object (or index mid)
– Otherwise if the target is greater than the object at index Otherwise if the target is greater than the object at index midmid start = mid + 1// shift the interval overstart = mid + 1// shift the interval over
– Otherwise (target is less than the object at mid)Otherwise (target is less than the object at mid) end = mid – 1end = mid – 1
– mid = (start + end)/2//update the midmid = (start + end)/2//update the mid At this point the only way out of the loop is if the At this point the only way out of the loop is if the
target was not found, return a sentinel valuetarget was not found, return a sentinel value
SortingSorting How do you sort a collection of values?How do you sort a collection of values? This is another common task for a computerThis is another common task for a computer There are many, many algorithms that for sorting There are many, many algorithms that for sorting
data, here are some of the most popular:data, here are some of the most popular:– Selection SortSelection Sort– Insertion SortInsertion Sort– Heap SortHeap Sort– Merge SortMerge Sort– Quick SortQuick Sort– Radix SortRadix Sort– Bubble SortBubble Sort– ……
We’re focusing on the first two, Selection and We’re focusing on the first two, Selection and Insertion SortInsertion Sort
Selection SortSelection Sort Like the name implies, the idea is to Like the name implies, the idea is to
‘select’ the smallest value in the collection ‘select’ the smallest value in the collection and move it to the front.and move it to the front.
The pseudo code is:The pseudo code is:– Start at index n = 0Start at index n = 0– While n is less than the last indexWhile n is less than the last index
Find the smallest value in the collectionFind the smallest value in the collection Swap it with the value at index nSwap it with the value at index n n = n + 1 //go to the next valuen = n + 1 //go to the next value
Swapping values (one, two):Swapping values (one, two):– copy = onecopy = one– one = twoone = two– two = copytwo = copy
Selection Sort SampleSelection Sort Sample
77 55 33 99 88 44 22
22 33 55 99 88 44 77
22 55 33 99 88 44 77
22 33 44 99 88 55 77
22 33 44 55 88 99 77
22 33 44 55 77 88 99
22 33 44 55 77 99 88
Here is a sample run of the selection sort algorithm:
Find the smallest item
Swap it with the item at the front
Insertion SortInsertion Sort Insertion sort is another simple but Insertion sort is another simple but
inefficient algorithminefficient algorithm The implementation is a bit tougher The implementation is a bit tougher
since we’ll have to insert elements since we’ll have to insert elements rather than swap themrather than swap them
Here’s the idea:Here’s the idea:– Start with a sorted listStart with a sorted list– Take in a new value by inserting it to the Take in a new value by inserting it to the
proper locationproper location
Implementing Insertion SortImplementing Insertion Sort How do you start with a sorted list How do you start with a sorted list
when you’re trying to sort the list?when you’re trying to sort the list? Start with a list with one elementStart with a list with one element For the sample run I’ll put the sorted For the sample run I’ll put the sorted
list in {} style brackets and the list in {} style brackets and the remainder of the items will followremainder of the items will follow
Sample RunSample Run SortingSorting
– 5,7,3,4,2,9,8,1,65,7,3,4,2,9,8,1,6– {5},7,3,4,2,9,8,1,6 insert 7 to the list{5},7,3,4,2,9,8,1,6 insert 7 to the list– {5,7},3,4,2,9,8,1,6 insert 3 to the list{5,7},3,4,2,9,8,1,6 insert 3 to the list– {3,5,7},4,2,9,8,1,6 insert 4 to the list{3,5,7},4,2,9,8,1,6 insert 4 to the list– {3,4,5,7},2,9,8,1,6 insert 2 to the list{3,4,5,7},2,9,8,1,6 insert 2 to the list– {2,3,4,5,7},9,8,1,6 insert 9 to the list{2,3,4,5,7},9,8,1,6 insert 9 to the list– {2,3,4,5,7,9},8,1,6 insert 8 to the list{2,3,4,5,7,9},8,1,6 insert 8 to the list– {2,3,4,5,7,8,9},1,6 insert 1 to the list{2,3,4,5,7,8,9},1,6 insert 1 to the list– {1,2,3,4,5,7,8,9},6 insert 6 to the list{1,2,3,4,5,7,8,9},6 insert 6 to the list– {1,2,3,4,5,6,7,8,9} the list is sorted{1,2,3,4,5,6,7,8,9} the list is sorted
Insertion Sort AlgorithmInsertion Sort Algorithm Here is the pseudo code:Here is the pseudo code:
– index = 1index = 1– While index < size of collectionWhile index < size of collection
insert = Array[index] //object to insertinsert = Array[index] //object to insert current = index – 1 //end of sorted listcurrent = index – 1 //end of sorted list While current > 0 and insert < Array[current]While current > 0 and insert < Array[current]
– Array[current] = Array[current+1] Array[current] = Array[current+1] – current = current – 1current = current – 1
Array[current+1] = insert Array[current+1] = insert index = index + 1index = index + 1
Big-O notationBig-O notation How do we compare algorithms?How do we compare algorithms? There are usually two considerationsThere are usually two considerations
– Space requirements (memory)Space requirements (memory)– Time requirements (speed of execution)Time requirements (speed of execution)
We’re going to take a short We’re going to take a short introduction to how computer introduction to how computer scientists compare the speed of scientists compare the speed of algorithms.algorithms.
Classes of FunctionsClasses of Functions Which function is larger?Which function is larger?
We say they are all related, that they We say they are all related, that they “behave” the same way, since the “behave” the same way, since the most significant part of them is xmost significant part of them is x22
As computer scientists, we’d say As computer scientists, we’d say they are: they are:
2 2 2 215 3 1 7 1000100
x x x x x
2O x
Complexity of SearchingComplexity of Searching Linear search:Linear search:
– Since the worst case is that we have to search through each Since the worst case is that we have to search through each element in a collection, linear search is O(n)element in a collection, linear search is O(n)
– This means it requires approximately the same number of This means it requires approximately the same number of executions in the CPU as there are values in the collectionexecutions in the CPU as there are values in the collection
Binary Search:Binary Search:– Since binary search is a divide and conquer algorithm, it Since binary search is a divide and conquer algorithm, it
executes much faster.executes much faster.– It’s easiest to find the relationship by trying some examplesIt’s easiest to find the relationship by trying some examples– If there were 8 values, then if worst came to worst:If there were 8 values, then if worst came to worst:
8844221 the problem is reduced to a singleton in 3 steps1 the problem is reduced to a singleton in 3 steps 32 32 16 16 8 8 4 4 2 2 1 reduced in 5 steps 1 reduced in 5 steps 256 256 128 128 64 64 32 32 16 16 8 8 4 4 2 2 1 reduced in 8 1 reduced in 8
stepssteps– 223 3 = 8, 2= 8, 255 = 32, 2 = 32, 28 8 = 256= 256– The relationship is O(logThe relationship is O(log22n)n)
Complexity of SortingComplexity of Sorting The best sorting algorithms execute The best sorting algorithms execute
O(nlogn)O(nlogn) So far all of the sorting algorithms So far all of the sorting algorithms
we’ve seen execute O(nwe’ve seen execute O(n22) ) How do we do better?How do we do better? We’ll only learn one sorting algorithm We’ll only learn one sorting algorithm
that is O(nlogn), but there are many that is O(nlogn), but there are many othersothers
MergesortMergesort Here’s the idea:Here’s the idea: Take two sorted lists and merge them Take two sorted lists and merge them
together.together.– Ex.Ex.{ 1,3,5,7 } { 1,3,5,7 } { 2, 4, 6, 8 } { 2, 4, 6, 8 }Becomes: {1,2,3,4,5,6,7,8}Becomes: {1,2,3,4,5,6,7,8}This turns out to be MUCH faster in practice than This turns out to be MUCH faster in practice than
both Insertion sort and Selection sort.both Insertion sort and Selection sort.The next slides illustrate how to sort values with The next slides illustrate how to sort values with
any initial order so they can be merged together any initial order so they can be merged together
85 24 63 45 17 31 96 50
Mergesort: IllustrationMergesort: IllustrationThe initial list of values:
85 24 63 45 17 31 96 50
Mergesort: IllustrationMergesort: IllustrationSplit the values in two: Divide and conquer
85 24 63 45
17 31 96 50
Mergesort: IllustrationMergesort: IllustrationContinue splitting the lists up until they are sorted…Base case for sorted lists is two lists with a single element each
63 45
85 24
17 31 96 50
Mergesort: IllustrationMergesort: IllustrationFirst pair of sorted lists:
Mergesort: IllustrationMergesort: Illustration
63 45
85 24
17 31 96 50
Compare the elements to merge them:
Mergesort: IllustrationMergesort: Illustration
24 63 45
85
17 31 96 50
Move the winner to the front of the next list
Mergesort: IllustrationMergesort: Illustration
24 63 45
85
17 31 96 50
There is nothing to compare 85 with, so it wins by default
Mergesort: IllustrationMergesort: Illustration
24 85 63 45
17 31 96 50
Move 85 ti the next spot in the list
Mergesort: IllustrationMergesort: Illustration
24 85 63 45
17 31 96 50
The list has been “merged”, the values are sorted
Mergesort: IllustrationMergesort: Illustration
24 85 63 45
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 85
63 45
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 85
63 45
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 85 45
63
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 85 45
63
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 85 45 63
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 85 45 63
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 85 45 63
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 85 45 63
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24
85 45 63
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24
85 45 63
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 45
85 63
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 45
85 63
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 45 63
85
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 45 63
85
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 45 63 85 17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 45 63 85 17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 45 63 85 17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 45 63 85
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 45 63 85
96 50
17 31
Mergesort: IllustrationMergesort: Illustration
24 45 63 85
96 50
17 31
Mergesort: IllustrationMergesort: Illustration
24 45 63 85
17 96 50
31
Mergesort: IllustrationMergesort: Illustration
24 45 63 85
17 96 50
31
Mergesort: IllustrationMergesort: Illustration
24 45 63 85
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 45 63 85
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 45 63 85
17 31 96 50
Mergesort: IllustrationMergesort: Illustration
24 45 63 85
17 31
96 50
Mergesort: IllustrationMergesort: Illustration
24 45 63 85
17 31
96 50
Mergesort: IllustrationMergesort: Illustration
24 45 63 85
17 31 50
96
Mergesort: IllustrationMergesort: Illustration
24 45 63 85
17 31 50
96
Mergesort: IllustrationMergesort: Illustration
24 45 63 85
17 31 50 96
Mergesort: IllustrationMergesort: Illustration
24 45 63 85
17 31 50 96
Mergesort: IllustrationMergesort: Illustration
24 45 63 85
17 31 50 96
Mergesort: IllustrationMergesort: Illustration
24 45 63 85
17 31 50 96
Mergesort: IllustrationMergesort: Illustration
24 45 63 85 17
31 50 96
Mergesort: IllustrationMergesort: Illustration
24 45 63 85 17
31 50 96
Mergesort: IllustrationMergesort: Illustration
24 45 63 85 17 31
50 96
Mergesort: IllustrationMergesort: Illustration
24 45 63 85 17 31
50 96
Mergesort: IllustrationMergesort: Illustration
24 45 63 85 17 31 50 96
Mergesort: IllustrationMergesort: Illustration
24 45 63 85 17 31 50 96
Mergesort: IllustrationMergesort: Illustration
24 45 63 85 17 31 50 96
Mergesort: IllustrationMergesort: Illustration
24 45 63 85 17 31 50 96
Mergesort: IllustrationMergesort: Illustration
17
24 45 63 85 31 50 96
Mergesort: IllustrationMergesort: Illustration
17
24 45 63 85 31 50 96
Mergesort: IllustrationMergesort: Illustration
17 24
45 63 85 31 50 96
Mergesort: IllustrationMergesort: Illustration
17 24
45 63 85 31 50 96
Mergesort: IllustrationMergesort: Illustration
17 24 31
45 63 85 50 96
Mergesort: IllustrationMergesort: Illustration
17 24 31
45 63 85 50 96
Mergesort: IllustrationMergesort: Illustration
17 24 31 45
63 85 50 96
Mergesort: IllustrationMergesort: Illustration
17 24 31 45
63 85 50 96
Mergesort: IllustrationMergesort: Illustration
17 24 31 45 50
63 85 96
Mergesort: IllustrationMergesort: Illustration
17 24 31 45 50
63 85 96
Mergesort: IllustrationMergesort: Illustration
17 24 31 45 50 63
85 96
Mergesort: IllustrationMergesort: Illustration
17 24 31 45 50 63
85 96
Mergesort: IllustrationMergesort: Illustration
17 24 31 45 50 63 85
96
Mergesort: IllustrationMergesort: Illustration
17 24 31 45 50 63 85
96
Mergesort: IllustrationMergesort: Illustration
17 24 31 45 50 63 85 96
Mergesort: IllustrationMergesort: Illustration
17 24 31 45 50 63 85 96
Mergesort: IllustrationMergesort: Illustration
17 24 31 45 50 63 85 96
Web AnimationsWeb Animations http://math.hws.edu/TMCM/java/xSortLab/ https://cs.uwaterloo.ca/~bwbecker/sortingDe
mo/ Sorting out Sorting (Circa 1970!) from Sorting out Sorting (Circa 1970!) from
University of TorontoUniversity of Toronto– http://www.youtube.com/watch?http://www.youtube.com/watch?
v=gv0JUEqaAXov=gv0JUEqaAXo
Programming ExercisesProgramming Exercises Create a program for each of the Create a program for each of the
algorithms in the slidesalgorithms in the slides– Linear SearchLinear Search– Binary SearchBinary Search– Selection SortSelection Sort– Insertion SortInsertion Sort– Merge Sort (you will write the merge Merge Sort (you will write the merge
method, I will provide the rest of the method, I will provide the rest of the code) code) see slides below see slides below
Programming ExercisesProgramming Exercises Your program will do something like the following.Your program will do something like the following. How many values would you like to test? 5How many values would you like to test? 5 Here are 5 random values: {9,3,5,1,4}Here are 5 random values: {9,3,5,1,4} Using Insertion Sort it took 0.0012 seconds and 29 Using Insertion Sort it took 0.0012 seconds and 29
comparisons were madecomparisons were made Using Selection Sort it took 0.0018 seconds and 41 Using Selection Sort it took 0.0018 seconds and 41
comparisons were madecomparisons were made Using Merge Sort it took 0.0008 seconds and 17 Using Merge Sort it took 0.0008 seconds and 17
comparisons were madecomparisons were made
Something similar will be done to compare linear Something similar will be done to compare linear and binary searchingand binary searching
TipsTips The System class has a static method:The System class has a static method:
– long currentTimeMillis()long currentTimeMillis()– Get the time before you run your algorithm and Get the time before you run your algorithm and
immediately after in order to see how long it immediately after in order to see how long it took.took.
Implementing MergesortImplementing Mergesort Mergesort can be done iteratively Mergesort can be done iteratively
(using loops) but in practice, its (using loops) but in practice, its usually done with recursion as the usually done with recursion as the code is much easier to implementcode is much easier to implement
Base case Base case – List of length 1 is sortedList of length 1 is sorted
Recursive caseRecursive case– Split lists in halfSplit lists in half– Merge them back together once sortedMerge them back together once sorted
MergeMerge As discussed previously, inserting values into an array requires As discussed previously, inserting values into an array requires
extra overheadextra overhead An efficient way to merge values in an array is to use a temporary An efficient way to merge values in an array is to use a temporary
copycopy For example For example
– without extra array (have to shuffle values over):without extra array (have to shuffle values over):{1,3,5}{2,4,6} {1,3,5}{2,4,6} {1,3,5,5}{2,4,6}{1,3,5,5}{2,4,6}
{1,3,3,5}{2,4,6}{1,3,3,5}{2,4,6}{1,2,3,5}{4,6}{1,2,3,5}{4,6}
{1,2,3,5}{4,6} {1,2,3,5}{4,6} {1,2,3,5,5}{4,6}{1,2,3,5,5}{4,6}{1,2,3,4,5}{4,6}{1,2,3,4,5}{4,6}
{1,2,3,4,5}{6} {1,2,3,4,5}{6} {1,2,3,4,5,6}{1,2,3,4,5,6}– with extra array:with extra array:{1,3,5}{2,4,6}{1,3,5}{2,4,6} Check which value at the front of the array Check which value at the front of the array
is smallestis smallest{1, _, _, _, _, _}{1, _, _, _, _, _} Copy it to the next value in the extra arrayCopy it to the next value in the extra array{1, 2, _, _, _, _}{1, 2, _, _, _, _}{1, 2, 3, _, _, _}{1, 2, 3, _, _, _}{1, 2, 3, 4, _, _}{1, 2, 3, 4, _, _}{1, 2, 3, 4, 5, _}{1, 2, 3, 4, 5, _}{1, 2, 3, 4, 5, 6}{1, 2, 3, 4, 5, 6}
MergeMerge When sorting data, it is only one array that When sorting data, it is only one array that
we want to sort, not taking many pieces we want to sort, not taking many pieces and merging them together.and merging them together.
We can merge values in the same array We can merge values in the same array together by defining boundaries for where together by defining boundaries for where the first and second array start and end.the first and second array start and end.
{ { 4,7,84,7,8,,1,2,31,2,3,0,9,5 },0,9,5 } One sorted array is shown in red, the other One sorted array is shown in red, the other
in green, merging them within the same in green, merging them within the same array we would get: {array we would get: {1,2,3,4,7,81,2,3,4,7,8,0,9,5 },0,9,5 }
MergeMerge Your method will behave as follows:Your method will behave as follows: merge( array, start, mid, end )merge( array, start, mid, end ) merge( {1,5,7,2,3,0,9}, 0, 3, 4 } )merge( {1,5,7,2,3,0,9}, 0, 3, 4 } ) merges the values starting at index 0 to 2 with the merges the values starting at index 0 to 2 with the
values at index 3 to 4, all values beyond index 4 values at index 3 to 4, all values beyond index 4 are ignoredare ignored
The result in the array is now {1,2,3,5,7,0,9}The result in the array is now {1,2,3,5,7,0,9} Some additional clarification may be needed at Some additional clarification may be needed at
this point in classthis point in class You should create a copy array to merge the You should create a copy array to merge the
values as it is more efficient than inserting in values as it is more efficient than inserting in placeplace
Mergesort codeMergesort codeprivate void mergesortHelper(Compareable[] a, int lo, int hi) private void mergesortHelper(Compareable[] a, int lo, int hi) { {
if (hi - lo <= 1) if (hi - lo <= 1) // base case// base case return; return;
// sort each half, recursively// sort each half, recursively int mid = (lo + hi) / 2; int mid = (lo + hi) / 2; mergesortHelper(a, lo, mid); mergesortHelper(a, lo, mid); mergesortHelper(a, mid, hi); mergesortHelper(a, mid, hi);
// merge back together// merge back together merge(a, lo, mid, hi); merge(a, lo, mid, hi);
} }
public void mergesort(Compareable[] a) public void mergesort(Compareable[] a) { {
int n = a.length;int n = a.length;mergesort(a, 0, n); mergesort(a, 0, n);
} }
Algorithms Pt II (ICTP12)Algorithms Pt II (ICTP12) Bubble SortBubble Sort QuicksortQuicksort
Bubble SortBubble Sort BubbleSort is another O(n^2) sorting algorithm that makes an BubbleSort is another O(n^2) sorting algorithm that makes an
improvement over selection sort by shuffling the values as it finds them.improvement over selection sort by shuffling the values as it finds them. {4,7,3,5,8,6,2,1}{4,7,3,5,8,6,2,1}
– 4 vs. 7, 7 is larger so keep track of 74 vs. 7, 7 is larger so keep track of 7– 7 vs. 3, 3 is smaller so swap them7 vs. 3, 3 is smaller so swap them
{4,3,7,5,8,6,2,1}{4,3,7,5,8,6,2,1}– 5 vs 7, 5 is smaller so swap them5 vs 7, 5 is smaller so swap them
{4,3,5,7,8,6,2,1}{4,3,5,7,8,6,2,1}– 7 vs. 8, 8 is larger so keep track of 87 vs. 8, 8 is larger so keep track of 8
{4,3,5,7,8,6,2,1}{4,3,5,7,8,6,2,1}– 8 vs. 6, 6 is smaller so swap them8 vs. 6, 6 is smaller so swap them
{4,3,5,7,6,8,2,1}{4,3,5,7,6,8,2,1}– 8 vs. 2, 2 is smaller so swap them8 vs. 2, 2 is smaller so swap them
{4,3,5,7,6,2,8,1}{4,3,5,7,6,2,8,1}– 8 vs. 1, 1 is smaller so swap them8 vs. 1, 1 is smaller so swap them
{4,3,5,7,6,2,1,8}{4,3,5,7,6,2,1,8} This is similar in that we have selected 8 to be put in the largest position, This is similar in that we have selected 8 to be put in the largest position,
but along the way we moved all of the smaller values closer to their proper but along the way we moved all of the smaller values closer to their proper position where as Selection Sort would not do any additional moves.position where as Selection Sort would not do any additional moves.
QuicksortQuicksort Quicksort is a very fast sorting algorithm, hence the Quicksort is a very fast sorting algorithm, hence the
name. Caution is needed though as its worst case name. Caution is needed though as its worst case scenario is O(n^2) while it typically performs O(nlgn)scenario is O(n^2) while it typically performs O(nlgn)
Here is the idea:Here is the idea:– Choose a value, call this the pivotChoose a value, call this the pivot– Process the array so that at the end of your Process the array so that at the end of your
method you will have put everyone smaller than method you will have put everyone smaller than the pivot on its left and everyone larger on its the pivot on its left and everyone larger on its right.right.
– This process of placing the pivot is called This process of placing the pivot is called partitioning the array partitioning the array
– Recursively call quicksort on the left and right Recursively call quicksort on the left and right halves.halves.
QuicksortQuicksort Here’s the basic idea:Here’s the basic idea: {6,8,1,7,3,5,2,4}{6,8,1,7,3,5,2,4} Let’s say we choose 6 as the pivot, we’d Let’s say we choose 6 as the pivot, we’d
like (something like) this when we are like (something like) this when we are done:done:– {1,3,5,2,4} 6 {8,7}{1,3,5,2,4} 6 {8,7}
Now we quicksort {1,3,5,2,4} and {8,7}Now we quicksort {1,3,5,2,4} and {8,7} The actual process of partitioning is a The actual process of partitioning is a
little trickier (to do well)…little trickier (to do well)…
Pivoting… efficientlyPivoting… efficiently Here is one way you can pivot an array Here is one way you can pivot an array
without (much) extra memory (in place)without (much) extra memory (in place)– Choose a pivot, in this case I’ll take the first Choose a pivot, in this case I’ll take the first
item in the array.item in the array.– Maintain three sections of the arrayMaintain three sections of the array
Less than the pivotLess than the pivot Greater than the pivotGreater than the pivot UnexploredUnexplored
– When you are finished processing the array, When you are finished processing the array, place the pivot between the < and > sections.place the pivot between the < and > sections.
Pivot Walk ThroughPivot Walk Through array = {6,8,1,7,3,5,2,4}array = {6,8,1,7,3,5,2,4} Variables:Variables:
– pivot = 6, low = 0, high= 7pivot = 6, low = 0, high= 7– Low and high represent the region which is Low and high represent the region which is
unexploredunexplored– All values less than low are smaller than the pivotAll values less than low are smaller than the pivot– All values greater than high are larger than the All values greater than high are larger than the
pivotpivot– When low == high, you are finished and now can When low == high, you are finished and now can
place the pivotplace the pivot
Pivot Walk ThroughPivot Walk Through Let’s take out the pivot and put it in a temporary Let’s take out the pivot and put it in a temporary
variablevariable Now we have a home to move the first item smaller Now we have a home to move the first item smaller
than the pivot to (left side)than the pivot to (left side) That means I’ll start looking on the larger sideThat means I’ll start looking on the larger side
– If the item really is bigger, great, I can leave it If the item really is bigger, great, I can leave it alone (proper place)alone (proper place)
– If it is smaller though, I can swap it to the free If it is smaller though, I can swap it to the free spacespace
Initially, Initially, – low = 0, high = 7, target = 7low = 0, high = 7, target = 7
8 1 7 3 5 2 46low target
high
Pivot Walk ThroughPivot Walk Through array[target] = 4array[target] = 4 This is smaller than 6 so I will swap it to the left/smaller This is smaller than 6 so I will swap it to the left/smaller
sideside– low = low + 1low = low + 1– low = 1, high = 7low = 1, high = 7
Now my empty space is on the right side, so I will be Now my empty space is on the right side, so I will be ready for the first time I find a larger value.ready for the first time I find a larger value.
That means I want continue working on the smaller sideThat means I want continue working on the smaller side– target = low = 1target = low = 1– If the value is still smaller, leave it alone (proper place)If the value is still smaller, leave it alone (proper place)– If it is larger, then I have a place to swap it toIf it is larger, then I have a place to swap it to
8 1 7 3 5 2 64low target
high
Pivot Walk ThroughPivot Walk Through array[target] = 8array[target] = 8 This is larger than 6 so I will swap it to the right/larger This is larger than 6 so I will swap it to the right/larger
sideside– high = high – 1high = high – 1– low = 1, high = 6low = 1, high = 6
Now my empty space is on the left side, so I will be Now my empty space is on the left side, so I will be ready for the next time I find a smaller value.ready for the next time I find a smaller value.
That means I want continue working on the larger sideThat means I want continue working on the larger side– target = high = 6 target = high = 6 – If the value is still larger, leave it alone (proper place)If the value is still larger, leave it alone (proper place)– If it is smaller, then I have a place to swap it toIf it is smaller, then I have a place to swap it to
4 1 7 3 5 2 68lowtarget
high
Pivot Walk ThroughPivot Walk Through array[target] = 2array[target] = 2 This is smaller than 6 so I will swap it to the left/smaller This is smaller than 6 so I will swap it to the left/smaller
sideside– low = low + 1low = low + 1– low = 2, high = 6low = 2, high = 6
Now my empty space is on the right side, so I will be Now my empty space is on the right side, so I will be ready for the next time I find a larger value.ready for the next time I find a larger value.
That means I want continue working on the smaller sideThat means I want continue working on the smaller side– target = low = 2target = low = 2– If the value is still smaller, leave it alone (proper place)If the value is still smaller, leave it alone (proper place)– If it is larger, then I have a place to swap it toIf it is larger, then I have a place to swap it to
4 1 7 3 5 682low
targethigh
Pivot Walk ThroughPivot Walk Through array[target] = 1array[target] = 1 This is smaller than 6 so I can leave it alone (one more This is smaller than 6 so I can leave it alone (one more
item resolved on the left side) item resolved on the left side) – low = low + 1low = low + 1– low = 3, high = 6low = 3, high = 6
My empty space is still on the right side, so I will be My empty space is still on the right side, so I will be ready for the next time I find a larger value.ready for the next time I find a larger value.
That means I want continue working on the smaller sideThat means I want continue working on the smaller side– target = low = 3target = low = 3– If the value is still smaller, leave it alone (proper place)If the value is still smaller, leave it alone (proper place)– If it is larger, then I have a place to swap it toIf it is larger, then I have a place to swap it to
4 2 1 7 3 5 68lowtarget
high
Pivot Walk ThroughPivot Walk Through array[target] = 7array[target] = 7 This is larger than 6 so I can swap it to the larger sideThis is larger than 6 so I can swap it to the larger side
– high = high – 1high = high – 1– low = 3, high = 5low = 3, high = 5
Now my empty space is on the left side, so I will be Now my empty space is on the left side, so I will be ready for the next time I find a smaller value.ready for the next time I find a smaller value.
That means I want continue working on the larger That means I want continue working on the larger sideside– target = high = 5target = high = 5– If the value is still larger, leave it alone (proper place)If the value is still larger, leave it alone (proper place)– If it is smaller, then I have a place to swap it toIf it is smaller, then I have a place to swap it to
4 2 1 3 5 687lowtarget
high
Pivot Walk ThroughPivot Walk Through array[target] = 5array[target] = 5 This is smaller than 6 so I can swap it to the smaller This is smaller than 6 so I can swap it to the smaller
sideside– low = low + 1low = low + 1– low = 4, high = 5low = 4, high = 5
Now my empty space is on the right side, so I will be Now my empty space is on the right side, so I will be ready for the next time I find a larger value.ready for the next time I find a larger value.
That means I want continue working on the smaller sideThat means I want continue working on the smaller side– target = low = 4target = low = 4– If the value is still smaller, leave it alone (proper place)If the value is still smaller, leave it alone (proper place)– If it is larger, then I have a place to swap it toIf it is larger, then I have a place to swap it to
4 2 1 3 7 685low target
high
Pivot Walk ThroughPivot Walk Through array[target] = 3array[target] = 3 This is smaller than 6 so I can leave it aloneThis is smaller than 6 so I can leave it alone
– low = low + 1low = low + 1– low = 5, high = 5low = 5, high = 5
Now that low == high I have finished the Now that low == high I have finished the partition.partition.
All that’s left to do is place the pivotAll that’s left to do is place the pivot
4 2 1 3 7 685lowtarget
high
Exercises Cont’Exercises Cont’ Implement the sorting algorithms forImplement the sorting algorithms for
– Bubble sortBubble sort– QuicksortQuicksort
Add them to the main program to put the Add them to the main program to put the algorithms against each otheralgorithms against each other
In your main program you should input the In your main program you should input the size of your test array to fill with random data size of your test array to fill with random data and then report out how long each sorting and then report out how long each sorting algorithm took to finish the same data set algorithm took to finish the same data set (give each a copy of the test data to sort) (give each a copy of the test data to sort)