ADS Practical 1_removed
ADS Practical 1_removed
Details of Quiz:
Q.
Question Correct Answer Given Answer
No.
Total number of swapping required to sort the
1 numbers 8, 22, 7, 9, 31, 5, 13 in ascending order c) 10 c) 10
using bubble sort is
Average case time complexity of Binary
2 Search is _______, where n is the number of b) O(log n) b) O(log n)
elements in the array.
d) When the list has d) When the list has
only a few elements only a few elements
and When and When
3 Where is linear searching used?
performing a single performing a single
search in an search in an
unordered list unordered list
a) Shell sort allows a) Shell sort allows
an exchange of far an exchange of far
Why is Shell sort called as a generalization of items whereas items whereas
4
Insertion sort? insertion sort moves insertion sort moves
elements by one elements by one
position position
The ______ is a simple sorting algorithm which
finds the smallest element in the array and
5 b) Selection sort b) Selection sort
exchanges it with the element in the first
position.
Suppose you have the following list of numbers
to sort: - [15, 5, 4, 18, 12, 19, 14, 10, 8, c) [4, 5, 15, 18, 12, c) [4, 5, 15, 18, 12,
6
20] which list represents the partially sorted list 19, 14, 10, 8, 20] 19, 14, 10, 8, 20]
after three complete passes of insertion sort?
Experiment Evaluation:
Sr. No. Parameters Marks Obtained Out of
Technical Understanding (Assessment based on Q and A
1 6 6
through online quiz)
2 Neatness/Presentation 2
3 Punctuality 2
Date of Execution (DOE) Total Marks Obtained 10
Date of Assessment (DOA) Signature of Teacher
FINOLEX ACADEMY OF MANAGEMENT AND TECHNOLOGY, RATNAGIRI FY MCA - SEM I (CBCGS)
(MCAL11) Advanced Data Structures Lab
Title – Sorting and Searching Techniques
Searching: A search algorithm is the step-by-step procedure used to locate specific data among a collection
of data.
It is considered a fundamental procedure in computing. All search algorithms make use of a search key to
proceed with the procedure.
Search algorithms are expected to return a success or failure status, usually denoted by Boolean true/false.
1. Sequential Search: -
A linear search algorithm is considered the most basic of all search algorithms.
In this type of search, a sequential search is made over all items one by one.
Every item is checked and if a match is found then that particular item is returned, otherwise the search
continues till the end of the data collection.
2. Binary Search: -
If the data is sorted, a binary search may be done.
Variables ‘bottom’ and ‘top’ keep track of the lower bound and upper bound of the array, respectively.
We begin by examining the middle element of the array.
If the key we are searching for is less than the middle element, then it must reside in the top half of the array.
Thus, we set ‘top’ to (MAX - 1). This restricts our next iteration through the loop to the top half of the array.
In this way, each iteration halves the size of the array to be searched.
This is a powerful method. Given an array of 1023 elements, we can narrow the search to 511 items in one
comparison. Another comparison, and we’re looking at only 255 elements. In fact, we can search the entire
array in only 10 comparisons.
Binary search is a fast search algorithm with run-time complexity of Ο(log n).
Algorithm binary_search(list[],size, key)
Input : Array list of ‘size’ elements and key to be searched.
Output : Message regarding searched key
1. set found_flag false
2. set top = 0
3. set bottom size-1
4. while top <= bottom && found_flag = false do
● Sorting: an operation that segregates items into groups according to specified criterion. (Sorted = ordered
based on a particular way.)
● Sorting is the process of arranging items in some sequence and/or in different sets.
● Stable sort means elements with the same key inherit their relative order from the input sequence.
Algorithm Complexity
● Bubble sort All case efficiency is O(N2).
● Enhancement - The variable ‘e’ is used to count the number of exchanges performed on any pass.
Algorithm Bubblesort(list[], size)
Input : Array list of n Unsorted elements.
Output : Sorted Array.
1. for hold ← 0, l, ..... size – 1 do
2. for walker ← 0, l, ..... (size–hold-1) do
3. if (list [walker] > list[walker + 1]) then
4. set list [walker] ↔ list [walker + 1] //Exchanging values.
5. end if
6. end for
7. print the hold variable along with list //optional
8. end for
end Bubblesort
Insertion Sort
● Consider one element at a time from input list, and inserting it in its proper place among already sorted
elements.
● The key is temporarily stored in a element and then its appropriate place is found for insertion.
Algorithm Complexity
P - 01 ADS Lab: Sorting and Searching Techniques Page No. 2
FINOLEX ACADEMY OF MANAGEMENT AND TECHNOLOGY, RATNAGIRI FY MCA - SEM I (CBCGS)
● Insertion sort Worst and Average case efficiency is O(N2) whereas best case is O(n).
2. Selection Sort
● Find the smallest element in the array and exchange it with the element in the first position.
● Then, find the second smallest element and exchange it with the element in the second position, and so on
until the entire array is sorted.
● In ith pass it selects the lowest value key & exchanges it with list[i]. Process continues till ‘n’ passes & array
get sorted.
Algorithm Complexity
● Selection sort All case efficiency is O(N2).
3. Shell Sort
● Donald Shell discovered the Shell sort and thence it is known as Shell Sort. The algorithms for shell sort can
be defined in two steps –
Step 1: divide the original list into smaller lists.
4. Algorithm Complexity
● shell sort worst and average case efficiency vary from O(N1.25) to O(N2) and best case is O(nlogn)
● It has been empirically found that most of the shell sort versions perform better than simple sorting
algorithms.
Exercise:
1. Write a java program to implement a sequential search algorithm for searching for a key
2. Write a java program to implement a recursive binary search algorithm for searching for a key.
3. Write a java program to implement a Bubble sort algorithm to sort 15 numbers.
4. Write a java program to sort an array of 10 numbers using Insertion Sort.
5. Write a java implement a Selection Sort algorithm for sorting 10 numbers.
6. Write a Java program to sort an array of 15 numbers using Shell Sort. Take input from num.txt file.