Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

CPE010 | Data Structures and Algorithms

Searching Techniques and Sorting


Algorithms

Engr. Roman M. Richard


Faculty | Computer Engineering Department
Technological Institute of the Philippines – Quezon City
Kentucky Fried
Chicken
• The real-life story of Colonel Harland Sanders
who was disappointed umpteen times in his life and
still made his dream come true late in his life is
really inspiring.
• He is a seventh grade dropped out who tried
many ventures in life but tasted bitter every time.
He started selling chicken at his age of 40 but his
dream of a restaurant was turned down many times
due to conflicts and wars.
• Later he attempted to franchise his restaurant.
His recipe got rejected 1,009 times before the final
approval. And soon the secret recipe, “Kentucky
Fried Chicken” became a huge hit worldwide. KFC
was expanded globally and the company was sold
for 2 million dollars and his face is still celebrated in
the logos.
Have you stopped your attempts to a venture just
because you were rejected or failed a few times? Can
you even accept a failure of 1,009 times? This story
inspires everyone to try hard and believe in yourself until
you see success despite how many times you have
failed.
Searching Techniques
Searching is the process of finding the position an element within a given list. It is considered a success
if the element is within the list, and a failure if otherwise.

The main idea with searching can be summarized by the following:


• Given: a collection of elements, 𝐴 = (𝑎1 , 𝑎2 , … , 𝑎𝑛 ) and a key element 𝑒𝑘 .
• Output: The element 𝑎𝑖 in 𝐴 that matches 𝑒𝑘 .

There are generally many variations to searching such as:


• Find the first such element
• Find the last such element
• Find the index of the element
• Key versus "equality" based
• Find all such elements
• Find extremal elements(s)
• How to handle failed searches (such that when the object does not exist)

There are two types of searching techniques:


• Linear Search
• Binary Search
Linear Search
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 item is returned,
otherwise the search continues till the end of the data collection.
Linear Search
(Pseudocode/Analysis)
N -> Boundary of the list Analysis
Item -> Searching number Our analysis focuses on the count of
Data -> Linear array times a comparison has to be made before we're
able to find a match to confirm the presence of
Step 1: I := 0 the sought-after value.
Step 2: Repeat while I <= n
If (item = data[i]) • Best Case Complexity: When the key value is
Print "Searching is successful" at the first position, therefor 𝑇(𝑁) = 𝑂(1)
Exit • Worst Case Complexity: 𝑇(𝑁) = 𝑂(𝑁)
Else • Average Case Complexity: (𝐵𝑒𝑠𝑡 + 𝑊𝑜𝑟𝑠𝑡)/
Print "Searching is Unsuccessful" 2 = (1 + 𝑁)/2 𝑜𝑟𝑇(𝑁) = 𝑂(𝑁)
Exit
Binary Search
The binary search is fast and efficient, it also requires the data that will serve as elements to be sorted.

Search a sorted array by repeatedly dividing


the search interval in half. Begin with an
interval covering the whole array. If the value
of the search key is less than the item in the
middle of the interval, narrow the interval to
the lower half. Otherwise, narrow it to the
upper half. Repeatedly check until the value
is found or the interval is empty.
Binary Search
(Pseudocode/Analysis)
Step 1: low :=0, up=n-1
Step 2: Repeat while low <= up Analysis
mid = int(low+up)/2 Best Case Complexity: T(N)=O(1)
if(no=arr[mid]) Worst Case Complexity: T(N)=O(logN)
print "Search element is found!"
exit
else
if(no < arr[mid]) then
up = mid-1
else
low = mid+1
Step 3: Print "Search element is not found"
Step 4: Exit
Hashing
• Previous search times can be improved by using an approach called Hashing.
• Usually implemented on dictionaries.
• There are lots of applications that need to support ONLY the operations INSERT, SEARCH, and
DELETE.
• These are dictionary operations.
• Hashing can make this happen in O(1) and is quite fast in practice.
Dictionary
• A dictionary is a collection of elements.
• Each element has a field called key.
• (key, value)
• Every key is usually distinct.
• Typical dictionary operations are:
• Insert a pair into the dictionary.
• Search the pair with a specified key.
• Delete the pair with a specified key.
• Collection of student records in a class:
• (key, value) = (student_number, assignments and marks)
• All keys are distinct
Dictionary as an Ordered Linear
List
• 𝐿 = (𝑒1 , 𝑒2 , 𝑒3 , … , 𝑒𝑛 )
• Each 𝑒𝑖 is a pair (key, value)
• Array or chain representation:
• Unsorted array: O(n) search time
• Sorted array: O(logn) search time
• Unsorted chain: O(n) search time
• Sorted chain: O(n) search time
Hash Table
• A hash table is a data structure that stores elements and allows
insertions, lookups, and deletions to be performed in O(1) time.
• A hash table is an alternative method for representing a
dictionary.
• In a hash table, a hash function is used to map keys into
positions in a table. This act is called hashing.
• Hash Table Operations:
• Search: compute f(k) and see if a pair exists.
• Insert: computer f(k) and place it in that position.
• Delete: compute f(k) and delete the pair in that position.
Implementation
• The table part is just an ordinary array, it is the hash that we are
interested in.
• The hash is a function that transforms a key into address or index of
array(table) where the record will be stored. If the size of the table is
N, then the integer will be in the range 0 to N-1.
• The integer is used as an index into the array. Thus, in essence, the key itself
indexes the array.
• If h is a hash function and k is key, then h(k) is called hash of the
key and is the index at which a record with the key k should be
place.
• The hash function generates this address by performing some simple
arithmetic or logical operations on the key.
Ideal Hashing Example
Pairs are: (22,a), (33,c), (3,d),(72,e),(85,f)
• (key, value) pairs
Hash table is ht[0:7], m = 8 (m is number of positions in the hash table)
Hash function h is k % m = k % 8
Where are the pairs stored?

[0] [1] [2] [3] [4] [5] [6] [7]


Characteristics of a Good Hash
Function
• The hash value is fully determined by the data being hashed.
• The hash function uses all the input data.
• The hash function “uniformly” distributes the data across the
entire set of possible hash values.
• The hash function generates very different hash values for
similar strings.
Sorting Algorithms
• Definitions
• In place Sorting: Sorting of a data structure does not require any
external data structure for storing the intermediate steps.
• External Sorting: Sorting of records not present in memory.
• Stable Sorting: If the same element is present multiple times, then they
retain the original relative order of positions.
C++ STL Sorting Algorithms
• Sort function template
• void sort(iterator begin, iterator end)
• void sort(iterator begin, iterator end, comparator cmp)
• begin and end are star and end marker of container (or a range of it)
• Container needs support to random access such as vector
• Sort is not stable sorting
• stable_sort() is stable.
Bubble Sort
• Simple and uncomplicated
• Compare neighboring elements
• Swap if out of order
• Two nested loops
• O(n2)
Insertion Sort
• O(n2) sort
• N-1 passes
• After pass p all elements from
0 to p are sorted
• Following step inserts the
next element in correct
position within the sorted part

You might also like