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

Lab 04 Sorting & Searching

The document outlines the Data Structures Lab for Fall 2024, focusing on various sorting algorithms (Bubble, Selection, Insertion, Shell, and Comb Sort) and searching algorithms (Linear, Binary, and Interpolation Search). Each algorithm is described with detailed steps for implementation and optimization techniques. Students are required to maintain discipline, complete all tasks, and get their lab work checked at the end of the session.

Uploaded by

alishba.subhani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lab 04 Sorting & Searching

The document outlines the Data Structures Lab for Fall 2024, focusing on various sorting algorithms (Bubble, Selection, Insertion, Shell, and Comb Sort) and searching algorithms (Linear, Binary, and Interpolation Search). Each algorithm is described with detailed steps for implementation and optimization techniques. Students are required to maintain discipline, complete all tasks, and get their lab work checked at the end of the session.

Uploaded by

alishba.subhani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Data Structures Lab

Session 4
Course: Data Structures (CS218) Semester: Fall 2024
Instructor: Zainab Asif Jawed T.A: N/A

Note:
 Lab manual cover following below elementary sorting and searching algorithms
{Bubble, insertion, selection, shell sort, comb sort, binary search, interpolation search
, Linear Search}
 Maintain discipline during the lab.
 Just raise hand if you have any problem.
 Completing all tasks of each lab is compulsory.
 Get your lab checked at the end of the session.

Bubble Sort:
Steps:
1. Outer Loop: Let i be the iteration count, starting from 0 to n:
2. Inner Loop: Let j be an index that runs from 0 to n-i-1:
o Compare each element A[j] with the next element A[j+1].
o If A[j] > A[j+1], swap them. This moves the larger element
towards the end of the array.
3. After each complete pass of the inner loop, the largest unsorted element
is placed at its correct position at the end of the array, and the sorted
portion grows from the right.
4. Optimization (Optional):
o If no elements were swapped during the inner pass, it means the
array is already sorted. In this case, exit the loop early.
Selection Sort:

Steps:
1. Outer Loop: Start with the first element (index i), and run a loop from i
= 0 to n-1:
o Initialize the min_idx to the current index i. That is, assume that
the first element is the minimum element.
2. Inner Loop: The inner loop starts from j = i + 1 and runs through the
rest of the array (until the last element j = n).
o For each iteration of the inner loop, the current element arr[j] is
compared with the current minimum element arr[min_idx].
o If arr[j] is smaller than arr[min_idx], we update min_idx to j,
indicating that arr[j] is the new minimum element.
3. Check If Swap is necessary: After the inner loop completes, if min_idx
is not equal to i, it means that the smallest element found is not at the
current index i. Hence, we need to swap the smallest element with the
element at i.
Insertion Sort:

1. Outer Loop: This loop iterates over the array starting from the second
element (index i = 1) to the last element (i = n-1).
o The element at position i is considered the key.
o j is initialized to i - 1, the index of the last element in the sorted
subarray.
2. Inner Loop: Compare the key with the elements in the sorted portion
(from i-1 to 0) and shift elements one position to the right if they are
greater than the key. The condition arr[j] > key ensures that elements are
only shifted when they are larger than the key.
3. Insert the Key: Place the key in its correct position once the right place
is found.
Shell Sort : (Improvement Over Insertion Sort)
1. 1st Outer Loop choosing and controlling Gap Sequence: Begin with a
large gap (often n/2), and reduce the gap in each step by half until it
becomes 1.
2. Outer Loop for Gapped Insertion Sort: For each gap, the algorithm
performs a gapped insertion sort. The loop starts from index i = gap and
goes through the array. This means elements that are gap distance apart
are compared and sorted.
o Store Current Element: The element at index i is temporarily
stored in key. This is the element that needs to be placed at its
correct position among elements that are gap apart.
o j is initialized to i - 1, the index of the last element in the sorted
subarray.
3. Inner Loop to shift Elements: The inner loop shifts elements that are
gap positions apart. The condition arr[j - gap] > key ensures that
elements larger than key are moved to the right (shifted by gap
positions). for (int j = i; j >= gap && arr[j - gap] > key; j -= gap)
o Each time an element is found to be greater than key, it is moved
to the right by gap positions.
4. Insert the Key: Once the correct position for temp is found (when arr[j
- gap] <= key), key is inserted at position j.
23, 29, 15, 19, 31, 7, 9, 5, 2
Comb Sort: (Improvement Over Bubble Sort)

Steps:

1. Initialize the Gap: Start with a gap equal to the length of the array n,
shrink factor is a constant, typically set to 1.3, and a sorted flag
(initialized to True) is used to check if the array is already sorted. The gap
continues to reduce by shrink factor until it reaches 1.
2. Main Loop to Reduce Gap: Continue sorting until the gap becomes 1
and no further swaps are required (indicated by sorted being true).
3. Update Gap: The gap is reduced by dividing it by the shrink factor. If
the gap becomes less than 1, it is set to 1. Once the gap becomes 1, it
behaves like Bubble Sort.
4. Perform Gapped Bubble Sort
o For the current gap, compare elements that are gap positions apart.
o If an element is larger than the one gap positions ahead, they are
swapped.
o If any swap occurs, set sorted = false to indicate that the array is
not yet fully sorted.

SEARCHING ALGORITHMS:

Linear Search Algorithm: Linear search is a very simple search algorithm. 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.

1. Start from the first element of the array and compare it with the target x.
2. Iterate through the array:
 For each element, check if it matches the target x.
 If a match is found, return the index of the element.
3. If the target is not found after checking all elements, return -1 to indicate that the element
is not in the array.
Binary Search Algorithm: (ALWAYS performed over SORTED array)

Binary Search is a searching algorithm for finding an element's position in a sorted array. In
this approach, the element is always searched in the middle of a portion of an array. Binary
search can be implemented only on a sorted list of items. If the elements are not sorted
already, we need to sort them first.
1. Initialize pointers: Set two pointers, low (start of the array) and high (end of the array).
2. Middle Element: Compute the middle index mid = (low + high) / 2.
3. Compare:
 If the middle element is equal to the target x, return mid (index of the target).
 If the middle element is greater than the target, narrow the search to the left
half by updating high = mid – 1.
 If the middle element is less than the target, narrow the search to the right half
by updating low = mid + 1.
4. Repeat: Continue dividing the search space in half until the target is found or low
becomes greater than high.

Interpolation Search:
The Interpolation Search is an improvement over Binary Search for instances, where the values in a
sorted array are uniformly distributed.

The idea of formula is to return higher value of pos when element to be searched is closer to
array[high] and smaller value when closer to array[low]

1. Initialize: Set two variables low (start of the array) and high (end of the array).
2. Estimate Position: Use the interpolation formula to estimate the position pos.
3. Check the Position:
 If the element at arr[pos] equals x, return pos.
 If arr[pos] is less than x, narrow the search to the right half by updating low =
pos + 1.
 If arr[pos] is greater than x, narrow the search to the left half by updating high
= pos - 1.
4. Repeat the process until the target is found or low becomes greater than high.
5. Return -1: If the target is not found, return -1.

You might also like