SORTING
SORTING
Internal Sorting :
When all data is placed in the main memory or internal memory then
sorting is called internal sorting.
In internal sorting, the problem cannot take input beyond its size.
Example: heap sort, bubble sort, selection sort, quick sort, shell sort,
insertion sort.
External Sorting :
When all data that needs to be sorted cannot be placed in memory at a
time, the sorting is called external sorting. External Sorting is
used for the massive amount of data.
Merge Sort and its variations are typically used for external sorting.
Some external storage like hard disks and CDs are used for external
sorting.
Example: Merge sort, Tag sort, Polyphase sort, Four tape sort,
External radix sort, Internal merge sort, etc.
HEAP SORT
Heapsort is a popular and efficient sorting algorithm. The concept of heap sort is to
eliminate the elements one by one from the heap part of the list, and then insert them
into the sorted part of the list.
Algorithm
1. HeapSort(arr)
2. BuildMaxHeap(arr)
3. for i = length(arr) to 2
4. swap arr[1] with arr[i]
5. heap_size[arr] = heap_size[arr] ? 1
6. MaxHeapify(arr,1)
7. End
BuildMaxHeap(arr)
1. BuildMaxHeap(arr)
2. heap_size(arr) = length(arr)
3. for i = length(arr)/2 to 1
4. MaxHeapify(arr,i)
5. End
MaxHeapify(arr,i)
1. MaxHeapify(arr,i)
2. L = left(i)
3. R = right(i)
4. if L ? heap_size[arr] and arr[L] > arr[i]
5. largest = L
6. else
7. largest = i
8. if R ? heap_size[arr] and arr[R] > arr[largest]
9. largest = R
10. if largest != i
11. swap arr[i] with arr[largest]
12. MaxHeapify(arr,largest)
13. End
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
largest = l;
}
largest = r;
if (largest != i) {
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
heapify(arr, n, i);
}
// One by one extract an element from heap
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
printf("\n");
// Driver's code
int main() {
printArray(arr, n);
return 0;
MERGE SORT
Merge sort is the sorting technique that follows the divide and conquer approach. This
article will be very helpful and interesting to students as they might face merge sort as a
question in their examinations. In coding or technical interviews for software engineers,
sorting algorithms are widely asked. So, it is important to discuss the topic.
Merge sort is similar to the quick sort algorithm as it uses the divide and conquer
approach to sort the elements. It is one of the most popular and efficient sorting
algorithm. It divides the given list into two equal halves, calls itself for the two halves and
then merges the two sorted halves. We have to define the merge() function to perform
the merging.
The sub-lists are divided again and again into halves until the list cannot be divided
further. Then we combine the pair of one element lists into two-element lists, sorting
them in the process. The sorted two-element pairs is merged into the four-element lists,
and so on until we get the sorted list.
In the following algorithm, arr is the given array, beg is the starting element, and end is
the last element of the array.
#include <stdio.h>
#include <stdlib.h>
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
i = 0;
j = 0;
k = l;
arr[k] = L[i];
i++;
else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
arr[k] = R[j];
j++;
k++;
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
int i;
printf("\n");
}
// Driver code
int main()
printArray(arr, arr_size);
printArray(arr, arr_size);
return 0;
QUICK SORT
The working procedure of Quicksort is also simple. This article will be very helpful and
interesting to students as they might face quicksort as a question in their examinations.
So, it is important to discuss the topic.
Sorting is a way of arranging items in a systematic manner. Quicksort is the widely used
sorting algorithm that makes n log n comparisons in average case for sorting an array of
n elements. It is a faster and highly efficient sorting algorithm. This algorithm follows the
divide and conquer approach. Divide and conquer is a technique of breaking down the
algorithms into subproblems, then solving the subproblems, and combining the results
back together to solve the original problem.
Divide: In Divide, first pick a pivot element. After that, partition or rearrange the array
into two sub-arrays such that each element in the left sub-array is less than or equal to
the pivot element and each element in the right sub-array is larger than the pivot
element.
Conquer: Recursively, sort two subarrays with Quicksort.
Quicksort picks an element as pivot, and then it partitions the given array around the
picked pivot element. In quick sort, a large array is divided into two arrays in which one
holds values that are smaller than the specified value (Pivot), and another array holds
the values that are greater than the pivot.
After that, left and right sub-arrays are also partitioned using the same approach. It will
continue until the single element remains in the sub-array.
o Pivot can be random, i.e. select the random pivot from the given array.
o Pivot can either be the rightmost element of the leftmost element of the given
array.
o Select median as the pivot element.
Algorithm
Algorithm:
// Partition function
int i = low - 1;
i++;
swap(&arr[i], &arr[j]);
return i + 1;
quickSort(arr, pi + 1, high);
}
}
int t = *a;
*a = *b;
*b = t;
int main() {
quickSort(arr, 0, n - 1);
return 0;
The process of radix sort works similar to the sorting of students names, according to the
alphabetical order. In this case, there are 26 radix formed due to the 26 alphabets in
English. In the first pass, the names of students are grouped according to the ascending
order of the first letter of their names. After that, in the second pass, their names are
grouped according to the ascending order of the second letter of their name. And the
process continues until we find the sorted list.
Algorithm
1. radixSort(arr)
2. max = largest element in the given array
3. d = number of digits in the largest element (or, max)
4. Now, create d buckets of size 0 - 9
5. for i -> 0 to d
6. sort the array elements using counting sort (or any stable sort) according to the digits at
7. the ith place
The steps used in the sorting of radix sort are listed as follows -
o First, we have to find the largest element (suppose max) from the given array.
Suppose 'x' be the number of digits in max. The 'x' is calculated because we need to
go through the significant places of all elements.
o After that, go through one by one each significant place. Here, we have to use any
stable sorting algorithm to sort the digits of each significant place.
Now let's see the working of radix sort in detail by using an example. To understand it
more clearly, let's take an unsorted array and try to sort it using radix sort. It will make
the explanation clearer and easier.
In the given array, the largest element is 736 that have 3 digits in it. So, the loop will run
up to three times (i.e., to the hundreds place). That means three passes are required to
sort the array.
Now, first sort the elements on the basis of unit place digits (i.e., x = 0). Here, we are
using the counting sort algorithm to sort the elements.
Pass 1:
In the first pass, the list is sorted on the basis of the digits at 0's place.
After the first pass, the array elements are -
Pass 2:
In this pass, the list is sorted on the basis of the next significant digits (i.e., digits at
10th place).
After the second pass, the array elements are -
Pass 3:
In this pass, the list is sorted on the basis of the next significant digits (i.e., digits at
100th place).
SEQUENTIAL SEARCH:
// C code to linearly search x in arr[].
#include <stdio.h>
if (arr[i] == x)
return i;
return -1;
// Driver code
int main(void)
int x = 10;
// Function call
(result == -1)
return 0;
BINARY SEARCH:
// C program to implement iterative Binary Search
#include <stdio.h>
// An iterative binary search function.
if (arr[mid] == x)
return mid;
if (arr[mid] < x)
low = mid + 1;
else
high = mid - 1;
return -1;
// Driver code
int main(void)
{
int x = 10;
HASHING:
Hashing is a technique used in data structures that efficiently stores and
retrieves data in a way that allows for quick access. It involves mapping
data to a specific index in a hash table using a hash function that
enables fast retrieval of information based on its key. This method is
commonly used in databases, caching systems, and various programming
applications to optimize search and retrieval operations. The great thing
about hashing is, we can achieve all three operations (search, insert and
delete) in O(1) time on average.
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.
Need for Hash data structure
The amount of data on the internet is growing exponentially every day,
making it difficult to store it all effectively. In day-to-day programming, this
amount of data might not be that big, but still, it needs to be stored,
accessed, and processed easily and efficiently. A very common data
structure that is used for such a purpose is the Array data structure.
Now the question arises if Array was already there, what was the need for
a new data structure! The answer to this is in the word “efficiency“.
Though storing in Array takes O(1) time, searching in it takes at
least O(log n) time. This time appears to be small, but for a large data set,
it can cause a lot of problems and this, in turn, makes the Array data
structure inefficient.
So now we are looking for a data structure that can store the data and
search in it in constant time, i.e. in O(1) time. This is how Hashing data
structure came into play. With the introduction of the Hash data structure,
it is now possible to easily store data in constant time and retrieve them in
constant time as well.
Components of Hashing
There are majorly three components of hashing:
1. 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.
2. 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.
3. 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.
Components of Hashing
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.
Collision in Hashing