0% found this document useful (0 votes)
20 views

ADS Practical 1_removed

The document outlines the Advanced Data Structures Lab course at Finolex Academy, focusing on sorting and searching techniques. It includes a quiz evaluation for a student, detailing their performance and understanding of algorithms like sequential search, binary search, bubble sort, insertion sort, selection sort, and shell sort. Additionally, it provides algorithms and complexities for each sorting method, along with exercises for practical implementation in Java.

Uploaded by

jiteshkadam2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

ADS Practical 1_removed

The document outlines the Advanced Data Structures Lab course at Finolex Academy, focusing on sorting and searching techniques. It includes a quiz evaluation for a student, detailing their performance and understanding of algorithms like sequential search, binary search, bubble sort, insertion sort, selection sort, and shell sort. Additionally, it provides algorithms and complexities for each sorting method, along with exercises for practical implementation in Java.

Uploaded by

jiteshkadam2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

HOPE Foundation’s

Finolex Academy Of Management And Technology, Ratnagiri


Department of MCA
Course Code: MCAL11 Course Name: Advanced Data Structures Lab
Class: FYMCA - Div - A Semester: I CBCGS Academic Year: 2024-25
Roll No: 35 Name of Student: Jitesh Sandesh Kadam
Experiment No: 01 Quiz Score: 6.00 / 6
Name of Experiment: Sorting and Searching Techniques
Lab Objective applicable: LOB1: Understand concepts of searching and sorting algorithms.
Lab Outcome applicable: LO1: Apply searching and sorting algorithms.

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.

Algorithm sequential_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. for i  0, ..... size – 1 step 1 do
3. if key = list[i] then
4. set found_flag  true
5. end if
6. end for
7. if found_flag = false then
8. print “Key not found in the given list.”
9. else
10. print “ Key found at “ i+1 “ position in the given list.”
11. end if
end sequential_search

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

P - 01 ADS Lab: Sorting and Searching Techniques Page No. 1


FINOLEX ACADEMY OF MANAGEMENT AND TECHNOLOGY, RATNAGIRI FY MCA - SEM I (CBCGS)
5. set mid  (top+bottom)/2
6. If key>list[mid] then
7. set top  mid+1
8. else if key < list[mid] then
9. set bottom  mid-1
10. else
11. set found_flag  true
12. end if
13. end while
14. if found_flag = true then
15. print "Key found at "<<mid+1<<" position."
16. else
17. print "Element not found."
18. end if
end binary_search

● 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.

1. Bubble sort (Sinking Sort)


● It works by repeatedly stepping through d list to be sorted, comparing each pair of adjacent items & swapping
them if they are in the wrong order i.e “Bubble” the largest value to the end using pair-wise comparisons &
swapping.
● One problem of bubble sort is that its running time badly depends on the initial order of the elements.
● Big elements (rabbits) go up fast, while small ones (turtles) go down very slow.

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).

Algorithm Insertionsort(list[], size)


Input : Array list of n Unsorted elements.
Output : Sorted Array.
1. for key ← l, ..... size – 1 do
2. walker=key-1
3. hold=list[key] //holding temporary a key/element
4. while walker >= 0 and hold<list[walker] do //finding place for insertion
5. list[walker+1] ← list[walker]
6. set walker ← walker-1
7. end while
8. list[walker+1] ← hold //Inserting at appropriate position
9. end for
end Insertionsort

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).

Algorithm Selectionsort(list[], size)


Input : Array list of n Unsorted elements.
Output : Sorted Array.
1. for hold ← 0, ..... size – 1 do
2. pos=hold
3. for walker ← hold+1, ..... size – 1 do
4. if list[walker] < list[pos]
5. set pos ← walker
6. end if
7. end for
8. if hold ≠ pos then
9. list[pos] ↔ list[hold]
10. end if
11. end for
end Selectionsort

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.

P - 01 ADS Lab: Sorting and Searching Techniques Page No. 3


FINOLEX ACADEMY OF MANAGEMENT AND TECHNOLOGY, RATNAGIRI FY MCA - SEM I (CBCGS)
Step 2: sort individual sub lists using any known sorting algorithm (generally insertion sort is used).
● For dividing the original list into smaller lists, we choose a value K, which is known as increment.
● Based on the value of K, we split the list into K sub lists. Then in every pass we reduce the value of K and we
will reduce the number of sub lists.
● Eventually, when K will be set to 1, we will have only one sub list. Hence this is the termination condition for
shell sort algorithm. As in every iteration we are decreasing the value of the increment (K) the algorithm is
also known as “diminishing increment sort”.
● By this stage the elements are sufficiently sorted that the running time of the final stage is much closer to
O(N)
● Donald Shell has suggested Ht = N/2, Hk= hk+1 /2 Which give the increment series as: N/2, N/4, …….1.
● Hibbard suggested the sequence of increment as 1, 3, 7, …….2K-1.
● Knuth's increments: 1, 4, 13, ..., (3k - 1) / 2.
● Sedgewick's increments: 1, 5, 19, 41, 109…

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.

Algorithm Shellsort(list[], size)


Input : Array list of n Unsorted elements.
Output : Sorted Array.
1. set diff ← size
2. while diff > 1 do
3. set pass ← pass+1
4. set diff ← diff/2
5. for walker ← 0,1, ..... size–diff do
6. if list[walker] > list[walker+diff]
7. list[walker] ↔ list[walker+diff]
8. end if
9. end for
10. end while
end Shellsort

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.

P - 01 ADS Lab: Sorting and Searching Techniques Page No. 4

You might also like