searching & sorting · searching and sorting •searching and sorting are basic operations and...

24
Searching & Sorting

Upload: others

Post on 16-Oct-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

Searching & Sorting

Page 2: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

Searching and Sorting

• Searching and Sorting are basic operations and many other

algorithms make use of them.

Page 3: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

Searching Algorithms

Page 4: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

Linear Search

• This was already covered in the practical exercise done in the lab

• What are the worst (O) and best case (Ω) running times?

Page 5: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

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.

Page 6: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

Binary Search Illustration

Page 7: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

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

Page 8: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

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)

Page 9: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

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.

Page 10: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

Sorting Algorithms

Page 11: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

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

Page 12: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

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

Page 13: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

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

Page 14: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

Bubble Sort

• Worst-case running time: n2

– Even with effort to speed up! Why?

• Best-case running time: ?

Page 15: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

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

Page 16: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

Selection Sort

Page 17: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

Selection Sort

• Worst-case running time: n2

– How?

• Best-case running time: ?

Page 18: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

Insertion Sort

Page 19: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

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

Page 20: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

Insertion Sort

Page 21: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

Insertion Sort

• Worst-case running time: n2

• Best-case running time: ?

Page 22: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

Big O Notation

Page 23: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

Merge Sort

• Much faster than bubble sort, selection sort, and insertion sort.

Page 24: Searching & Sorting · Searching and Sorting •Searching and Sorting are basic operations and many other algorithms make use of them. Searching Algorithms. Linear Search •This

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