Design and Analysis of Algorithms · Unit 2 · 4 hrs
Iterative Algorithms
Exam-focused notes for Iterative Algorithms (Design and Analysis of Algorithms, CSC325): what the TU syllabus asks and how it has actually been tested, with 7 solved past questions from this unit.
What this unit covers
- Basic Algorithms: Algorithm for GCD, Fibonacci Number and analysis of their time and space complexity
- Searching Algorithms: Sequential Search and its analysis
- Sorting Algorithms: Bubble, Selection, and Insertion Sort and their Analysis
Sorting Algorithms
Find the best and worst case for Bubble sort. [5]
Bubble sort is the simplest sorting algorithm that works by repeatedly swapping adjacent elements if they are in the wrong order. A list with n elements requires n-1 passes for sorting. In each pass, element a[i] is compared with a[i+1] and swapped if they ...
Full solved answer →Write down algorithm of insertion sort and analyze its time and space complexity. [5]
Insertion Sort works by picking each element and inserting it into its correct position among the already-sorted elements to its left. Steps: 1. Start with the array A of n elements. 2. Pick the element at position i (starting from i = 1). 3. Store it in a ...
Full solved answer →Write the algorithm for bubble sort and explain its time complexity. [5]
Idea: Repeatedly compare adjacent elements and swap them if they are in the wrong order. After each pass, the largest unsorted element "bubbles up" to its correct position. 1. Start from the beginning of the array. 2. Compare adjacent elements A[j] and A[j+...
Full solved answer →Write the algorithm for selection sort and explain its time and space complexity. [5]
Selection Sort is a simple comparison-based sorting algorithm that works by repeatedly selecting the minimum element from the unsorted portion of the array and placing it at the correct position. --- 1. Start. 2. Let array A have n elements (index 0 to n-1)...
Full solved answer →Searching Algorithms
State the time and space complexity for sequential search. Write the rules for master theorem for finding asymptotic bounds. [5]
--- Sequential search (also called linear search) compares the search element with each element in the list one by one from the beginning. Case Condition Complexity ----------------------------- Best Case Element found at the first position O(1) Average Cas...
Full solved answer →Basic Algorithms
Write an algorithm to find the $n^{th}$ fibonacci number with its time and space complexity. [5]
The Fibonacci sequence is defined as: - F(0) = 0 - F(1) = 1 - F(n) = F(n-1) + F(n-2) for n = 2 --- --- Iteration temp = first + second first second i --------------------------------------------------- Initial - 0 1 2 1 0 + 1 = 1 1 1 3 2 1 + 1 = 2 1 2 4 3 1...
Full solved answer →Explain the iterative algorithm to find the GCD of given two numbers and analyze its complexity. [5]
The GCD (Greatest Common Divisor) of two numbers is found using the iterative Euclidean algorithm based on the property that GCD(m, n) = GCD(n, m mod n). Steps: Pseudocode: --- Find GCD(48, 18): Step m n r = m mod n --------------------------- 1 48 18 48 mo...
Full solved answer →Make Unit 2 stick
Practice CSC325 with flashcards & quizzes