Sorting Algorithms
Sorting Algorithms
1. In Pass 1, A[0] is compared with A[1], A[1] is compared with A[2], A[2]
is compared with A[3] and so on. At the end of pass 1, the largest element
of the list is placed at the highest index of the list.
2. In Pass 2, A[0] is compared with A[1], A[1] is compared with A[2] and
so on. At the end of Pass 2 the second largest element of the list is placed
at the second highest index of the list.
3. In pass n-1, A[0] is compared with A[1], A[1] is compared with A[2] and
so on. At the end of this pass. The smallest element of the list is placed at
the first index of the list.
Output :
We can see that to sort an array of size 5 it takes 9.822 sec, and also to
sort an array of size 6 it takes 10.45 sec and also to sort an array of
size 7 takes 11.82 seconds .
3.)Merge sort :
Merge sort keeps on dividing the list into equal halves until it can no more be
divided. By definition, if it is only one element in the list, it is sorted. Then,
merge sort combines the smaller sorted lists keeping the new list sorted too.
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more be
divided.
Step 3 − merge the smaller lists into new list in sorted order.
Output :
We can see that to sort an array of size 5 it takes 7.394 sec, and also to sort an
array of size 6 it takes 9.315 sec and also to sort an array of size 7 takes 13.67
seconds .