You are on page 1of 10

Bubble Sort

Its a sorting algorithm, in which each pair of adjacent items are compared and
swapped if they are in wrong order. The comparison is repeated until no swaps are
needed, indicating that the list is sorted. The smaller elements bubble to the top of
the list, hence, the name Bubble Sort. In this like selection sort, after every pass the
largest element moves to the highest index position of the list.
Algorithm:
It starts with the first element of the list. The comparison of the adjacent pair of
elements and swapping is done until the list is sorted as follows
1. Compare two adjacent elements and check if they are in correct order (that is
second one has to be greater than the first).
2. Swap them if they are not in correct order.

Let iter denotes the number of iterations, then for iter ranging from 1 to n-1 (where n
is total number of elements in the list) check
1. If value of the second item at position iter is lesser than the value of the first item
i.e. at position iter-1, then swap them.
2. Else, move to the next element at position iter+1.
The effective size of the list is hence reduced by one after every pass and the
largest element is moved to its final position in the sorted array. Repeat the two steps
until the list is sorted and no swap is required i.e. the effective size is reduced to 1.

To see a Java Applet Visualization of the Bubble Sort Mechanism, click on the
image below :



Properties:

1. Best Case performance When the list is already sorted, we require only one pass
to check, hence O(n).
2. Worst Case performance When the list is in the reverse order, n number of
comparisons and swap are required to be done n number of times, hence O(n2).
3. Average Case performance O(n2)
4. It does not require any extra space for sorting, hence O(1) extra space.

Insertion sort
Insertion sort uses linear search to find the location of the 1st element in the unsorted list, in
the sorted portion of the list. It is an elementary sorting algorithm best used to sort
small data sets or insert a new element in the sorted list.

Algorithm:
Insertion sort starts with a sorted list of size 1 and inserts elements one at a time. It
continues inserting each successive element into the sorted list so far.
1. Suppose if the array is sorted till index i then we can sort the array till i+1 by inserting the
(i+1)th element in the correct position from 0 to i+1.
2. The position at which (i+1)th element has to be inserted has to be found by iterating from 0 to
i.
3. As any array is sorted till 0th position (Single element is always sorted) and we know how to
expand, we can sort the whole array.

Properties:
1. Best case performance When the array is already sorted O(n). Total number
of comparisons: N 1 and total number of exchanges: N 1.
2. Worst case performance When the array is sorted in reverse order O(n2). N-1 iterations of
comparison and exchanges.
3. Average case performance O(n2)
4. It is sensitive to the input as the number of comparison and exchanges depends on the input.
5. It does not require any extra space for sorting, hence O(1) extra space.

Selection Sort
The idea of the selection sort is to find the smallest element in the list and exchange it with
the element in the first position. Then, find the second smallest element and exchange it with
the element in the second position, and so on until the entire array is sorted.

Algorithm:
For every index from 0 to number_of_elements-1, we find the element which is appropriate
for that index and we swap the element which is already there with the element which has to
be there. Finding the element which is appropriate for an index is simple. We just have to find
the minimum value which is there from that index till number_of_elements-1.
1. minIndex denotes the index which has the minimum value which for now assumed to be
the value at current index and we update it in a for loop.
2. For elements from minIndex+1 to the last element of the list check if some index has got
element smaller than minimum then update minindex to be that index.
3. Then swap the two elements i.e. the element at minidex in 1 and the element at the
updated minindex.
4. Follow the first three steps for every index from 0 to number_of_elements-1.


To Check out a Java Applet Visualization of Selection Sort, click on the image below :


Properties:
Best case performance When the list is already sorted O(n2).
Worst case performance - When the list is sorted in reverse order O(n2).
Average case performance O(n2).
It does not require any extra space for sorting, hence O(1) extra space.
It is not stable.
O(n2) time complexity makes it difficult for long lists.

Merge Sort



Merge Sort
Merge sort is based on Divide and conquer method. It takes the list to be sorted and divide it in
half to create two unsorted lists. The two unsorted lists are then sorted and merged to get a sorted
list. The two unsorted lists are sorted by continually calling the merge-sort algorithm;
we eventually get a list of size 1 which is already sorted. The two lists of size 1 are then merged.
Algorithm:
This is a divide and conquer algorithm. This works as follows

1. Divide the input which we have to sort into two parts in the middle. Call it the left part and
right part.
Example: Say the input is -10 32 45 -78 91 1 0 -16 then the left part will be -10 32 45 -
78 and the right part will be 91 1 0 6.
2. Sort each of them separately. Note that here sort does not mean to sort it using some
other method. We use the same function recursively.
3. Then merge the two sorted parts.

Input the total number of elements that are there in an array (number_of_elements). Input
the array (array[number_of_elements]). Then call the function MergeSort() to sort the input
array. MergeSort() function sorts the array in the range [left,right] i.e. from index left to index
right inclusive. Merge() function merges the two sorted parts. Sorted parts will be from [left,
mid] and [mid+1, right]. After merging output the sorted array.

MergeSort() function:

It takes the array, left-most and right-most index of the array to be sorted as arguments. Middle
index (mid) of the array is calculated as (left + right)/2. Check if (left<right) cause we have to
sort only when left<right because when left=right it is anyhow sorted. Sort the left part by calling
MergeSort() function again over the left part MergeSort(array,left,mid) and the right part by
recursive call of MergeSort function as MergeSort(array,mid + 1, right). Lastly merge the two
arrays using the Merge function.

Merge() function: (Explained in greater detail within the tutorial document)

It takes the array, left-most , middle and right-most index of the array to be merged
as arguments.
Finally copy back the sorted array to the original array.
Properties:
Best case When the array is already sorted O(nlogn).
Worst case When the array is sorted in reverse order O(nlogn).
Average case O(nlogn).
Extra space is required, so space complexity is O(n) for arrays and O(logn) for linked lists.

To go through the C program / source-code, scroll down to the end of this page
Quick Sort

Quick Sort is divide and conquer algorithm like Merge Sort. Unlike Merge Sort this does
not require extra space. So it sorts in place. Here dividing step is to chose a pivot and
partition the array such that all elements less than or equal to pivot are to the left of it
and all the elements which are greater than or equal to the pivot are to the right of it.
Recursively sort the left and right parts.

Algorithm (described in detail in the document for this tutorial)

The key to the algorithm is the partition procedure.
A 'partition' element is chosen. All elements less than the partition are put in the left half of the
array, all elements greater than the partition are placed in the right half of the array.
The two halves are sorted independently and recursively.


Property:

1. Best case performance When the partitioning produces two regions of size n/2 (where,
n is the total number of elements in the list) O(nlgn).
2. Worst case performance - When the partitioning produces one region of size n-1 (where,
n is the total number of elements in the list) and other of size 1 O(n2).
3. Average case O(n(lgn))
4. It is not stable and uses O(lg(n)) extra space in the worst case.

Binary Search Algorithm
Search as the name suggests, is an operation of finding an item from the given collection
of items. Binary Search algorithm is used to find the position of a specified value (an Input
Key) given by the user in a sorted list.
Algorithm:
It starts with the middle element of the list.

1. If the middle element of the list is equal to the input key then we have found the position the
specified value.
2. Else if the input key is greater than the middle element then the input key has to be present
in the last half of the list.
3. Or if the input key is lesser than the middle element then the input key has to be present in
the first half of the list.

Hence, the search list gets reduced by half after each iteration.

First, the list has to be sorted in non-decreasing order.

[low, high] denotes the range in which element has to be present and [mid] denotes the
middle element. Initially low = 0, high = number_of_elements and mid =
floor((low+high)/2). In every iteration we reduce the range by doing the following
until low is less than or equal to high(meaning elements are left in the array) or the
position of the input key has been found.

(i) If the middle element (mid) is less than key then key has to present in range [mid+1 , high], so
low=mid+1, high remains unchanged and mid is adjusted accordingly
(ii) If middle element is the key, then we are done.
(iii) If the middle element is greater than key then key has to be present in the range [low,mid-1],
so high=mid-1, low remains unchanged and mid is adjusted accordingly.
Properties:
1. Best Case performance The middle element is equal to the input key O(1).
2. Worst Case performance - The input key is not present in the list O(logn).
3. Average Case performance The input key is present, but its not the middle element
O(logn).
4. The List should be sorted for using Binary Search Algorithm.
5. It is faster than Linear Search algorithm, and its performance increases in comaparison to
linear search as N grows.

You might also like