Coding
Coding
1 Sorting algorithms:
5.1.1 Insertion sort
5.1.2 Selection sort
5.1.3 Shell sort
5.1.4 Bubble sort
5.1.5 Quick sort
5.1.6 Merge sort
5.1.7 Radix sort
5.2 Searching:
5.2.1 Linear search
5.2.2 Binary Search
5.3 Hashing
5.4 Separate chaining
5.5 Open addressing
5.6 Rehashing
5.7 Extendible hashing
Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to arrange
data in a particular order. Most common orders are in numerical or lexicographical order.
The importance of sorting lies in the fact that data searching can be optimized to a very high level, if
data is stored in a sorted manner. Sorting is also used to represent data in more readable formats.
Following are some of the examples of sorting in real-life scenarios.
Telephone Directory the telephone directory stores the telephone numbers of people sorted by
their names, so that the names can be searched easily.
Dictionary the dictionary stores words in an alphabetical order so that searching of any word
becomes easy.
There are many types of Sorting techniques, differentiated by their efficiency and space requirements.
Following are some sorting techniques which we will be covering in next sections.
1. Bubble Sort
2. Insertion Sort
3. Selection Sort
4. Shell Sort
5. Quick Sort
6. Merge Sort
7. Radix Sort
UNIT 5 Page 1
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.
1. It has one of the simplest implementation
2. It is efficient for smaller data sets, but very inefficient for larger lists.
3. Insertion Sort is adaptive, that means it reduces its total number of steps if given a partially sorted
list, hence it increases its efficiency.
4. It is better than Selection Sort and Bubble Sort algorithms.
5. Its space complexity is less. Like Bubble Sorting, insertion sort also requires a single additional
memory space.
6. It is a sorting, as it does not change the relative order of elements with equal keys
It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.
It swaps 33 with 27. It also checks with all the elements of sorted sub-list. Here we see that the sorted sub-
list has only one element 14, and 27 is greater than 14. Hence, the sorted sub-list remains sorted after
swapping.
UNIT 5 Page 2
By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with 10.
So we swap them.
We swap them again. By the end of third iteration, we have a sorted sub-list of 4 items.
This process goes on until all the unsorted values are covered in a sorted sub-list.
#include <stdlib.h>
#include <iostream.h>
using namespace std;
//member functions declaration
void insertionSort(int arr[], int length);
void printArray(int array[],int size);
UNIT 5 Page 3
int main() {
int array[5]= {5,4,3,2,1};
insertionSort(array,5);
return 0;
}
void insertionSort(int arr[], int length) {
int i, j ,tmp;
for (i = 1; i < length; i++) {
j = i;
while (j > 0 && arr[j - 1] > arr[j]) {
tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
j--;
}
printArray(arr,5);
}
}
void printArray(int array[], int size){
cout<< "Sorting tha array using Insertion sort... ";
int j;
for (j=0; j < size;j++)
for (j=0; j < size;j++)
cout <<" "<< array[j];
cout << endl;
}
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.
Selection sort is an unstable sort i.e it might change the occurrence of two similar elements in the
list while sorting. But it can also be a stable sort when it is implemented using linked list data structures.
UNIT 5 Page 4