DESIGN AND ANAYSIS OF ALGORITHMS
22CS2205R
Course Description (Description about the subject):
This course will enable the student to write the algorithms in a descriptive manner with a clear
analysis. It helps the students to measure the time and space complexity of the algorithms for
methods like Divide and Conquer, Greedy method, Dynamic Programming, Backtracking and
Branch and Bound and are elaborated with generic and real world examples.
Session: 2
Design Techniques to solve real world problems
Aim:
To introduce students to the basic concepts of Sorting
Familiarize students with solving real world problems using Sorting Techniques
Instructional Objectives:
The Basic objective is to design fast, efficient and effective solution to a problem domain. Some
problems are easy to solve and some are hard. Quite cleverness is required to design solution
with fast and better approach. Designing new system need a new technology and background of
the new technology is the enhancement of existing algorithm. The study of algorithm is to design
efficient algorithm not only limited in reducing cost and time but to enhance scalability,
reliability and availability.
Learning Outcomes:
At the end of this session, students are expected to solve any real world problems
Module Description:
The objective of this course is to study paradigms and approaches used to analyze and design
algorithms and to appreciate the impact of algorithm design in practice.
Session Introduction:
Hacker Rank helps students to solve real-world problems and make them analyze the algorithms
The purpose of HackerRank is to help developers improve their problem-solving abilities and
coding proficiency through practice. It offers a vast library of coding problems, often categorized
by difficulty level, which developers can solve in different programming languages.
Session description:
Sorting is the process of arranging items or elements in a particular order or sequence based on
certain criteria. It is a fundamental operation in computer science and is used in various
applications to organize and retrieve data efficiently.
1. Merge Sort: Merge sort is a divide-and-conquer algorithm. It divides the unsorted list into
smaller sublists, sorts those sublists recursively, and then merges them back together to
obtain the final sorted list.
2. Quick Sort: Quick sort also uses a divide-and-conquer approach. It selects a pivot
element and partitions the list into two sublists, one with elements smaller than the pivot
and one with elements larger than the pivot. It then recursively sorts the sublists.
Activities/ Case studies/ Important facts related to the session:
Whenever George asks Lily to hang out, she's busy doing homework. George wants to help her
finish it faster, but he's in over his head! Can you help George understand Lily's homework so
she can hang out with him?
Consider an array of distinct integers, George can swap any two elements of the array any
number of times. An array is beautiful if the sum of among is minimal.
Given the array , determine and return the minimum number of swaps that should be performed
in order to make the array beautiful.
.Examples and contemporary extracts of articles or practices to convey the idea of the
session:
Eg:
[6, 5, 3, 1, 8, 7, 2, 4]
1.Select a pivot element: We choose the pivot element as the last element in the array, which is
2.Partitioning: Rearrange the array so that all elements smaller than the pivot are on the left
side, and all elements greater than the pivot are on the right side.
Starting from the leftmost element, we compare each element with the pivot (4) and swap
them if they are smaller. After the partitioning step, the array might look like this:
[3, 1, 2, 4, 8, 7, 6, 5] At this point, the pivot (4) is in its final sorted position.
3. Recursive step: Apply the above two steps recursively to the subarrays on the left and
right sides of the pivot until the entire array is sorted.
Applying Quick Sort to the left subarray [3, 1, 2]:
a. Select the pivot element as the last element (2).
b. Partition the array: [3, 1, 2] -> [1, 2, 3] (since 1 is smaller than the pivot and 3 is
larger)
c. The pivot (2) is in its final sorted position.
Applying Quick Sort to the right subarray [8, 7, 6, 5]:
d. Select the pivot element as the last element (5).
e. Partition the array: [8, 7, 6, 5] -> [5, 7, 6, 8] (since 5 is smaller than the pivot and
7, 6, 8 are larger)
f. The pivot (5) is in its final sorted position.
4. The array is now sorted: Combine the sorted subarrays to obtain the final sorted array.
[1, 2, 3, 4, 5, 6, 7, 8]
And that's the sorted array using the Quick Sort algorithm. The steps are repeated recursively
until the entire array is sorted, with each recursive step partitioning the array around a chosen
pivot element.
Table Numbering: NA
Figures with captions: NA
Self Assessment Questions:
Define Sorting
Give an example of a real-world scenario where sorting algorithms are used
How does Quick Sort choose a pivot element, and what impact does the choice of pivot
have on the algorithm's performance?
Summary:
sorting is a fundamental operation in computer science that involves arranging elements or items
in a particular order. There are various sorting algorithms available, each with its own
characteristics, and the choice of algorithm depends on factors such as input size and desired
efficiency. Sorting is used in numerous applications and plays a crucial role in efficient data
management and retrieval.
Terminal Questions:
Describe the partitioning step in the Quick Sort algorithm
Why is it important to analyze the worst-case time complexity of a sorting algorithm
Case Study: NA
Answer Key:NA
Glossary:
1.Sorting: The process of arranging elements or items in a specific order based on
certain criteria.
2. Sorting Algorithm: A set of well-defined steps or procedures used to sort a collection
of elements.
3. Pivot: A chosen element used as a reference point in partitioning-based sorting
algorithms, such as Quick Sort.
4. Partitioning: The process of rearranging elements in a list or array such that elements
smaller than a chosen pivot are placed before it, and elements greater than or equal to
the pivot are placed after it.
References of books, sites, links:
Reference Books:
1. Ellis Horowitz, SartajSahni and Sanguthevar Rajasekaran, “Fundamentals of Computer
Algorithms”, 2nd Edition, University Press, 2008.
2. Cormen, Leizerson&Rivest, “Introduction to algorithms”, 3rd Edition, Prentice-Hall, 2002.
3. Jon Kleinberg and Eva Tardos, “Algorithm Design”, Pearson Education, 2006.
Sites and Web links:
1)www.hackerrank.com
Keywords: Best case Time complexity, Average-case complexity, Average-case complexity