Searching & Sorting
Searching and Sorting
• Searching and Sorting are basic operations and many other
algorithms make use of them.
Searching Algorithms
Linear Search
• This was already covered in the practical exercise done in the lab
• What are the worst (O) and best case (Ω) running times?
Binary Search
• This performs better than linear search.
• Binary search requires that the list be ordered, unlike linear
search in which the ordering doesn’t matter.
• The algorithm works by repeatedly splitting a list of ordered
elements in two and searching either in the lower half or the upper
half.
Binary Search Illustration
int binarySearch (int key, int array[], int arraySize)
// key is the value being searched for
// array[] contains the list of elements
// function returns -1 if key is not found else it returns the
index at which the key occurs
minIndex = 0 // the index of the first element
maxIndex = size – 1 // the index of the last element
Binary Search: using iteration
while true:
if maxIndex < minIndex:
return -1 // element not found
else:
midPoint = (minIndex + maxIndex) / 2
if array[midPoint] < key:
minIndex = midPoint + 1 // focus on the right half
else if array[midPoint] > key:
maxIndex = midPoint - 1 // focus on the left half
else:
return midPoint // return index at which element occurs
Binary Search: using iteration (pseudocode)
Binary Search
• Worst case running time: O(log n)
– NB: the list is being cut in half with every iteration.
• Best case running time: Ω(1)
• Later on we will look at a recursive implementation.
Sorting Algorithms
Bubble Sort
• Starting from the beginning of the list, compare every adjacent
pair
• swap their position if they are not in the right order (i.e. the latter
one is smaller than the former one)
• Repeat the above two steps until there are no more swaps to be
done
bubbleSort(values, n)
for i = 0 to n – 1
for j = 0 to n – 1
if (values[j] > values[j + 1])
swap values[j] and values[j+ 1]
end if
end for
end for
Effort to speed up bubbleSort
for i = 0 to n – 1
swapped = false
for j = 0 to n – 1 - i
if (values[j] > values[j + 1])
swap values[j] and values[j+ 1]
Swapped = true
end if
end for
if (not swapped)
break
end
end for
Bubble Sort
• Worst-case running time: n2
– Even with effort to speed up! Why?
• Best-case running time: ?
Selection Sort
• Iterate through the numbers from first to last and keep track of the
smallest number
• After each iteration, swap that smallest number with the first
number that hasn’t been put in its correct place yet
• On each iteration, start one position after the starting position of
the previous iteration so as not to swap out a number already in
its correct position
Selection Sort
Selection Sort
• Worst-case running time: n2
– How?
• Best-case running time: ?
Insertion Sort
Insertion Sort
• 4 2 6 1 3 7 5
• 4 2 6 1 3 7 5
• 2 4 6 1 3 7 5
• 2 4 6 1 3 7 5
• 1 2 4 6 3 7 5
• 1 2 3 4 6 7 5
• 1 2 3 4 5 6 7
• 1 2 3 4 5 6 7
Insertion Sort
Insertion Sort
• Worst-case running time: n2
• Best-case running time: ?
Big O Notation
Merge Sort
• Much faster than bubble sort, selection sort, and insertion sort.
Further reading
• Chapter 11, Goodrich M et al., “Data Structures and Algorithms in C++”, 2nd Edition.
• http://www.tutorialspoint.com/data_structures_algorithms/bubble_sort_algorithm.htm
• http://www.tutorialspoint.com/data_structures_algorithms/selection_sort_algorithm.htm
• http://www.tutorialspoint.com/data_structures_algorithms/merge_sort_algorithm.htm