- Type
- Midterm past paper
- Course
- CS502 Fundamentals of Algorithms
- Size
- PDF 710 KB
- Updated
- 23 Oct 2026
You get 3 free files a day on the website. Add the Chrome extensionSign in with the extension for 30 a day.
Preview text
Text from pages 1, 2, 3 of 10. Download the file for all 10 pages.
The ancient Roman politicians understood an important principle of good algorithm design (although they were probably not thinking about algorithms at the time). You divide your enemies (by getting them to distrust each other) and then conquer them piece by piece. This is called divide-and-conquer. In algorithm design, the idea is to take a problem on a large input, break the input into smaller pieces, solve the problem on each of the small pieces, and then combine the piecewise solutions into a global solution. But once you have broken the problem into pieces, how do you solve these pieces? The answer is to apply divide-and-conquer to them, thus further breaking them down. The process ends when you are left with such tiny pieces remaining (e.g. one or two items) that it is trivial to solve them. Summarizing, the main elements to a divide-and-conquer solution are
Divide: the problem into a small number of pieces Conquer: solve each piece by applying divide and conquer to it recursively Combine: the pieces together into a global solution.
Selection Problem
(Page#34)
Suppose we are given a set of n numbers. Define the rank of an element to be one plus the number of elements that are smaller. Thus, the rank of an element is its final position if the set is sorted. The minimum is of rank 1 and the maximum is of rank n.
Consider the set: {5, 7, 2, 10, 8, 15, 21, 37, 41}. The rank of each number is its position in the sorted order.
position Number
Sieve Technique
(Page#34)
The reason for introducing this algorithm is that it illustrates a very important special case of divide-and-conquer, which I call the sieve technique. We think of divide-and-conquer as breaking the problem into a small number of smaller subproblems, which are then solved recursively. The sieve technique is a special case, where the number of subproblems is just 1.
The sieve technique works in phases as follows. It applies to problems where we are interested in finding a single item from a larger set of n items.
Slow Sorting Algorithms
(Page#39)
There are a number of well-known slow O(n2) sorting algorithms. These include the following:
Bubble sort: Scan the array. Whenever two consecutive items are found that are out of order, swap them. Repeat until all consecutive items are in order.
Insertion sort: Assume that A[1..i − 1] have already been sorted. Insert A[i] into its proper position in this sub array. Create this position by shifting all larger elements to the right.
Selection sort: Assume that A[1..i − 1] contain the i − 1 smallest elements in sorted order. Find the smallest element in A[i..n] Swap it with A[i].
Heaps (Page#40)
A heap is a left-complete binary tree that conforms to the heap order. The heap order property: in a (min) heap, for every node X, the key in the parent is smaller than or equal to the key in X. In other words, the parent node has key smaller than or equal to both of its children nodes. Similarly, in a max heap, the parent has a key larger than or equal both of its children Thus the smallest key is in the root in a min heap; in the max heap, the largest is in the root.
Quicksort (Page#46)
Our next sorting algorithm is Quicksort. It is one of the fastest sorting algorithms known and is the method of choice in most sorting libraries. Quicksort is based on the divide and conquer strategy.
Here is the algorithm:
In-place, Stable Sorting
(Page#46)
An in-place sorting algorithm is one that uses no additional array for storage. A sorting algorithm is stable if duplicate elements remain in the same relative position after sorting.
Related CS502 files
All CS502 files →- CS502 Midterm Grand Quiz Midterm PDF 1.4 MB
- CS502 Midterm Short Notes Midterm PDF 8.4 MB
- CS502 Midterm Solved Subjective with References by Moaaz Midterm DOCX 163 KB
- CS502 Midterm Solved MCQs with References Midterm PDF 268 KB
- CS502 Highlighted Handouts by VUBuddy Highlighted PDF 1.5 MB
- CS502 Quiz Quiz PDF 1.3 MB
Get more out of VUBuddy, for free
- Unlimited downloads
- The explanation behind every MCQ answer
- Study offline on slow data
- 30 downloads a day instead of 3
- Sign in and buy Pro Semester here
One Pro Semester membership works in both the extension and the app.