Dsa Lab 3 Searching
Dsa Lab 3 Searching
Algorithms
LE SUMAYA AMIN
Link-List
Output
Searching
LINEAR(DATA,N, ITEM,LOC)
Set DATA[N+1] = ITEM
Set LOC = 1
Repeat while DATA[LOC] !=ITEM
Set LOC = LOC +1
If LOC = N+1, then Set LOC =0
Exit.
Pseudocode (Linear Search Algo)
Set LOC = 1:
LOC is initialized to 1, indicating the starting position of the search. It represents the index or location of the
current element being compared.
Exit:
The search terminates either when the ITEM is found (when DATA[LOC] == ITEM) or when LOC reaches N+1
(indicating ITEM was not found and the search ends).
Binary search Pseudocode
Set BEG=LB,
END=UB,
and MID= INT((BEG+END)/2)
Repeat Steps 3 and 4 while BEG<=END and DATA[MID]!=ITEM
if ITEM<DATA[MID] then
set END=MID – 1
else
set BEG = MID +1
Set MID = = INT((BEG+END)/2)
if DATA[MID]=ITEM, then
set LOC = MID
else
set LOC = NULL
Exit.
Set BEG = LB, END = UB, and MID = INT((BEG + END) / 2):
BEG is initialized to LB (lower bound), and END is set to UB (upper bound),
which represent the current search range within the array.MID is calculated
as the middle index of the array using the formula: (BEG + END) / 2. The
function INT takes the integer part of the division to get a valid index.
Repeat Steps 3 and 4 while BEG <= END and DATA[MID] != ITEM:
The loop continues as long as the search range (BEG ≤ END) is valid, and
the item at the middle index MID is not equal to the target ITEM.
Exit:
The algorithm terminates either when the ITEM is found or when the
search range is exhausted, meaning the ITEM is not in the array.
Lab Task
Exercise 1:
Create function for each algorithm using Array:
Linear search
Binary search
Input list of numbers: 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
Exercise 2:
Write a program to search an element linearly from the entered numbers. Also, indicate its
position in case the element is found or unavailability.
Exercise :
Write a program to demonstrate binary search. Use character array and store 10 names. This
program prompts the user to enter ten names. They are stored in ascending order the name[15]
[10] the fn[15] is used to store the name which we want to search.
SORTING
1. selection
2. insertion
3. bubble
4. merge sort
Selection Sort
Step 1b: Check the rest of the array (starting from i+1) to find if any
element is smaller than the current minimum.
This algorithm takes one value at a time and builds up another sorted
list of values. It helps if you can imagine what you do when you play a
game of cards: you pick a card and insert it to an already sorted list
of cards.
def insertion_sort(A):
# Iterate over each element starting from the second element
for i in range(1, len(A)):
value = A[i]
j=i-1
# Repeat shifting elements until the correct position for 'value' is found
while j >= 0 and A[j] > value:
A[j + 1] = A[j]
j -= 1
# Place 'value' into its correct position
A[j + 1] = value
# Example usage
array = [12, 11, 13, 5, 6]
insertion_sort(array)
print("Sorted array:", array)
Bubble Sort
This algorithm looks at pairs of entries in the array and swaps their
order if needed. After the first step of the algorithm the maximal
value will "bubble" up the list and will occupy the last entry. After the
second iteration, the second largest value will "bubble" up the list and
will occupy the next to last entry, and so on.
procedure BubbleSort(arr, n)
for i from n-1 down to 1 do // Outer loop runs from last element
to first
for j from 0 to i-1 do // Inner loop runs from the first element
to 'i-1'
if arr[j] > arr[j+1] then // Compare adjacent elements
swap(arr[j], arr[j+1]) // Swap if the left element is greater
than the right
end for
end procedure
Merge Sort
Merge - Combine the two sorted groups into one large sorted list
funcmergesort( var a as array )
if ( n == 1 ) return a
var l1 as array = a[0] ... a[n/2]
var l2 as array = a[n/2+1] ... a[n]
l1 = mergesort( l1 )
l2 = mergesort( l2 )
return merge( l1, l2 )
end func
func merge( var a as array, var b as array )
var c as array
while ( a and b have elements )
if ( a[0] > b[0] )
add b[0] to the end of c
remove b[0] from b
else
add a[0] to the end of c
remove a[0] from a
while ( a has elements )
add a[0] to the end of c
remove a[0] from a
while ( b has elements )
add b[0] to the end of c
remove b[0] from b
return c
end func
Code Breakdown
if (n == 1) return a
If the array has only one element (n == 1), it is already sorted, so the
function returns the array as it is
Exercise 1:
Write a program to sort a given array by using the insertion sort.
Exercise 2:
Write a program to sort a given array by using the selection sort.
Exercise 3:
Write a program to sort a given array by using the bubble sort.
Exercise 4 :
Write a program to sort a given array by using the merge sort.