4x
Swaps : 0
Comparisons : 0

Bubble Sort

Algorithm

DESCRIPTION :
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.

BUBBLE-SORT(A, n) :
1. for j = n to 1
2.   for i = 1 to p-1 
3.       if A[i] > A[i+1] 
4.           temp = A[i]
5.           A[i] = A[i+1] 
6.           A[i+1] = temp
                        
Time Complexity

Average Case : Θ(n^n)

Best Case : Ω(n)

Worst Case : O(n^n)

Space Complexity
OverAll Space Complexity
=
Auxiliary Space + Space taken by Input


Auxiliary Space is the extra space or temporary space used by an algorithm.

Auxiliary Space : O(1)
Space Taken by Input : O(n)
OverAll Space Complexity : O(n)

Selection Sort

Algorithm

DESCRIPTION :
The Selection Sort algorithm sorts an array by repeatedly finding the minimun element from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element from the unsorted subarray is picked and moved to the sorted subarray.

SELECTION-SORT(A, n) :
1. for i = 1 to n-1 
2.   min = i 
3.   for j = i+1 to n 
4.       if A[j] < A[min]
5.           min = j 
6.   temp = A[min] 
7.   A[min] = A[i] 
8.   A[i] = temp
                            
Time Complexity

Average Case : Θ(n^n)

Best Case : Ω(n^2)

Worst Case : O(n^n)

Space Complexity
OverAll Space Complexity
=
Auxiliary Space + Space taken by Input


Auxiliary Space is the extra space or temporary space used by an algorithm.

Auxiliary Space : O(1)
Space Taken by Input : O(n)
OverAll Space Complexity : O(n)

Insertion Sort

Algorithm

DESCRIPTION :
Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part.

INSERTION-SORT(A, n)
1. for i = 2 to n
2.   v = A[i]
3.   j = i 
4.   while j>1 and A[j-1]>v
5.       A[j] = A[j-1] 
6.       A[j-1] = v 
7.       j = j - 1
                        
Time Complexity

Average Case : θ(n^2)

Best Case : Ω(n)

Worst Case : O(n^2)

Space Complexity
OverAll Space Complexity
=
Auxiliary Space + Space taken by Input


Auxiliary Space is the extra space or temporary space used by an algorithm.

Auxiliary Space : O(1)
Space Taken by Input : O(n)
OverAll Space Complexity : O(n)

Quick Sort

Algorithm

DESCRIPTION :
Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into smaller arrays. A large array is partitioned into two arrays one of which holds values smaller than the specified value, say pivot, based on which the partition is made and another array holds values greater than the pivot value.

QUICKSORT(arr[], int low, int high)
1. if(low < high)
2.      p = PARTITION(arr,low,high);
3       QUICKSORT(arr,low,p - 1);
4.      QUICKSORT(arr,p + 1,high);
PARTITION(arr,low,high)
1. pivot = arr[high];
2. i = (low - 1);
3.   for(j = low; j <= high - 1; j++)
4.        if(arr[j] < pivot)
5.            i = i + 1
6.            swap(arr[i],arr[j])
7.   swap(arr[i + 1],arr[high])
8. return i + 1
                           
Time Complexity

Average Case : θ(n log(n))

Best Case : Ω(n log(n))

Worst Case : O(n^2)

Space Complexity
OverAll Space Complexity
=
Auxiliary Space + Space taken by Input


Auxiliary Space is the extra space or temporary space used by an algorithm.

Average Case / Best Case :

Auxiliary Space : O(logn)
Space Taken by Input : O(n)
OverAll Space Complexity : O(n)

Worst Case :

Auxiliary Space : O(n)
Space Taken by Input : O(n)
OverAll Space Complexity : O(n)

Merge Sort

Algorithm

DESCRIPTION :
Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into smaller arrays. A large array is partitioned into two arrays one of which holds values smaller than the specified value, say pivot, based on which the partition is made and another array holds values greater than the pivot value.

MERGE-SORT (A,p,r)
1. if p < r
2.   q=floor((p+r)/2)
3.   MERGE-SORT(A,p,q)
4.   MERGE-SORT(A,q+1,r)
5.   MERGE(A,p,q,r) 
MERGE(A,p,q,r)
1. n1=q-p+1
2. n2=r-q
3. let L[1....n1+1] and R[1......n2+1] ne new arrays
4. for i=1 to n1
5.     L[i] = A[p+i-1]
6. for j=1 to n2
7.     R[j] = A[q+j]
8. L[n1+1] = ∞
9. R[n2+1] = ∞
10. i = 1
11. j = 1
12. for k= p to r
13.     if L[i]<=R[j]
14.         A[k] = L[i]
15.         i=i+1
16.     else
17.         A[k]=R[j]
18.         j=j+1
    
Time Complexity

Average Case : θ(n log(n))

Best Case : Ω(n log(n))

Worst Case : O(n log(n))

Space Complexity
OverAll Space Complexity
=
Auxiliary Space + Space taken by Input


Auxiliary Space is the extra space or temporary space used by an algorithm.

Auxiliary Space : O(n)
Space Taken by Input : O(n)
OverAll Space Complexity : O(n)