Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Mr.

Abhijit Sarkar
Asst. Professor
Dept. of CSE (Cyber Security)
Haldia Institute of Technology
 Sorting is the technique of arranging the array elements in a specified order i.e. either in
ascending or descending order.

Before Sorting

8 5 9 7 1 2 3 11 58 4

After Sorting

1 2 3 4 5 7 8 9 11 58
 Each element is compared with its adjacent element.
 If the first element is larger than the second one then the position of the elements are
interchanged, otherwise, its not changed.
 Then next element is compared with its adjacent element and same process is repeated for
all elements.
 In first pass, largest element will be in the last position of array.
 In the next pass, same process is repeated with first n-1 elements, leaving the second largest
element in second last position of the array.
 Same process is repeated until no more elements are left for comparison.
Input n elements of an array a
Initialize i=0
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1]
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
Display the sorted elements of array a.
 Time Complexity:- O(n2)
A pass through the array is made to locate the minimum value.
Once this is found, it is placed in the first position of the array (position 0).
Then, the remaining elements is made to find the next smallest element, which is placed in the
second position (position 1) and so on.
Take n elements of an array a
for(i=0;i<n-1;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<=n-1;j++)
{
if(a[j]<min)
{
min=a[j];
loc=j;
}
}
if(loc!=i)
{
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
}
Display elements of ‘a’ after sorting Time Complexity:- O(n2)
 An insertion sort is one that sorts a set of values by inserting values into an existing sorted file.
 Insertion sort scan array ‘a’ from a[1] to a[n], inserting each element a[k] into its proper position in
the previously sorted subarray a[1],a[2],……,a[k-1].
 Let, array ‘a’ index range is from 0 to n-1.
 Set, k=1
 for k=1 to n-1 do
{
 temp=a[k];
 j=k-1;
 while((temp<a[j])&&(j>=0)) do
 {
 a[j+1]=a[j];
 j=j-1;
 }
 a[j+1]=temp;
 }
 Display elements of ‘a’ after sorting.
In worst case, [Array is sorted in descending order]
 Pass No. of comparisons No. of shifts Effective Operation
 1st 1 1 1+1=2
 2nd 2 2 2+2=4
 3rd 3 3 3+3=6
 n-1 n-1 n-1 2(n-1)
 Total Effective Operations=2+4+6+…..+2(n-1)=2[1+2+3+…+(n-1)]=2(n-1)*n/2=O(n2)
In best Case, [Sorted Array]
 Pass No. of comparisons No. of shifts Effective Operation
 1st 1 0 1
 2nd 1 0 1
 3rd 1 0 1
 n-1 1 0 1
 Total Effective Operations=1+1+1+…+(n-1) times=O(n-1)=O(n)
 At first one pivot element is chosen from the array.
 Array is partitioned into two subarray/partition such that one partition contains elements smaller
than pivot/key element and another partition contains elements greater than the pivot/key element.
 Same procedure is repeated for the two partitions and so on.
Recurrence Relation:
 T(n)=T(i-1)+T(n-i)+n T(n): Time required to sort array having n elements
T(i-1): Time required to sort array having i-1 elements
T(n-i): Time required to sort array having n-i elements
n: Time required to find the pivot element

 Worst Case: (When first element is the Pivot element) [i=1]


T(n)=T(n-1)+n [Putting i=1]
=T(n-2)+(n-1)+n
=T(n-3)+(n-2)+(n-1)+n
…………………………..
=T(n-i)+(n-i+1)+(n-i+2)+……+n (After i-times)
=T(n-n)+T(n-n+1)+T(n-n+2)+…….+n (Replacing i with n)
=1+2+3+…..+n
=n(n+1)/2
=n2/2+n/2
So, Time Complexity=O(n2) [Considering the dominating term)
Worst Case: (When last element is the Pivot element) [i=n]
 T(n)=T(n-1)+n [Putting i=n in T(n)=T(i-1)+T(n-i)+n]
 Solving this would return Time Complexity=O(n2) [Considering the dominating term)
Best Case: (When Middle element is the Pivot element) [i=n/2]
T(n)=T(n/2-1)+T(n-n/2)+n
=T(n/2)-T(1)+T(n/2)+n
=2T(n/2)+n
=2T(2m-1)+2m [assuming, n=2m]
=2[2T(2m-2)+2m-1]+2m
=22T(2m-2)+2.2m
……………………………………
= 2iT(2m-i)+i.2m [After i-times]
=2m-1T(2m-m+1)+(m-1).2m [Assuming i=m-1]
=2m-1+m.2m-2m
= n/2 + nlogn –n
So, time complexity=O(nlogn) [Considering the dominating term]
 Merge sort is the technique which divides the array into subarrays of size 2 and merge adjacent
pair. (n/2 arrays of size 2)
 At the time of merging elements, they are sorted.
 Merge process is continued/repeated until there is only one array remaining of size n.
T(n)=2T(n/2)+n T(n)=Time required to sort array having n elements
T(n/2)=Time required to sort array having n/2 elements
n=Time required to merge two sub-arrays containing total n elements

=2T(2m-1)+2m [Assuming, n=2m]


=2[2T(2m-2)+2m-1] +2m
=22T(2m-2)+2. 2m
…………………………..
= 2iT(2m-i)+i.2m [After i-times]
=2m-1T(2m-m+1)+(m-1).2m [Assuming i=m-1]
=2m-1+m.2m-2m
= n/2 + nlogn –n
So, time complexity=O(nlogn) [Considering the dominating term]
 A max-heap is formed from the given element values such that largest element stays at root node of
max-heap.
 Root node value is deleted and stored in the last available position of sorted array and last index
element is taken to root node having first index.
 Heap is adjusted to form max-heap again out of the n-1 elements.
 Process is repeated until heap is empty.
 Consider max-heap containing minimum number of nodes (height h):-

 Consider max-heap containing maximum number of nodes (height h):-


 So, height of max-heap is logn.
 Height is traversed for all n number of elements/nodes one time during max-heap
formation and one time during adjustment operation.
 So, effective operation is 2nlogn
 Time complexity of Heap Sort is O(nlogn).

You might also like