Sorting Hashing: Sam Dominic B. Antonio March 15, 2017 Ms - Jovy Ruth Obliosca IS211
Sorting Hashing: Sam Dominic B. Antonio March 15, 2017 Ms - Jovy Ruth Obliosca IS211
Sorting Hashing: Sam Dominic B. Antonio March 15, 2017 Ms - Jovy Ruth Obliosca IS211
Sorting Hashing
Hashing is a technique to convert a range of key values into a range of indexes of
an array. We're going to use modulo operator to get a range of key values. Consider
an example of hash table of size 20, and the following items are to be stored. Item
are in the (key,value) format.
Concepts of Hashing
HASH TABLE:
It is a Data structure where the data elements are stored(inserted), searched, deleted based on the
keys generated for each element, which is obtained from a hashing function. In a hashing system
the keys are stored in an array which is called the Hash Table. A perfectly implemented hash
table would always promise an average insert/delete/retrieval time of O(1).
HASHING FUNCTION:
A function which employs some algorithm to computes the key K for all the data elements in the
set U, such that the key K which is of a fixed size. The same key K can be used to map data to a
hash table and all the operations like insertion,deletion and searching should be possible. The
values returned by a hash function are also referred to as hash values, hash codes, hash sums,
or hashes.
HASH COLLISION:
A situation when the resultant hashes for two or more data elements in the data set U, maps to
the same location in the has table, is called a hash collision. In such a situation two or more data
elements would qualify to be stored/mapped to the same location in the hash table.
HASH COLLISION RESOLUTION TECHNIQUES:
Open Hashing (Separate chaining)
Open Hashing, is a technique in which the data is not directly stored at the hash key index (k) of
the Hash table. Rather the data at the key index (k) in the hash table is a pointer to the head of the
data structure where the data is actually stored. In the most simple and common implementations
the data structure adopted for storing the element is a linked-list.
In this technique when a data needs to be searched, it might become necessary (worst case) to
traverse all the nodes in the linked list to retrieve the data.
Note that the order in which the data is stored in each of these linked lists (or other data
structures) is completely based on implementation requirements. Some of the popular criteria are
insertion order, frequency of access etc.
Liner Probing (this is prone to clustering of data + Some other constrains. Refer Wiki)
Quadratic probing (for more detail refer Wiki)
Double hashing (in short in case of collision another hashing function is used with the key value
as an input to identify where in the open addressing scheme the data should actually be stored.)
Bubble Sorting
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm
in which each pair of adjacent elements is compared and the elements are swapped if they are not
in order. This algorithm is not suitable for large data sets as its average and worst case
complexity are of (n2) where n is the number of items.
Insertion Sorting
It is a simple Sorting algorithm which sorts the array by shifting elements one by one. Following
are some of the important characteristics of Insertion Sort.
Selection Sorting
Selection sorting is conceptually the most simplest sorting algorithm. This algorithm first finds
the smallest element in the array and exchanges it with the element in the first position, then find
the second smallest element and exchange it with the element in the second position, and
continues in this way until the entire array is sorted.
Graph
a graph is an abstract data type that is meant to implement the undirected graph and directed
graph concepts from mathematics.
A graph data structure consists of a finite (and possibly mutable) set of vertices or nodes or
points, together with a set of unordered pairs of these vertices for an undirected graph or a set of
ordered pairs for a directed graph. These pairs are known as edges, arcs, or lines for an
undirected graph and as arrows, directed edges, directed arcs, or directed lines for a directed
graph. The vertices may be part of the graph structure, or may be external entities represented by
integer indices or references.
Kinds of Graph
DIRECTED GRAPH
The number of edges with one endpoint on a given vertex is called that vertex's degree. In a
directed graph, the number of edges that point to a given vertex is called its in-degree, and the
number that point from it is called its out-degree. Often, we may want to be able to distinguish
between different nodes and edges. We can associate labels with either. We call such a graph
labeled.
UNDIRECTED GRAPH
In a directed graph, the edges point from one vertex to another, while in an undirected graph,
they merely connect two vertices. we can travel forward or backward.It is a bidirectional graph.
WEIGHTED GRAPH
We may also want to associate some cost or weight to the traversal of an edge. When we add this
information, the graph is called weighted. An example of a weighted graph would be the distance
between the capitals of a set of countries.
Directed and undirected graphs may both be weighted. The operations on a weighted graph are
the same with addition of a weight parameter during edge creation:
Algorithm
Algorithm is a step-by-step procedure, which defines a set of instructions to be executed in a
certain order to get the desired output. Algorithms are generally created independent of
underlying languages, i.e. an algorithm can be implemented in more than one programming
language.
From the data structure point of view, following are some important categories of algorithms
Search Algorithm to search an item in a data structure.
Quick Sorting
Quick Sort, as the name suggests, sorts any list very quickly. Quick sort is not stable search, but
it is very fast and requires very less aditional space. It is based on the rule of Divide and
Conquer(also called partition-exchange sort). This algorithm divides the list into three main
parts:
2 .Pivot element
In the list of elements, mentioned in below example, we have taken 25 as pivot. So after the first
pass, the list will be changed like this.
6 8 17 14 25 63 37 52
Hence after the first pass, pivot will be set at its position, with all the elements smaller to it on its
left and all the elements larger than it on the right. Now 6 8 17 14 and 63 37 52 are considered as
two separate lists, and same logic is applied on them, and we keep doing this until the complete
list is sorted.
Heap Sorting
Heap Sort is one of the best sorting methods being in-place and with no quadratic worst-case
scenarios. Heap sort algorithm is divided into two basic parts. Creating a Heap of the unsorted
list.
Then a sorted array is created by repeatedly removing the largest/smallest element from the
heap, and inserting it into the array. The heap is reconstructed after each removal.
Merge Sort is quite fast, and has a time complexity of O(n log n). It is also a stable sort, which
means the "equal" elements are ordered in the same order in the sorted list.