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

Sorting___Searching

Uploaded by

Sahaj Khurana
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Sorting___Searching

Uploaded by

Sahaj Khurana
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

SORTING & SEARCHING

BUBBLE SORT

• Bubble sort is a simple sorting algorithm that repeatedly steps


through the list to be sorted, compares each pair of adjacent
items and swaps them if they are in the wrong order.
• The pass through the list is repeated until no swaps are
needed, which indicates that the list is sorted.
• The algorithm, which is a comparison sort, is named for the
way smaller elements "bubble" to the top of the list.
• Although the algorithm is simple, it is too slow and impractical
for most problems
BUBBLE
S O RT
EXAMPLE
BUBBLE SORT USING PSEUDOCODE

NUMS = [15,30,85,25,40,90,50,65,20,60]
output "Before sorting"
loop C from 0 to NUMS.length -1
output NUMS[C]
end loop

loop PASS from 0 to NUMS.length -2


loop CURRENT from 0 to NUMS.length -2
if NUMS[CURRENT] < NUMS[CURRENT + 1] then
TEMP = NUMS[CURRENT]
NUMS[CURRENT] = NUMS[CURRENT+1]
NUMS[CURRENT+1] = TEMP
end if
end loop
end loop
BUBBLE SORT USING PSEUDOCODE

• The Bubble Sort algorithm utilizes two loops: an outer loop to


iterate over each element in the input list, and an inner loop to
iterate, compare and exchange a pair of values in the list. The
inner loop takes (N-1) iterations while the outer loop takes N
iterations
SELECTION SORT

• Selection sort is a sorting algorithm and it is inefficient on large lists


• Selection sort is noted for its simplicity, and it has performance
advantages over more complicated algorithms in certain situations,
particularly where memory is limited.
• The algorithm divides the input list into two parts: the sublist of items
already sorted, which is built up from left to right at the front (left) of
the list, and the sublist of items remaining to be sorted that occupy
the rest of the list.
• Initially, the sorted sublist is empty and the unsorted sublist is the entire
input list.
• The algorithm proceeds by finding the smallest (or largest, depending on
sorting order) element in the unsorted sublist, exchanging (swapping) it
with the leftmost unsorted element (putting it in sorted order), and
moving the sublist boundaries one element to the right.
SELECTION
S O RT E X A M P L E
SELECTION SORT USING PSEUDOCODE

a - an array containing the list of numbers

Loop i = 0 to a.length - 1
Loop j = i+1 to a.length - 1
if A[i] > A[j]
// Swap the entries
Temp = A[i]
A[i] = A[j]
A[j] = Temp
end if
end loop
end loop
LINEAR SEARCH

• Linear search or sequential search is an algorithm to find an item


in a list.
• It starts at the first element and compares each element to the
one it’s looking for until it finds it.
• Commonly used with collections(which are unsorted lists of
items).
LINEAR SEARCH – PSEUDOCODE WITH ARRAYS

FOUND = false #this is called a *flag*


I = 0
ARR=[3,15,18,-9,0,4]
SEARCHVAL = -9
loop while I < ARR.length and not FOUND
if ARR[I] == SEARCHVAL then
FOUND = true
else
I = I + 1
end if
end loop
if FOUND then
output I
else
output "Not found"
end if
L I N E A R S E A RC H – P S E U D O C O D E W I T H C O L L E C T I O N S

FOUND = false
I = 0
COL={5,12,-9,0,23}
SEARCHVAL= -9
COL.resetNext()
loop while COL.hasNext() and not FOUND
if COL.getNext() == SEARCHVAL then
FOUND = true
else
I = I + 1
end if
end loop
if FOUND then
output I
else
output "Not Found"
end if
LINEAR SEARCH – PSEUDOCODE
WITH COLLECTIONS

NAMES = “Bob”,”Betty”,”Kim”,”Lucy”,”Dave”
output "These names start with D"
loop while NAMES.hasNext()
NAME = NAMES.getNext()
if firstLetter(NAME) == "D" then
output NAME
end if
end loop
BINARY SEARCH

• Binary search, also known as half-interval search, is a


search algorithm that finds the position of a target
value within a sorted array.
• It works by comparing the target value to the middle
element of the array;
• If they are unequal, the lower or upper half of the array
is eliminated depending on the result and the search is
repeated in the remaining sub-array until it is
successful.
• It only applies to SORTED arrays (where there are
usually no duplicate values, or duplicates do not
BINARY SEARCH
HOW TO IMPLEMENT BINARY
SEARCH?

1. Set the low index to the first element of the array and the high index to the last
element.
2. Set the middle index to the average of the low and high indices.
• If the element at the middle index is the target element, return the middle
index.
• Otherwise, based on the value of the key to be found and the value of the
middle element, decide the next search space.
• If the target is less than the element at the middle index, set the high index
to middle index – 1.
• If the target is greater than the element at the middle index, set the low
index to middle index + 1.
3. Perform step 2 repeatedly until the target element is found or the search space
is exhausted.
BINARY SEARCH

ARR if FOUND then


FOUND = false output MIDPOS
else
MINPOS = 0 output "Not found"
MAXPOS = ARR.LENGTH - 1 end if
loop while MINPOS <= MAXPOS and not FOUND
MIDPOS = (MAXPOS + MINPOS) div 2 # integer division
if ARR[MIDPOS] == SEARCHVAL then
FOUND = true #the correct spot is in MIDPOS
else if ARR[MIDPOS] > SEARCHVAL then
# can ignore the top half now!
MAXPOS = MIDPOS - 1
else
# can ignore the bottom half now!
MINPOS = MIDPOS + 1
end if
end loop
TRACE TABLE

ARR = [1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29,


31, 37] SEARCHVAL = 29

MINPOS <= MAXPOS and ARR[MIDPOS


SEARCHVAL FOUND MAXPOS MINPOS MIDPOS OUTPUT
not FOUND? ]

29 false 12 0 true 6 13

15 false 12 7 true 10 29

15 false 9 7 true 8 19

15 false 9 9 true 9 17

15 false 8 9 false -1

You might also like