Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Sorting Algorithms

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Sorting Algorithms

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Sorting algorithms

1.) Bubble sort :


The elements of the array are compared with each other in Bubble sort. The list
is processed in passes by the algorithm. Sorting a list with n elements requires
n-1 passes. Consider an array A with n elements whose elements are to be
sorted using Bubble sort. The algorithm works as follows.

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 .

2.) Insesrtion sort :


It is a simple sorting algorithm similar to how you sort playing cards in your
hands. Arrays are practically split into sorted and unsorted parts unsorted part.
Values from the unsorted part are picked and placed at the correct position in
the sorted part.
Algorithm
To sort an array of size n in ascending order:
1: Iterate from arr[1] to arr[n] over the array.
2: Compare the current element (key) to its predecessor.
3: If the key element is smaller than its predecessor, compare it to the
elements before. Move the greater elements one position up to make space
for the swapped element.
Output :
We can see that to sort an array of size 5 it takes 7.467 sec, and also to sort an
array of size 6 it takes 8.302 sec and also to sort an array of size 7 takes 13.09
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 .

You might also like