Sorting 2
Sorting 2
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:
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:
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
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:
• 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:
CHAIN:
As linear probing takes much time to search we use another
method called chain
ALGORITHM:
Example: