Data Structure 44 45
Data Structure 44 45
Set i=i+1
End while
Step 3: print no.of passes i-1
Step 4: end
Advantages of Insertion Sort:
It is simple sorting algorithm, in which the elements are sorted by considering one
item at a time. The implementation is simple.
It is efficient for smaller data set and for data set that has been substantially sorted
before.
It does not change the relative order of elements with equal keys
It reduces unnecessary travels through the array
It requires constant amount of extra memory space.
Disadvantages:-
It is less efficient on list containing more number of elements.
As the number of elements increases the performance of program would be slow
Complexity of Insertion Sort:
BEST CASE:-
Only one comparison is made in each pass.
The Time complexity is O(n2).
WORST CASE:- In the worst case i.e; if the list is arranged in descending order, the number
of comparisons required by the insertion sort is given by:
1+2+3+……………………….+(n-2)+(n-1)= (n*(n-1))/2;
= (n2-n)/2.
The number of Comparisons are O(n2).
AVERAGE CASE:- In average case the numer of comparisons is given by
42
J=j-1;
}
a[j]=t;
}
cout<<”Array after Insertion sort:”;
for(i=0;i<n;i++)
cout<”\n a[i]”;
getch();
}
OUTPUT:
Enter number of elements to sot:5
Enter number of elements to sorted: 7 33 20 11 6
Array after Insertion sort: 6 7 11 20 33.
QUICK SORT
The Quick Sort algorithm follows the principal of divide and Conquer. It first picks
up the partition element called ‘Pivot’, which divides the list into two sub lists such that all
the elements in the left sub list are smaller than pivot and all the elements in the right sub
list are greater than the pivot. The same process is applied on the left and right sub lists
separately. This process is repeated recursively until each sub list containing more than one
element.
Working of Quick Sort:
The main task in Quick Sort is to find the pivot that partitions the given list into two halves,
so that the pivot is placed at its appropriate position in the array. The choice of pivot as a
significant effect on the efficiency of Quick Sort algorithm. The simplest way is to choose the
first element as the Pivot. However the first element is not good choice, especially if the
given list is ordered or nearly ordered .For better efficiency the middle element can be
chosen as Pivot.
Initially three elements Pivot, Beg and End are taken, such that both Pivot and Beg refers to
0th position and End refers to the (n-1)th position in the list. The first pass terminates when
Pivot, Beg and End all refers to the same array element. This indicates that the Pivot
element is placed at its final position. The elements to the left of Pivot are smaller than this
element and the elements to it right are greater.
To understand the Quick Sort algorithm, consider an unsorted array as follows. The steps to
sort the values stored in the array in the ascending order using Quick Sort are given below.
8 33 6 21 4
Step 1: Initially the index ‘0’ in the list is chosen as Pivot and the index variable Beg and End
are initiated with index ‘0’ and (n-1) respectively.
43