0% found this document useful (0 votes)
9 views19 pages

Sorting 2

The document provides an overview of sorting and searching algorithms, detailing various methods such as Bubble Sort, Quick Sort, and Linear Search, among others. It also explains hashing, its components, types of hash functions, and collision handling techniques. Key concepts include the definition of sorting and searching, examples of algorithms, and methods for managing data efficiently.

Uploaded by

2313721033030
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)
9 views19 pages

Sorting 2

The document provides an overview of sorting and searching algorithms, detailing various methods such as Bubble Sort, Quick Sort, and Linear Search, among others. It also explains hashing, its components, types of hash functions, and collision handling techniques. Key concepts include the definition of sorting and searching, examples of algorithms, and methods for managing data efficiently.

Uploaded by

2313721033030
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/ 19

SORTING AND SEARCHING

1. What is sorting?
i) Sorting is a process of arranging group of items in an
ascending or descending order.
ii) Bubble sort, Quick sort, Heap sort, Merge sort and selection
sort are the various sorting algorithms
2. What is searching?
A search algorithm is the step by step procedure used to locate
specific data among a collection of data
Example: Linear Search, Binary Search
3. Insertion Sort:
Insertion sort is a simple sorting algorithm. It works by taking
elements from the list one by one and inserting then in their correct
into a new sorted list.
ALGORITHM:

Example:
4. Bubble sort:
Bubble sort is a simple sorting algorithm. The algorithm starts at
at the beginning of the list of values stored in an array. It compares each pair
of adjacent elements and swaps them if they are in the unsorted order.
This comparison and passed to be continued until no swaps are needed which
indicates that the list of values stored in an array is sorted.
ALGORITHM:

Example:
Let’s consider an array with values{15,11,16,12,14,13}Below, we have a
pictorial representation of how bubble sort will sort the given array.
5. Selection Sort :
The Selection sort is a simple sorting algorithm that improves on
the performance of bubble sort by making only one exchange for every pass
through the list. The algorithm will first find the smallest elements in array and
swap it with the element in the first position of an array, then it will find the
second smallest element and swap that element with the element in the
second position, and it will continue until the entire array is sorted in
respective order.
ALGORITHM:
Example:

6. Quick Sort:
Quick Sort is a sorting algorithm based on the Divide and Conquer
algorithm that picks an element as a pivot and partitions the given array
around the picked pivot by placing the pivot in its correct position in the
sorted array.
ALGORITHM:
Example:

7. Merge Sort:
Merge sort is defined as a sorting algorithm that works by
dividing an array into smaller subarrays, sorting each subarray,
and then merging the sorted subarrays back together to form
the final sorted array.
ALGORITHM:
Example:
8. Linear Search:
Linear Search also called sequential search is a sequential
method for finding a particular value in a list. This method checks
the search element with each element in sequence until the desired
element in found or the list or array is exhausted. In this searching
algorithm, list need not be ordered.
ALGORITHM:
Example:

9. Binary Search:
Binary search also called half-interval search algorithm. It
finds the position of a search element within a sorted array. The
binary search algorithm can be done as divide-and-conquer
search algorithm.
ALGORITHM:
Example:

Where L=BEG, M=MID, H=END.


10. Heap sort:
Heap sort is a comparison-based sorting technique based
on Binary Heap data structure. It is similar to the selection
sort where we first find the minimum element and place the
minimum element at the beginning. Repeat the same process
for the remaining elements.
ALGORITHM FOR INSERTING:
INHEAP(TREE, N, ITEM)
A heap H with N elements is stored in the array TREE, and an
ITEM of information is given.
⮚ This procedure inserts ITEM as a new element of H.
⮚ PTR gives the location of ITEM as it rises in the tree,
⮚ PAR denotes the location of the parent of ITEM.

DELETING A ROOT OF A HEAP:


◈ Suppose H is the heap with N elements, and suppose we want
to delete the root R of H . This is accomplished as follows:
1. Assign the root R to some variable ITEM.
2. Replace the deleted node R by the last node L of H so that H is
still a complete tree, but not necessarily a heap.
3. (Reheap) Let L sink to its appropriate place in H so that H is
finally a heap.
DELHEAP(TREE, N, ITEM)
A heap with N elements is stored in the array TREE. This
procedure assigns the root TREE[1] of H to the variable ITEM
and then replace the remaining elements.
⮚ LAST saves the value of original last node of H.
⮚ PTR, LEFT, RIGHT give the location of LAST.
ALGORITHM:
ALGORITHM FOR HEAP SORT:

Example:
HASHING
1. What is Hashing?
Hashing refers to the process of generating a fixed-size
output from an input of variable size using the
mathematical formulas known as hash functions. This
technique determines an index or location for the storage of
an item in a data structure.
Example:

2. Components of Hashing
There are majorly three components of hashing:
i) Key: A Key can be anything string or integer which is fed
as input in the hash function the technique that
determines an index or location for storage of an item in
a data structure.
ii) Hash Function: The hash function receives the input
key and returns the index of an element in an array
called a hash table. The index is known as the hash
index.
iii) Hash Table: Hash table is a data structure that maps
keys to values using a special function called a hash
function. Hash stores the data in an associative manner
in an array where each data value has its own unique
index.
Example:
3. What is Collision?
The hashing process generates a small number for a big key, so
there is a possibility that two keys could produce the same value.
The situation where the newly inserted key maps to an already
occupied, and it must be handled using some collision handling
technology.
Example:

4. Types of Hash functions:


There are many hash functions that use numeric or
alphanumeric keys.
i) Division Method.
ii) Mid Square Method
iii) Folding Method.
iv) Multiplication Method

Divison Method:
i) This is the most simple and easiest method to generate a
hash value. The hash function divides the value k by M
and then uses the remainder obtained.
ii) Formula:
iii) h(K) = k mod M
iv) Here,
k is the key value, and
M is the size of the hash table.
v) It is best suited that M is a prime number as that can
make sure the keys are more uniformly distributed. The
hash function is dependent upon the remainder of a
division.
vi) Example:
k = 12345
M = 95
h(12345) = 12345 mod 95
= 90
k = 1276
M = 11
h(1276) = 1276 mod 11
=0

2. Mid Square Method:


The mid-square method is a very good hashing method. It
involves two steps to compute the hash value-
1. Square the value of the key k i.e. k 2
2. Extract the middle r digits as the hash value.
Formula:
h(K) = h(k x k)
Here,
k is the key value.
The value of r can be decided based on the size of the table.
Example:
Suppose the hash table has 100 memory locations. So r = 2
because two digits are required to map the key to the memory
location.
k = 60
k x k = 60 x 60
= 3600
h(60) = 60
The hash value obtained is 60

3. Digit Folding Method:


This method involves two steps:
1. Divide the key-value k into a number of parts i.e. k1, k2,
k3,….,kn, where each part has the same number of digits
except for the last part that can have lesser digits than the
other parts.
2. Add the individual parts. The hash value is obtained by
ignoring the last carry if any.
Formula:
k = k1, k2, k3, k4, ….., kn
s = k1+ k2 + k3 + k4 +….+ kn
h(K)= s
Here,
s is obtained by adding the parts of the key k
Example:
k = 12345
k1 = 12, k2 = 34, k3 = 5
s = k1 + k2 + k3
= 12 + 34 + 5
= 51
h(K) = 51

4. Multiplication Method
This method involves the following steps:
1. Choose a constant value A such that 0 < A < 1.
2. Multiply the key value with A.
3. Extract the fractional part of kA.
4. Multiply the result of the above step by the size of the hash
table i.e. M.
5. The resulting hash value is obtained by taking the floor of the
result obtained in step 4.
Formula:

h(K) = floor (M (kA mod 1))


Here,
M is the size of the hash table.
k is the key value.
A is a constant value.
Example:
k = 12345
A = 0.357840
M = 100
h(12345) = floor[ 100 (12345*0.357840 mod 1)]
= floor[ 100 (4417.5348 mod 1) ]
= floor[ 100 (0.5348) ]
= floor[ 53.48 ]
= 53
6. OVERFLOW HANDLING
An overflow occurs at the time of the home bucket for a
new pair (key, element) is full.

Eliminate overflows by allowing each bucket to keep a list of all pairs


for which it is the home bucket.

• Linear probing
• Chain.
Linear Probing:
Linear Probing is performed to resolve collisions by placing the data
into the next open slot in the table.

ALGORITHM:
Example:

Problem of Linear Probing

• Identifiers are tending to cluster together


• Adjacent clusters are tending to coalesce
• Increase or enhance the search time

CHAIN:
As linear probing takes much time to search we use another
method called chain
ALGORITHM:
Example:

You might also like