title-img


Insertion Sort - Part 1

Sorting One common task for computers is to sort data. For example, people might want to see all their files on a computer sorted by size. Since sorting is a simple problem with many different possible solutions, it is often used to introduce the study of algorithms. Insertion Sort These challenges will cover Insertion Sort, a simple and intuitive sorting algorithm. We will first start with a nearly sorted list. Insert element into sorted list Given a sorted list with an unsorted number

View Solution →

Insertion Sort - Part 2

In Insertion Sort Part 1, you inserted one element into an array at its correct sorted position. Using the same approach repeatedly, can you sort an entire array? Guideline: You already can place an element into a sorted array. How can you use that code to build up a sorted array, one element at a time? Note that in the first step, when you consider an array with just the first element, it is already sorted since there's nothing to compare it to. In this challenge, print the array after ea

View Solution →

Correctness and the Loop Invariant

In the previous challenge, you wrote code to perform an Insertion Sort on an unsorted array. But how would you prove that the code is correct? I.e. how do you show that for any input your code will provide the right output? Loop Invariant In computer science, you could prove it formally with a loop invariant, where you state that a desired property is maintained in your loop. Such a proof is broken down into the following parts: Initialization: It is true (in a limited sense) before the l

View Solution →

Running Time of Algorithms

n a previous challenge you implemented the Insertion Sort algorithm. It is a simple sorting algorithm that works well with small or mostly sorted data. However, it takes a long time to sort large unsorted data. To see why, we will analyze its running time. Running Time of Algorithms The running time of an algorithm for a specific input depends on the number of operations executed. The greater the number of operations, the longer the running time of an algorithm. We usually want to know how

View Solution →

Quicksort 1 - Partition

The previous challenges covered Insertion Sort, which is a simple and intuitive sorting algorithm with a running time of . In these next few challenges, we're covering a divide-and-conquer algorithm called Quicksort (also known as Partition Sort). This challenge is a modified version of the algorithm that only addresses partitioning. It is implemented as follows: Step 1: Divide Choose some pivot element, , and partition your unsorted array, , into three smaller arrays: , , and , where each e

View Solution →