Home » Data Structure & Algorithms » Sorting Algorithms

Sorting Algorithms

 

In this tutorial we are going to see some of the important sorting techniques.
1 : Bubble Sort
2 : Quick Sort
3 : Merge Sort

Bubble Sort :

Bubble sort or sinking sort  is a sorting algorithm  that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.

Algorithms : Bubble sort has worst-case and average complexity both O(n2), where n is the number of items being sorted.

Step 1: The 0th element is compared with the 1st element.If it is found greater than the 1st element ,they are swapped.

Step 2 : Then the 1st  element is compared with the 2nd  element.If it is found greater than the 1st element ,they are swapped.

Step 3 : In the same way all the elements are compared with their next element and they are swapped if required.This is the first iteration.On completing this iteration largest element is placed in the last position

Step 4 : Similarly in the second iteration the comparisons are made till last but one element and the second largest element is placed in the second last position. 

Step 5 : After all iterations elements are sorted.

Complexity :

Algorithm Worst Case Average Case Best Case
Bubble Sort O(n^2) O(n^2) O(n^2)

Question :
Given an array.Sort the array using bubble sort technique ?

Solution :
The problem is solved in iterative way.

Program :

 

 


Leave a comment

Your email address will not be published. Required fields are marked *

17 + twenty =