Python - Searching, Sorting, Hash - Practice

QTM 350: Data Science Computing

Davi Moreira

Introduction


This topic material is based on Professor Mike Gelbart Algorithms and Data Structures course. It was adapted for our purposes.

In [3]:
import numpy as np
import timeit
import sys
import matplotlib.pyplot as plt
import urllib.request
from collections import defaultdict, Counter

Exercise:

Is searching in Python faster when the element you're looking for is that the start of the array? Design and run an "experiment" comparing the runtime of Python's in operator for the caes when the element being sought is at the start vs. the end of an array. Does it seem to make a difference? Briefly discuss your results.

Answer:

Exercise:

Is sorting in Python faster when the array you're sorting is already sorted? Design and run an "experiment" comparing the runtime of numpy's .sort() for the caes when the the array is vs. isn't already sorted. Does it seem to make a difference?

Answer:

Exercise:

We saw in class that hash tables like Python's dict grow when they get too full. Make a plot of the size of a dictionary using sys.getsizeof vs. the number of elements. At what sizes does the dictionary seem to grow? Discuss your results.

Answer:

Exercise:

Now do the same experiment but with a list instead of a dict. Discuss your results.

Answer:

In [1]:
!jupyter nbconvert _02-py-searching-sorting-hash-practice.ipynb --to html --template classic --output 02-py-searching-sorting-hash-practice.html
[NbConvertApp] Converting notebook _02-py-searching-sorting-hash-practice.ipynb to html
[NbConvertApp] Writing 278495 bytes to 02-py-searching-sorting-hash-practice.html

Have fun!