Chapter 8 ALGO
Chapter 8 ALGO
Chapter Eight
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. Heap sort is an in-
place algorithm. It follows LIFO (Last In First Out) principle in which recently added element
must be removed from the stack first. Heap sort processes the elements by creating the min-heap
or max-heap using the elements of the given array. Min-heap or max-heap represents the
ordering of array in which the root element represents the minimum or maximum element of the
array. The worst case its complexity is O(n log n)
Heap is a special case of balanced binary tree data structure where the root-node key is compared
with its children and arranged accordingly. If α has child node β then −
key(α) ≥ key(β)
As the value of parent is greater than that of child, this property generates Max Heap. Based on
this criteria, a heap can be of two types −
For Input → 35 33 42 10 14 19 27 44 26 31
Min-Heap − Where the value of the root node is less than or equal to either of its children.
Max-Heap − Where the value of the root node is greater than or equal to either of its children.
1
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science Students
Quick Sort
Quick sort is the fastest known algorithm. It uses divide and conquer strategy and in the worst
case its complexity is O (n2). But its expected complexity is O(nlogn).
Algorithm:
1. Choose a pivot value (mostly the first element is taken as the pivot value)
2. Position the pivot element and partition the list so that:
the left part has items less than or equal to the pivot value
the right part has items greater than or equal to the pivot value
3. Recursively sort the left part
4. Recursively sort the right part
The following algorithm can be used to position a pivot value and create partition.
Left=0;
Right=n-1; // n is the total number of elements in the list
PivotPos=Left;
while(Left<Right)
{
if(PivotPos==Left)
{
if(Data[Left]>Data[Right])
2
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science Students
{
swap(data[Left], Data[Right]);
PivotPos=Right;
Left++;
}
else
Right--;
}
else
{
if(Data[Left]>Data[Right])
{
swap(data[Left], Data[Right]);
PivotPos=Left;
Right--;
}
else
Left++;
}
}
3
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science Students
4
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science Students
Merge Sort
Like quick sort, merge sort uses divide and conquer strategy and its time complexity is O(nlogn).
1. Divide the array in to two halves.
2. Recursively sort the first n/2 items.
3. Recursively sort the last n/2 items.
4. Merge sorted items (using an auxiliary array).
ALGORITHM
Let A be a linear array of size n, A [1], A [2], A [3]...... A [n], l1 and u1 represent lower and
upper bounds of the first sub-array and l2 and u2 represent the lower and upper bound of the
second sub-array. Aux is an auxiliary array of size n. Size is the sizes of merge files.
1. Input an array of n elements to be sorted
2. Size = 1
3. Repeat through the step 13 while (Size < n)
(a) set l1 = 0; k = 0
4. Repeat through step 10 while ((l1+Size) < n)
(a) l2 = l1+Size
(b) u1 = l2–1
5. If ((l2+Size–1) < n)
(i) u2 = l2+Size–1
(b) Else
(i) u2 = n-1
6. Initialize i = l1; j = l2 and repeat through step 7 if (i <= u1) and ( j < = u2)
7. If (A [i] < = A[j])
(i) Aux [k] = A [i++]
(b) Else
(i) Aux [k] = A [j++]
8. Repeat the step 8 by incrementing the value of k until (i < = u1)
(a) Aux [k] = A [I++]
5
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science Students
6
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science Students
7
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science Students
Shell Sort
Shell sort is a sorting algorithm that is highly efficient and is based on the insertion sort
algorithm. This algorithm avoids large shifts, as in insertion sort, where the smaller value is on
the far right and must be moved to the far left.
Shell sort is a highly efficient sorting algorithm and is based on insertion sort algorithm. This
algorithm avoids large shifts as in case of insertion sort, if the smaller value is to the far right and
has to be moved to the far left.
8
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science Students
This algorithm uses insertion sort on a widely spread elements, first to sort them and then sorts
the less widely spaced elements. This spacing is termed as interval. This interval is calculated
based on Knuth's formula as −
Knuth's Formula
h=h*3+1
where − h is interval with initial value 1
This algorithm is quite efficient for medium-sized data sets as its average and worst-case
complexity of this algorithm depends on the gap sequence the best known is Ο(n), where n is the
number of items. And the worst case space complexity is O (n).
We compare values in each sub-list and swap them (if necessary) in the original array. After this
step, the new array should look like this –
Then, we take interval of 1 and this gap generates two sub-lists - {14, 27, 35, 42}, {19, 10, 33,
44}
9
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science Students
We compare and swap the values, if required, in the original array. After this step, the array
should look like this –
Finally, we sort the rest of the array using interval of value 1. Shell sort uses insertion sort to sort
the array.
10
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science Students
11
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science Students
Algorithm
Hash Function
The hash function in a data structure maps the arbitrary size of data to fixed-sized data. It returns
the following values: a small integer value (also known as hash value), hash codes, and hash
sums. The hashing techniques in the data structure are very interesting, such as:
hash = hashfunc(key)
index = hash % array_size
The three characteristics of the hash function in the data structure are:
1. Collision free
2. Property to be hidden
3. Puzzle friendly
Hash Table
12
Complied By Mr. Abraham W
Data Structure and Algorithms For 2nd Year Computer Science Students
Hashing in data structure uses hash tables to store the key-value pairs. The hash table then uses
the hash function to generate an index. Hashing uses this unique index to perform insert, update,
and search operations.
It can be defined as a bucket where the data are stored in an array format.
13
Complied By Mr. Abraham W