Data Structures unit-5 KHK
Explain in detail about sorting and different types of sorting techniques
Sorting is a technique to rearrange the elements of a list in ascending or descending order, which
can be numerical, or any user-defined order. Sorting can be classified in two types;
Internal Sorts
This method uses only the primary memory during sorting process. All data items are held in
main memory and no secondary memory is required this sorting process. If all the data that is
to be sorted can be accommodated at a time in memory is called internal sorting. There is a
limitation for internal sorts; they can only process relatively small lists due to memory
constraints. There are 3 types of internal sorts.
(i) SELECTION SORT :- Ex:- Selection sort algorithm, Heap Sort algorithm
(ii) INSERTION SORT :- Ex:- Insertion sort algorithm, Shell Sort algorithm
(iii) EXCHANGE SORT :- Ex:- Bubble Sort Algorithm, Quick sort algorithm
External Sorts
Sorting large amount of data requires external or secondary memory. This process uses
external memory such as HDD, to store the data which is not fit into the main memory. So,
primary memory holds the currently being sorted data only. All external sorts are based on
process of merging. Different parts of data are sorted separately and merged together.
Ex:- Merge Sort
Write and explain linear search procedure or algorithm with a suitable
example.
Linear search technique is also known as sequential search technique. This is the
simplest of all searching techniques. In this technique, an ordered or unordered list will
be searched one by one from the beginning until the desired element is found. If the
desired element is found in the list then the search is successful otherwise unsuccessful.
Suppose there are „n‟ elements organized sequentially on a List. The number of
comparisons required to retrieve an element from the list, purely depends on where the
element is stored in the list. If it is the first element, one comparison will do; if it is
second element two comparisons are necessary and so on. On an average you need
Vasavi Degree College 1
Data Structures unit-5 KHK
[(n+1)/2] comparison‟s to search an element. If search is not successful, you would need
‟n‟ comparisons. The time complexity of linear search is O(n).
Example:
Let us illustrate linear search on the following 9 elements:
Searching different elements is as follows:
Searching for x = 7 Search successful, data found at 3rd position.
Searching for x = 82 Search successful, data found at 7th position.
Searching for x = 42 Search un-successful, data not found.
Algorithm:
Step 1:
set-up a flag to indicate “element not found”
Step 2:
Take the first element in the list
Step 3:
If the element in the list is equal to the desired element
Set flag to “element found”
Display the message “element found in the list”
Go to step 6
Step 4:
If it is not the end of list,
Take the next element in the list
Go to step 3
Step 5:
If the flag is “element not found”
Display the message “element not found”
Step 6:
End of the Algorithm
Vasavi Degree College 2
Data Structures unit-5 KHK
Advantages:
It is simple and conventional method of searching data. The linear or sequential name
implies that the items are stored in a systematic manner.
The elements in the list can be in any order. i.e. The linear search can be applied on
sorted or unsorted linear data structure.
As the linear search does not require the list to be sorted, additional elements can be
added and deleted i.e. no need to reorder the list after insertions or deletions.
Disadvantage:
This method is insufficient when large number of elements is present in list.
It consumes more time and reduces the retrieval rate of the system.
Example:
import java.util.*;
public class linearsearch {
public static void main(String[ ] args) {
int a[ ] ={10,-1,28,13,44,5,36,97,-18,11};
Scanner sc= new Scanner(System.in);
System.out.println(" The contents of the Array are :");
for(int x: a)
System.out.print(x+" ");
System.out.println("enter search element ");
int s = sc.nextInt();
int f = -1;
for (int i =0;i <a.length; i+ +)
{
if(a[i]==i)
{
f = i;
break;
}
Vasavi Degree College 3
Data Structures unit-5 KHK
}
if (f != -1) {
System.out.println(" The search element is : " + s);
System.out.println(" It is found in the list at position : " + (f+1) );
}
else
System.out.println("\n The search element is not found in the array.");
}
}
Formulate algorithm for Binary Search.
Binary search follows divide and conquer approach in which the list is divided into two
halves and the search item is compared with the middle element of the list. If the match is
found then the location of middle element is returned otherwise we search into either of the
halves depending upon the result produced through the match.
If the search item is in first half, we do not need to check the second half and if it is in
second half no need to check in first half. Similarly we repeat this process until we find the
search item in the list or not found from the list
To implement binary search method, the elements in the list must be in sorted order. It cannot
be applied on unsorted list. Binary Search is performed as follows:
The key is compared with item in the middle position of an array
If the key matches with item, return it and stop
If the key is less than mid positioned item, then the item to be found must be in
first half of array, otherwise it must be in second half of array.
Repeat the procedure for lower (or upper half) of array until the element is found.
Example :
Let us illustrate binary search on the following 10 elements:
index 0 1 2 3 4 5 6 7 8 9
Elements 12 22 34 43 48 52 61 72 85 89
Vasavi Degree College 4
Data Structures unit-5 KHK
If we are searching for x = 52
low = 0, high = 9, mid = (0+9)/2=4 a[mid] =48 ( 52>48)
low = mid+1= 5, high = 9 , mid = (5+9)/2 = 7 a[mid]= 72 (52<72)
low = 5, high = mid-1(7-1)=6 , mid = (5+6)/2=5 a[mid], a[5] = 52 FOUND
in the above example 52 is found at 5th index of the list.
If we are searching for x = 95
low = 0, high = 9, mid = (0+9)/2=4 a[mid] =48 ( 95>48)
low = mid+1= 5, high = 9 , mid = (5+9)/2 = 7 a[mid]= 72 (95>72)
low = mid+1 (7+1) =8, high =9 , mid = (8+9)/2=8 a[mid], a[8] =85 (95> 85)
low = mid+1 (8+1) =9, high =9 , mid = (9+9)/2=9 a[mid], a[9] =89 (95> 89)
low = mid+1 (9+1) =10 high =9 , 95 NOT FOUND
Since “low” is greater than the “high” it means that search element (95) is not found in the list.
Algorithm:
BINARY_SEARCH(A, lower_bound, upper_bound, VAL)
Step 1:
[INITIALIZE] set LOW = lower_bound
HIGH = upper_bound, POS = - 1
Step 2:
Repeat Steps 3 and 4 while LOW <= HIGH
Step 3:
SET MID = (LOW + HIGH)/2
Step 4:
if A[MID] = =VAL
set POS = MID
PRINT “Element FOUND at “POS”
Go to Step 6
else if ( A[MID] > VAL)
set HIGH = MID - 1
else
Vasavi Degree College 5
Data Structures unit-5 KHK
set LOW = MID + 1
[END OF IF]
[END OF LOOP]
Step 5:
if POS = -1
PRINT "VALUE IS NOT FOUND IN THE ARRAY"
Step 6:
EXIT
Time Complexity:
The time complexity of binary search in a successful search is O(log n) and for an
unsuccessful search is O(log n).
Advantages:
Compared to linear search, binary search is much faster. Linear search takes, on average
N/2 comparisons (where N is the number of elements in the array), and worst case N
comparisons. Binary search takes an average and worst-case log2(N) comparisons. So for
a million elements, linear search would take an average of 500,000 comparisons, whereas
binary search would take 20.
It‟s a fairly simple algorithm.
Disadvantages:
It works efficiently on large no of list items.
It works only on lists that are sorted and kept sorted.
Not always feasible, especially when the elements are constantly being added to the list.
Example:
import java.util.*;
class binarysearch {
public static void main(String[ ] args) {
int[ ] arr = {2, 4, 6, 8, 10, 12, 14, 16};
int lb= 0;
int ub = arr.length - 1;
int mid , key;
Vasavi Degree College 6
Data Structures unit-5 KHK
Scanner sc = new Scanner(System.in);
System.out.println("List of elements ");
for(int x: arr)
System.out.print(x+" ");
System.out.println("\nenter search element ");
key=sc.nextInt();
while (lb <= ub) {
mid = (lb + ub) / 2;
if (key == arr[mid]) {
System.out.println("the element is found at "+(mid+1) +" position");
break;
}
if (key <arr[mid])
ub = mid - 1;
else
lb = mid + 1;
}
if(lb>ub)
System.out.println("element is not found in the list");
}
}
Explain the algorithm for insertion sort and give a suitable Example.
In insertion sort the element is inserted at an appropriate place similar to card insertion.
Here the list is divided into two parts sorted and unsorted sub-lists. In each pass, the first
element of unsorted sub list is picked up and moved into the sorted sub list by inserting it
in suitable position. Suppose we have „n‟ elements, we need n-1 passes to sort the
elements.
Vasavi Degree College 7
Data Structures unit-5 KHK
Insertion sort works this way:
Step 1:
Assume that first element in the list is in sorted portion of the list and remaining
all elements are in unsorted portion.
Step 2:
Consider first element from the unsorted list and insert that element into the
sorted list in order specified.
Step 3:
Repeat the above process until all the elements from the unsorted list are moved
into the sorted list.
Algorithm
Step 1 − If it is the first element, it is already sorted. return 1;
Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is greater than the
value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted
Pseudo code
INSERTION_SORT (A)
for i = 1 to n
key ← A [i]
j←i–1
while j > = 0 and A[j] > key
A[j+1] ← A[j]
j←j–1
End while
A[j+1] ← key
End for
Vasavi Degree College 8
Data Structures unit-5 KHK
Advantages of Insertion Sort:
It is very simple.
It is very efficient for small data sets.
It is stable; i.e., it does not change the relative order of elements with equal keys.
In-place; i.e., only requires a constant amount O(1) of additional memory space.
Disadvantages
It is less efficient on list containing more number of elements.
As the number of elements increases the performance of the program would be slow.
Insertion sort needs a large number of element shifts.
A list of unsorted elements are: 4,3,2,10,12,1,5,6 .
The results of insertion sort for each pass is as follows:-
A list of sorted elements now : 1,2,3,4,5,6,10,12
Vasavi Degree College 9
Data Structures unit-5 KHK
Complexity:
To sort a unsorted list with 'n' number of elements we need to make
(1+2+3+......+n-1) = (n (n-1))/2 number of comparisons in the worst case. If the list
already sorted, then it requires 'n' number of comparisons.
Worst Case Time Complexity [ Big-O ] : O(n2)
Best Case Time Complexity [Big-omega] : Ω(n)
Average Time Complexity [Big-theta]: : Θ(n2)
Space Complexity: O(1)
Example:
public class InsertionSort {
public static void main(String a[]){
int[] arr = {9,14,3,2,43,11,58,22};
int n,key,j,i ;
System.out.println("List Before Sorting");
for(int x:arr){
System.out.print(x+" ");
}
System.out.println();
n = arr.length;
for ( j = 1; j < n; j++) {
key = arr[j];
i = j-1;
while ( (i > -1) && ( arr [i] > key ) ) {
arr [i+1] = arr [i];
i--;
}
arr[i+1] = key;
}
System.out.println("List After Sorting");
for(int x:arr){
Vasavi Degree College 10
Data Structures unit-5 KHK
System.out.print(x+" ");
}
}
}
Explain the algorithm for Bubble sort and give a suitable example.
In bubble sort method the list is divided into two sub-lists sorted and unsorted. The
smallest element is bubbled from unsorted sub-list. After moving the smallest element the
imaginary wall moves one element ahead. The bubble sort was originally written to
bubble up the highest element in the list. But there is no difference whether highest /
lowest element is bubbled. This method is easy to understand but time consuming. In this
type, two successive elements are compared and swapping is done. Thus, step-by-step
entire array elements are checked. Given a list of „n‟ elements the bubble sort requires up
to n-1 passes to sort the data.
Algorithm for Bubble Sort:
1. Compare each pair of adjacent elements from the beginning of an array and, if they are in
reversed order, swap them.
2. If at least one swap has been done, repeat step 1.
Bubble_Sort ( A [ ] , N )
Step 1 :
Repeat For P = 1 to N – 1 Begin
Step 2 :
Repeat For J = 1 to N – P Begin
Step 3 :
Vasavi Degree College 11
Data Structures unit-5 KHK
If ( A [ J ] < A [ J – 1 ] )
Swap ( A [ J ] , A [ J – 1 ] ) End For
End For
Step 4 :
Exit
A list of unsorted elements are: 10 47 12 54 19 23
(Bubble up for highest value shown here)
Time Complexity:
The bubble sort method of sorting an array of size n requires (n-1) passes and (n-1)
comparisons on each pass. Thus the total number of comparisons is (n-1) * (n-1) = n2 –
2n + 1, which is O(n2). Therefore bubble sort is very inefficient when there are more
elements to sorting.
Advantages:
It is popular and easy to implement.
In the bubble sort, elements are swapped in place without using additional temporary
storage, so the space requirement is at a minimum
Disadvantages:
The main disadvantage of the bubble sort is the fact that it does not deal well with a list
containing a huge number of items.
Vasavi Degree College 12
Data Structures unit-5 KHK
Example:
class Bsort{
public static void main(String [] args){
int a[] = { 50 , 30,10,40,20};
int i,j, swap=0,t;
System.out.println("List before sorting ");
for(int x : a)
System.out.print (x + " ");
for(i=0 ; i< a.length-1 ; i++) {
swap=0;
for(j=0 ; j<a.length-1-i; j++) {
if(a[j]> a[j+1]) {
t=a[j];
a[j]= a[j+1];
a[j+1]= t;
swap=1;
}
}
if(swap==0)
break;
}
System.out.println("\n List after sorting ");
for(int x: a)
System.out.print(x+" ");
}
}
Vasavi Degree College 13
Data Structures unit-5 KHK
Explain the algorithm for SELECTION sort and give a suitable example.
Selection sort is conceptually the most simplest sorting algorithm. This algorithm will
first find the smallest element in the array and swap it with the element in the first
position, then it will find the second smallest element and swap it with the element in the
second position, and it will keep on doing this until the entire array is sorted.
It is called selection sort because it repeatedly selects the next-smallest element and
swaps it into the right place.
Working:
Following are the steps involved in selection sort(for sorting a given array in ascending order):
1. Starting from the first element, we search the smallest element in the array, and replace it
with the element in the first position.
2. We then move on to the second position, and look for smallest element present in the
subarray, starting from index 1, till the last index.
3. We replace the element at the second position in the original array, or we can say at the
first position in the subarray, with the second smallest element.
4. This is repeated, until the array is completely sorted.
Algorithm:
The idea of algorithm is quite simple. Array is imaginary divided into two parts - sorted
one and unsorted one. At the beginning, sorted part is empty, while unsorted
one contains whole array.
Selection_Sort ( A [ ] , N )
Step 1 :
Repeat For K = 0 to N – 2 Begin
Step 2 :
Set POS = K
Step 3 :
Repeat for J = K + 1 to N – 1 Begin
If A[ J ] < A [ POS ]
Set POS = J
End For
Step 4 :
Vasavi Degree College 14
Data Structures unit-5 KHK
Swap A [ K ] with A [ POS ]
End For
Step 5 : Exit
A list of unsorted elements are: 23 78 45 8 32 56
A list of sorted elements now : 8 23 32 45 56 78
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 work as a stable sort when it is implemented using linked list.
Example:
public class SelectionSort{
public static void main(String a[]){
int[] arr = {9,14,3,2,43,11,58,22};
int index,i,j;
System.out.println("List Before Sorting ");
for(int x:arr){
System.out.print(x+" ");
}
System.out.println();
for (i = 0; i < arr.length - 1; i++) {
index = i;
Vasavi Degree College 15
Data Structures unit-5 KHK
for (j = i + 1; j < arr.length; j++){
if (arr[j] < arr[index]){
index = j; //searching for lowest index
}
}
if(index!=i)
{
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}
System.out.println("After Selection Sort");
for(int x:arr){
System.out.print(x+" ");
}
}
}
Explain the algorithm for QUICK sort ( partition exchange sort)
and give a suitable example.
Quick Sort is also based on the concept of “Divide and Conquer”, just like merge sort.
But in quick sort all the major work is done while dividing the array into subarrays, while
in case of merge sort, all the real work happens during merging the subarrays. It is also
called partition-exchange sort. This algorithm divides the list into three main parts:
Elements less than the Pivot element
Pivot element(Central element)
Elements greater than the pivot element
Pivot element can be any element from the array, it can be the first element, the last
element or any random element
Vasavi Degree College 16
Data Structures unit-5 KHK
Working
Following are the steps involved in quick sort algorithm:
After selecting an element as pivot, we divide the array for the first time.
In quick sort, we call this partitioning. In partitioning, the array elements are so
positioned that all the elements smaller than the pivot will be on the left side of the pivot
and all the elements greater than the pivot will be on the right side of it. And the pivot
element will be at its final sorted position.
The elements to the left and right, may not be sorted.
Then we pick subarrays, elements on the left of pivot and elements on the right of pivot,
and we perform partitioning on them by choosing a pivot in the subarrays.
Ex:- A list of unsorted elements are: 8 3 2 11 5 14 0 2 9 4 20
Algorithm for quick sort:
quicksort(q)
varlist less, pivotList, greater
if length(q) ≤ 1
return q
Vasavi Degree College 17
Data Structures unit-5 KHK
select a pivot value pivot from q
for each x in q except the pivot element
if x < pivot then add x to less
if x >= pivot then add x to greater
add pivot to pivotList
return concatenate(quicksort(less), pivotList, quicksort(greater))
Time Complexity of Quick sort:
Best case : O (n log n)
Average case : O (n log n)
Worst case : O (n2)
Advantages of quick sort
This is faster sorting method among all.
Its efficiency is also relatively good.
It requires relatively small amount of memory.
Disadvantages of quick sort:
It is complex method of sorting so, it is little hard to implement than other sorting
methods.
Example:
public class MyQuickSort {
private int array[ ];
private int length;
public void sort (int[ ] inputArr) {
if (inputArr == null || inputArr.length == 0) {
return;
}
this.array = inputArr;
length = inputArr.length;
quickSort(0, length - 1);
}
private void quickSort(int lowerIndex, int higherIndex) {
Vasavi Degree College 18
Data Structures unit-5 KHK
int i = lowerIndex;
int j = higherIndex;
int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
exchangeNumbers(i, j);
i++;
j--;
}
}
if (lowerIndex < j)
quickSort(lowerIndex, j);
if (i < higherIndex)
quickSort(i, higherIndex);
}
private void exchangeNumbers(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String a[ ]){
MyQuickSort sorter = new MyQuickSort();
int[] arr = {24,2,45,20,56,75,2,56,99,53,12};
System.out.println("List before sort ");
Vasavi Degree College 19
Data Structures unit-5 KHK
for(int x :arr)
System.out.print(x+" ");
sorter.sort(arr);
System.out.println("\nList after sort");
for(int x:arr){
System.out.print(x+ " ");
System.out.print(" ");
}
}
}
Explain the algorithm for Merge sort and give a suitable example.
The basic concept of merge sort is divides the list into two smaller sub-lists of
approximately equal size. Recursively repeat this procedure till only one element is left in
the sub-list. After this, various sorted sub-lists are merged to form sorted parent list. This
process goes on recursively till the original sorted list arrived.
Algorithm for merge sort:
Merge sort is based on the divide-and-conquer paradigm. Its worst-case running time has
a lower order of growth than insertion sort. Since we are dealing with sub-problems, we
state each sub-problem as sorting a sub-array A[p .. r]. Initially, p = 1 and r = n, but these
values change as we recurse through sub-problems.
To sort A[p .. r]:
1. Divide Step
If a given array A has zero or one element, simply return; it is already sorted. Otherwise,
split A[p .. r] into two sub-arrays A[p .. q] and A[q + 1 .. r], each containing about half of
the elements of A[p .. r]. That is, q is the halfway point of A[p .. r].
2. Conquer Step
Conquer by recursively sorting the two sub-arrays A[p .. q] and A[q + 1 .. r].
Vasavi Degree College 20
Data Structures unit-5 KHK
3. Combine Step
Combine the elements back in A[p .. r] by merging the two sorted sub-arrays A[p .. q] and
A[q + 1 .. r] into a sorted sequence. To accomplish this step, we will define a procedure
MERGE (A, p, q, r).
Note that the recursion bottoms out when the sub-array has just one element, so that it is sorted.
To sort the entire sequence A[1 .. n], make the initial call to the procedure MERGE-SORT (A, 1,
n).
Properties
It is a Stable algorithm i.e does not change the relative order of elements with equal keys
It does not require random access to data
Time Complexity of merge sort:
Best case : O (n log n)
Average case : O (n log n)
Worst case : O (n log n)
Vasavi Degree College 21
Data Structures unit-5 KHK
Example:
public class MyMergeSort {
private int[ ] array;
private int[ ] tempMergArr;
private int length;
public static void main(String a[ ]){
int[ ] inputArr = {45,23,11,89,77,98,4,28,65,43};
MyMergeSort mms = new MyMergeSort( );
System.out.println("Before sorting ");
for(int i:inputArr)
System.out.print(i +" ");
mms.sort(inputArr);
System.out.println("\nAfter sorting ");
for(int i:inputArr)
System.out.print(i +" ");
}
public void sort(int inputArr[ ]) {
this.array = inputArr;
this.length = inputArr.length;
this.tempMergArr = new int[length];
doMergeSort(0, length - 1);
}
private void doMergeSort(int lowerIndex, int higherIndex) {
if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
// Below step sorts the left side of the array
doMergeSort(lowerIndex, middle);
// Below step sorts the right side of the array
doMergeSort(middle + 1, higherIndex);
// Now merge both sides
Vasavi Degree College 22
Data Structures unit-5 KHK
mergeParts(lowerIndex, middle, higherIndex);
}
}
private void mergeParts(int lowerIndex, int middle, int higherIndex) {
for (int i = lowerIndex; i <= higherIndex; i++) {
tempMergArr[i] = array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex) {
if (tempMergArr[i] <= tempMergArr[j]) {
array[k] = tempMergArr[i];
i++;
}
else {
array[k] = tempMergArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempMergArr[i];
k++;
i++;
}
}
}
Vasavi Degree College 23