DS Unit-4
DS Unit-4
It is an Asymptotic notation used to determine the relationship between the input and time. Big O
Notation expresses the run time of an algorithm in terms of how quickly it grows relative to the input ‘n’ by
defining the N number of operations that are done on it. Thus, the time complexity of an algorithm is
denoted by the combination of all O[n] assigned for each line of function.
Common types of time complexities used
• Constant time – O (1)
• Logarithmic time – O (log n)
• Linear time – O (n)
• Log liner time – O (n log n)
• Exponential – O (2n)
Generally, Algorithms are analysed in three cases
• Best case
• Worst case
• Average case
Department of Computer Science, SSBN Degree College, ATP
1
Efficiency:
The selection sort has quadratic order (O (N2)) in best, average and worst cases. The selection
sort algorithm is suited for smaller lists and is not an efficient algorithm on larger lists.
Method:
public void selectionSort ()
{
int min;
for (int out=0; out<nElems-1; out++)
{
min=out;
for (int in=out+1; in<nElems; in++)
{
if(A[in]<A[min])
min=in;
}
swap (out, min);
}
}
public void swap(int one, int two)
{
long temp=A[one];
A[one]=A[two];
A[two]=temp;
}
Program:
//Program to illustrate Selection sort
import java.util.*;
class MyArray
{
long A []; int nElems;
public MyArray (int size)
{
A=new long[size];
nElems =0;
}
public void insert (long item)
{
A[nElems++] =item;
}
public void display ()
{
System.out.print("Array: [ ");
for (long item: A)
System.out.print(item+" ");
System.out.println("]");
}
Department of Computer Science, SSBN Degree College, ATP
3
Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands.
The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and
placed at the correct position in the sorted part.
Example2:
12, 11, 13, 5, 6
Let us loop for i = 1 (second element of the array) to 4 (last element of the array)
i = 1. Since 11 is smaller than 12, move 12 and insert 11 before 12
11, 12, 13, 5, 6
i = 2. 13 will remain at its position as all elements in A[0..I-1] are smaller than 13
11, 12, 13, 5, 6
i = 3. 5 will move to the beginning and all other elements from 11 to 13 will move one position ahead of
their current position.
5, 11, 12, 13, 6
i = 4. 6 will move to position after 5, and elements from 11 to 13 will move one position ahead of their
current position.
5, 6, 11, 12, 13
Algorithm:
Insertion Sort (A, MAX):
1. Repeat steps 2 to 5 varying out from 1 to MAX – 1
2. Set temp = A[out]
3. Set in = out
4. Repeat steps (a) and (b) until it becomes less than 0 or A[in] becomes less than or equal to
temp a. Shift the value at index in-1 to index in
b.Decrement in by 1
5. Store temp at index in
Method:
public void insertionSort ()
{
int out, in;
long temp;
for (out=1; out<nElems; out++)
{
temp=A[out];
in=out;
while(in>0&&A[in-1]>=temp)
{
A[in]=A[in-1];
in--;
}
A[in]=temp;
}
}
Efficiency:
The insertion sort has quadratic order (O(N2)) for average and worst cases but has linear order
(O(N)) for partially sorted lists (best case). The insertion sort is best suited for smaller lists only. It is an
efficient algorithm than bubble and selection sort when the list is nearly sorted.
Department of Computer Science, SSBN Degree College, ATP
6
Bubble Sorting:
Bubble sort is an Exchange sort algorithm. The basic idea of the bubble sort is to pass through the
list sequentially several times. Each pass consists of comparing each element in the file with its successor
and interchanging those two elements if they are not in proper order. The bubble sort always compares
the adjacent element and tries to place an element at its correct position.
E.g.:
X is an array of integers of which the first n are to be sorted so that x[i]<=x[j]for
0<=i<j<n. X = {25 57 48 37 12 92 86 33}
Comparisons made on the first pass:
X[0] with X[1] (25 with 57) No interchange
X[1] with X[2] (57 with 48) Interchange
X[2] with X[3] (57 with 37) Interchange
X[3] with X[4] (57 with 12) Interchange
X[4] with X[5] (57 with 92) No interchange
X[5] with X[6] (92 with 86) Interchange
X[6] with X[7] (92 with 33) Interchange
After the first pass,
X= {25 37 12 48 57 86 33 92}
The biggest element 92 in the list has found its correct position. Similarly, each iteration places an element
into its proper position.
Output:
Enter how many numbers are there: 5
Enter a number: 74
Enter a number: 15
Enter a number: 4
Enter a number: 65
Enter a number: 8
Before Sorting...
Array:[ 74 15 4 65 8 ]
After Sorting...
Array:[ 4 8 15 65 74 ]
Department of Computer Science, SSBN Degree College, ATP
11
• Applies insertion sort on each sub list to move the elements towards their correct positions. •
Helps an element to take a bigger step towards its correct position, thereby reducing the number of
comparisons.
h 3*h+1 (h–1)/3
14
4 13 1
13 40 4
40 121 13
121 364 40
364 1093 121
Note: we can also generate gap sequence by considering the number of elements ‘n’ in the list and
choosing the gap as follows.
In first iteration the gap (h) = n/2 for 1st pass and decrement h by h/2 for every iteration till
h becomes 1.
Algorithm:
1. Set h= MAX/2
2. Repeat steps 5 to 10 until h becomes less than or equal to zero.
3. Repeat steps 6 to 9 varying out from h to MAX – 1
4. Set temp = A[out]
5. Set in = out
6. Repeat steps (a) and (b) until in becomes less than h-1 or A[in-h] becomes less than or equal to
temp a) Shift the value at index in-h to index in
b) Decrement in by h (in=in-h)
7. Store temp at index in
8. Decrement h by h/2
Method 1:
public void shellSort()
{
int out, in, h=1;
long temp;
h=nElems/2;
while(h>0)
{
for(out=h; out<nElems; out++)
{
temp=A[out];
in=out;
while(in>h-1 && A[in-h]>=temp)
{
A[in]=A[in-h];
in=in-h;
}
A[in]=temp;
}
h=h/2;
}
}
Efficiency:
The Shell sort has log linear order (O(N log N)) for the best case and has O(N (log N)2)) for average
and worst cases. It is faster than bubble, selection, and insertion sorts but slower than quick sort.
Department of Computer Science, SSBN Degree College, ATP
13
Quick Sort:
Quick sort was discovered by CAR Hoare in 1962. This algorithm mainly works on partition algorithm.
Partition is the underlying mechanism of quick sort. Partition means dividing the data into two groups basing
on a criteria (key value) i.e., all elements with higher key value than the specified amount are placed in one
group and the rest with lower key value than the specified amount placed in another group. This algorithm
operates by partitioning a list in to two sub lists and then calling itself recursively to quick sort each of those
sub lists.
Quick sort works in three steps
1. Partition the array into left (smaller keys) and right (bigger keys) groups.
2. Call quick sort on left group.
3. Call quick sort on right group.
Department of Computer Science, SSBN Degree College, ATP
15
• The algorithm uses two pointers one at each end of the list.
• The pointer say, leftPtr moves from left towards right and the rightPtr moves from right towards the
left. • When the leftPtr encounters items smaller than the pivot it keeps going on because they are
already on correct side of the array. But when an element greater than pivot is found the pointer is
stopped. • Similarly, the rightPtr continues when bigger elements are found but stops when a small item
is found. • After finding the elements which are on wrong side of the arrays then those elements are
swapped. • After swapping again the pointers are continued to find the elements which are on wrong
side of array. • When the two pointers meet then the search is stopped and the pivot is restored to that
position. Now all elements left to the pivot are smaller than pivot and elements right to the pivot are
greater than pivot.
• Now the same process is continued on the two groups. This process continue until all the elements in
the list are processed.
Quick Sort Example:
Department of Computer Science, SSBN Degree College, ATP
16
Method:
public void quickSort()
{
recQuickSort(0,nElems-1);
}
public void recQuickSort(int left, int right)
{
if(right-left<=0)
return;
long pivot=A[right];
int partition=partitionIt(left,right,pivot);
recQuickSort(left,partition-1);
recQuickSort(partition+1,right);
Efficiency:
The quick sort has log linear order (O (N log N)) for best and average case and has quadratic order (O
(N2)) for worst case. Efficiency always depends up on the choosing the pivot element. Picking the median of
first, last and middle element of the list called as median-of-three approach is considered as the best way of
choosing the pivot.
swap(leftPtr,right);
return leftPtr;
}
}Output:
Enter how many numbers are there: 5
Enter a number: 74
Enter a number: 15
Enter a number: 4
Enter a number: 65
Enter a number: 8
Before Sorting... Array:[ 74 15 4 65 8 ]
After Sorting... Array:[ 4 8 15 65 74 ]
Department of Computer Science, SSBN Degree College, ATP
19
• Heap Property: All nodes are either [greater than or equal to] or [less than or equal to] each of its
children. If the parent nodes are greater than their children, heap is called a Max-Heap, and if the
parent nodes are smaller than their child nodes, heap is called Min-Heap.
Procedure:
The first step in heap sort is to create a Heap data structure (Max-Heap or Min-Heap). Once heap is built,
the first element of the Heap is either largest or smallest (depending upon Max-Heap or Min-Heap). That
first element of the heap is deleted and stored separately in an array. Then again heap is constructed with
the remaining elements, to again pick the first element of the heap and put it into the array. This process is
repeated until all the nodes are processed.
Algorithm:
Heap Sort (A, MAX)
The array ‘A’ of N elements can be sorted by this algorithm. First we build the heap and repeatedly delete
the root element. Since the root always contains the largest node, it deletes the elements in decending
order, Heap (A,N)
1. Repeat for j=1 to n-1 Call insertHeap(A,J,A[J+1])
2. Repeat while N>1Call deleteHeap(A, N, Item)
3. Set A (N+1)=Item
4. Exit
Inserting into a Heap:
Let H is a heap with N elements, and let us suppose that an ‘item’ of information is given, we insert item
into the heap H as follows.
1. First join ‘Item’ at the end of H so that H is still a complete tree, but not necessarily a
heap. 2. Then let Item rise to its suitable place in H so that H is finally a heap
Insertheap(Tree, N, Item)
1. Set N=N+1 & ptr=N
2. Repeat steps 3 to 6 while ptr<1
3. Set par=ptr/2
4. If item<Tree[par]
5. Set Tree[par]=Item & return Set Tree[ptr]=tree[par]
6. Set ptr=par
7. Tree[1]=Item
8. Return
Output:
Enter how many numbers are there: 5
Enter a number: 74
Enter a number: 15
Enter a number: 4
Enter a number: 65
Enter a number: 8
Before Sorting... Array:[ 74 15 4 65 8 ]
After Sorting... Array:[ 4 8 15 65 74 ]
Department of Computer Science, SSBN Degree College, ATP
23
Output:
Enter how many numbers are there: 5
Enter a number: 74
Enter a number: 45
Enter a number: 63
Enter a number: 15
Enter a number: 2
Elements in the array...
Array: [ 74 45 63 15 2 ]
Enter an element to search in the
Array: 63
Element is found at position: 3
Department of Computer Science, SSBN Degree College, ATP
26
Method:
public int binarySearch(long key)
{
int lower=0;
int upper=nElems-1;
while(true)
{
int pos=(lower+upper)/2;
if(A[pos]==key)
return (pos);
else if(lower>upper)
return -1;
else
{
if(A[pos]<key)
lower=pos+1;
else
upper=pos-1;
}
}
}
Example:
Efficiency:
Binary search has log linear order (O (N log N)) for worst case, logarithmic order (O (log N)) of average
case and has constant time (O (1)) for best case. Even though binary search is better than linear search but
it’s also important to note that for smaller values of N additional cost of sorting is probably not worth it.
Sometimes sorting larger list can be so expensive that linear search is best choice.
Program:
//Program to illustrate Binary Search
import java.util.*;
class MyArray
{
long A[];
int nElems;
public MyArray(int size)
{
A=new long[size];
nElems =0;
}
public void insert(long item)
{
A[nElems++]=item;
}
public void display()
{
System.out.print("Array:[ ");
for(long item: A)
System.out.print(item+" ");
System.out.println("]");
}
Department of Computer Science, SSBN Degree College, ATP
28
Output:
Enter how many numbers are there: 5
Enter a number: 74
Enter a number: 12
Enter a number: 56
Enter a number: 85
Enter a number: 2
Elements in the array...
Array:[ 2 12 56 74 85 ]
Enter an element to search in the Array: 56
Element is fount at position :3
Department of Computer Science, SSBN Degree College, ATP
30