Lesson 6: The Sorted List

Topics

In this lesson, we are going to introduce the Sorted_List ADT. The topic includes:

  1. Sorted_List ADT
  2. Use of the Sorted_List ADT
  3. Using Python lists to implement Sorted_List ADT
  4. The Binary Search

Required Tasks

  1. The Sorted_List ADT
  2. Using the Sorted_List
  3. Implementation of the Sorted_List ADT

Required Tasks

  1. Complete the Sorted_List tasks of Lab 3.
  2. Complete and submit Assignment 4.

Learning Outcomes

By the end of this lesson, you should be able to

Introduction

In this lesson, we are going to learn in more detail about the Sorted_List. We talk about how the methods of this ADT are similar to and differ from that of the List. We will discuss Array data structures and explain how we can implement the Sorted_List ADT using the Python lists.

The Sorted_List ADT

A Sorted_List is an ADT that contains a collection of values in sorted order. The values are ordered by some inherent characteristics of the values, so the values themselves determine where they are stored in the list. The values in a Sorted_List must always be sorted.

Sorted_List ADT Methods

Many of the Sorted_List ADT methods and operators are the same as the List ADT methods and operators such as initialize(), len(), remove(key), find(key), is_empty(), count(key), min(), max(), clear(), index(key), l[i], and in. However, insert is defined differently:

insert(value)
adds value to the Sorted_List at the appropriate (sorted) position. The new value must be added after any equivalent values.

A new element should be inserted so that the Sorted_List remains sorted. That's why the Sorted_List insert lacks the index parameter of the List insert.

Array-Based Sorted_List

Part of the point of a Sorted_List is to make use of the fact that its contents are sorted in its method algorithms. Thus, even the methods that return the same result as the equivalent method in the List ADT may be implemented with a significantly different algorithm, and these algorithms may have a different efficiency than that of its List counterpart.

Sorted Data Algorithms

We provided the code for the count method in the array-based List ADT:

List count
    
    def count(self, key):
        n = len(self._values)
        i = 0
        number = 0

        while i < n:
            if self._values[i] == key:
                number += 1
            i += 1
        return number

  

We can rewrite this code for the Sorted_List to take advantage of the fact that values are in order:

Sorted_List count
    
    def count(self, key):
        n = len(self._values)
        i = self._binary_search(key)

        if i == -1:
            number = 0
        else:
            # Because the list is sorted, all values with the same
            # key are lumped together.
            number = 1
            i += 1

            while i < n and self._values[i] == key:
                i += 1
                number += 1
        return number

  

The line:
i = self._binary_search(key)
is discussed in the next section. Its purpose is to find the first occurrence of key, and if it exists at all, count the rest of the values that are equivalent to key.

Note that although potentially this algorithm has to do less work than its List version (not every value in the list has to be compared against key like in the List version), it is still \(O(n)\). Why? Imagine a Sorted_List where all the values are the same. In this worst case, every value would have to be compared against key in any case.

Summary

In this lesson, we introduced the Sorted_List ADT and discussed different approaches to implement the Sorted_ListList ADT using arrays. We discussed how a Sorted_List can be implemented using Python lists. We looked at the Binary Search in detail.

References

  1. CS1027b: Computer Science Fundamentals II, Department of Computer Science at Western University, Notes on the List ADT.
  2. Design and Analysis of Data Structures, Niema Moshiri and Liz Izhikevich, 2018.