Merge Sort, Quicksort and Divide-n-Conquer Algorithms in Python
Data Structures and Algorithms in Python is beginner-friendly introduction to common data structures (linked lists, stacks, queues, graphs) and algorithms (search, sorting, recursion, dynamic programming) in Python, designed to help you prepare for coding interviews and assessments.
Earn a verified certificate of accomplishment for this course by signing up here: http://pythondsa.com . Ask questions, get help & participate in discussions on the course community forum.
Prerequisites
This course assumes very little background in programming and mathematics, and you can learn the required concepts here:
- Basic programming with Python (variables, data types, loops, functions etc.)
- Some high school mathematics (polynomials, vectors, matrices and probability)
- No prior knowledge of data structures or algorithms is required
We'll cover any additional mathematical and theoretical concepts we need as we go along.
How to Run the Code
The best way to learn the material is to execute the code and experiment with it yourself. This tutorial is an executable Jupyter notebook. You can run this tutorial and experiment with the code examples in a couple of ways: using free online resources (recommended) or on your computer.
Option 1: Running using free online resources (1-click, recommended)
The easiest way to start executing the code is to click the Run button at the top of this page and select Run on Binder. You can also select "Run on Colab" or "Run on Kaggle", but you'll need to create an account on Google Colab or Kaggle to use these platforms.
Option 2: Running on your computer locally
To run the code on your computer locally, you'll need to set up Python, download the notebook and install the required libraries. We recommend using the Conda distribution of Python. Click the Run button at the top of this page, select the Run Locally option, and follow the instructions.
Problem
In this notebook, we'll focus on solving the following problem:
QUESTION 1: You're working on a new feature on Jovian called "Top Notebooks of the Week". Write a function to sort a list of notebooks in decreasing order of likes. Keep in mind that up to millions of notebooks can be created every week, so your function needs to be as efficient as possible.
The problem of sorting a list of objects comes up over and over in computer science and software development, and it's important to understand common approaches for sorting, and the trade-offs they offer. Before we solve the above problem, we'll solve a simplified version of the problem:
QUESTION 2: Write a program to sort a list of numbers.
"Sorting" usually refers to "sorting in ascending order", unless specified otherwise.
The Method
Here's a systematic strategy we'll apply for solving problems:
- State the problem clearly. Identify the input & output formats.
- Come up with some example inputs & outputs. Try to cover all edge cases.
- Come up with a correct solution for the problem. State it in plain English.
- Implement the solution and test it using example inputs. Fix bugs, if any.
- Analyze the algorithm's complexity and identify inefficiencies, if any.
- Apply the right technique to overcome the inefficiency. Repeat steps 3 to 6.