Book Image

Python Data Structures and Algorithms

By : Benjamin Baka
Book Image

Python Data Structures and Algorithms

By: Benjamin Baka

Overview of this book

Data structures allow you to organize data in a particular way efficiently. They are critical to any problem, provide a complete solution, and act like reusable code. In this book, you will learn the essential Python data structures and the most common algorithms. With this easy-to-read book, you will be able to understand the power of linked lists, double linked lists, and circular linked lists. You will be able to create complex data structures such as graphs, stacks and queues. We will explore the application of binary searches and binary search trees. You will learn the common techniques and structures used in tasks such as preprocessing, modeling, and transforming data. We will also discuss how to organize your code in a manageable, consistent, and extendable way. The book will explore in detail sorting algorithms such as bubble sort, selection sort, insertion sort, and merge sort. By the end of the book, you will learn how to build components that are easy to understand, debug, and use in different applications.
Table of Contents (20 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
5
Stacks and Queues
7
Hashing and Symbol Tables

Deterministic selection


The worst-case performance of a randomized selection algorithm is O(n2). It is possible to improve on a section of the randomized selection algorithm to obtain a worst-case performance of O(n). This kind of algorithm is called deterministic selection.

The general approach to the deterministic algorithm is listed here:

  1. Select a pivot:
    1. Split a list of unordered items into groups of five elements each.
    2. Sort and find the median of all the groups.
    3. Repeat step 1 and step 2 recursively to obtain the true median of the list.
  2. Use the true median to partition the list of unordered items.
  3. Recurse into the part of the partitioned list that may contain the ith-smallest element.

Pivot selection

Previously, in the random selection algorithm, we selected the first element as the pivot. We shall replace that step with a sequence of steps that enables us to obtain the true or approximate median. This will improve the partitioning of the list about the pivot:

    def partition(unsorted_array...