Professional Documents
Culture Documents
Ch6 Sorting
Ch6 Sorting
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