Skip to Content
Python Data Structures and Algorithms
book

Python Data Structures and Algorithms

by Benjamin Baka
May 2017
Intermediate to advanced
310 pages
8h 5m
English
Packt Publishing
Content preview from Python Data Structures and Algorithms

Partitioning step

Now that we have obtained the approximate median, the get_index_of_nearest_median function takes the bounds of the list indicated by the first and last parameters:

    def get_index_of_nearest_median(array_list, first, second, median):         if first == second:             return first         else:             return first + array_list[first:second].index(median) 

Once again, we only return the first index if there is only one element in the list. The arraylist[first:second] returns an array with index 0 up to the size of the list -1. When we find the index of the median, we lose the portion in the list where it occurs because of the new range indexing the [first:second] code returns. Therefore, we must add whatever index is returned by arraylist[first:second] ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Data Structures and Algorithms in Python

Data Structures and Algorithms in Python

Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Hands-On Data Structures and Algorithms with Python - Second Edition

Hands-On Data Structures and Algorithms with Python - Second Edition

Dr. Basant Agarwal, Benjamin Baka, David Julian
Data Structures & Algorithms in Python

Data Structures & Algorithms in Python

John Canning, Alan Broder, Robert Lafore

Publisher Resources

ISBN: 9781786467355Supplemental Content