Complexity
Most of the primary sorting algorithms run on
different space and time complexity.
Time Complexity is defined to be the time the
computer takes to run a program (or algorithm in our
case).
Space complexity is defined to be the amount of
memory the computer needs to run a program.
Complexity in general, measures the algorithms
efficiency in internal factors such as the time needed
to run an algorithm.
Big O Analysis
When solving a computer science problem there
will usually be more than just one solution.
These solution often be in the form of different
algorithms.
Big O analysis will help to measure the efficiency
of the algorithm.
Big O notation is used to classify algorithms by
how they respond to changes in input size in
terms of time and space.
Big O only provides the upper bound on the
growth rate of the function.
Bubble sort
Explanation:
Compare each element (except last one) with its neighbour to the
right
If they are out of order, swap them
This puts the largest element at the very end
The last element is now in the correct and final phase
Compare each element (except last one) with its neighbour to the
right
If they are out of order, swap them
This puts the second largest element next to last
The last two elements are now in their correct and final phase
Compare each element (except last one) with its neighbour to the
right
Selection sort
Explanation:
Given an array of length n;
Search element 0 through n-1 and select the smallest
Swap it with the element at location 0;
Search element 1 through n-1 and select the smallest
Swap it with the element at location 1;
Search element 2 through n-1 and select the smallest
Swap it with the element at location 2;
Continue until there is nothing left to search.
Analysis
The outer loop executes n-1 time
The inner loop executes n/2 times on average.
Time required is roughly (n-1)*(n/2)
Time complexity = O(n^2)
Insertion sort
Explanation:
The basic idea of Insertion Sort algorithm can be described as these
steps:
1. Data elements are grouped into two sections:
a sorted section and an un-sorted section.
2. Take an element from the un-sorted section.
3. Insert the element into the sorted section at the correct position
based on the comparable property.
4. Repeat step 2 and 3 until no more elements left in the un-sorted
section.
Adding a new element to a sorted list will keep the list sorted if the
element is inserted in the correct place
A single element list is sorted
Inserting a second element in the proper place keeps the list sorted
This is repeated until all the elements have been inserted into the
Analysis
Best case: If A is sorted: O (n)
Worst case:
- The outer loop is always done N – 1 times
- The inner loop does the most work when the next
element is smaller than all of the past elements
- On each pass, the next element is compared to all
earlier elements, giving:
If A is reversed sorted: O (n^2)
Average case: O (n^2)
Merge Sort
Explanation:
Merge Sort is a complex and fast sorting algorithm that repeatedly
divides an un-sorted section into two equal sub-sections, sorts them
separately and merges them correctly.
The basic idea of Merge Sort algorithm can be described as these
steps:
1. Divide the data elements into two sections with equal number of
elements.
2. Sort the two sections separately.
3. Merge the two sorted sections into a single sorted collection.
Obviously, this is a recursive idea, where a problem is divided into
smaller problems. And the division will be repeated to make the
smaller problems even smaller, until they are smaller enough so that
the solutions are obvious.
Analysis
1. T(1)=1
2. T(N)= 2 T(N/2) + N
Time to mergesort two arrays each N/2 elements + time
to merge two arrays each n/2 elements
3. Next we will solve this recurrence relation
Firsy we Divide by N
T(N)/N = T(N/2)/(N/2) + 1
Now,
T(N/2)/(N/2) = T(N/4)/(N/4) + 1 ………… = log N
T(N)= N + N log N
O(n log n)
Quick Sort
Explanation:
Divide and conquer algorithm
It is divided in three parts
Elements less than pivot element
Pivot element
Elements greater than pivot elements
Where pivot as the middle element of the large list
Example:
List= 3 7 8 5 2 1 9 5 4
Assume 4 as a pivot element, so rewrite list as:
3 1 2 4 5 8 9 5 7
So element at the left side of pivot element is less than pivot element
and at the right side greater.
Recursively sort the sub-list of lesser elements and the sublist of
greater element
Quick Sort
quick_sort(int a[],int low,int high)
{
if(low<high) then
j=partition(a,low,high+1); //position of the pivot
index
quick_sort(a,low,j-1);
quick_sort(a,j+1,high);
}
}
int partition(int a[],int low,int high)
{
int pivot=a[low],i=low, j=high;
while(i<j){
do { i++;
}while(a[i]<pivot);
do{
j--;
} while(a[j]>pivot);
if(i<j){
interchange(a,i,j);
}
}
a[low]=a[j];
a[j]=pivot;
return j;
}