Sorting Techniques5
Sorting Techniques5
1. Bubble Sort
Sorting is to place elements in increasing or decreasing order. Bubble sort is also
known as sinking sort . In this sorting algorithm , we keep comparing the adjacent
pair , if they are in not right order , then they swapped each other position . When
there are no elements swapped in one full iteration of element list , then it
indicates that bubble sort is completed.
When to Use
Bubble sort is good to use for small number of input elements(less than 1000) .
You can see by running the below algorithm for
(Input)(Space) (Time taken)
10 elements : Bubble sort took 0 milliseconds
100 elements : Bubble sort took 1 milliseconds
1000 elements : Bubble sort took 2 milliseconds
10000 elements : Bubble sort took 181 milliseconds
100000 elements : Bubble sort took 19096 milliseconds
Big O Notation
In simple words , Big O allows us to say something about how the size of inputs
affect the runtime of a program .This technique can also be applied to other
resources that an algorithm takes up (such as memory)
and we can analyze other bounds than the upper bound such as lower bound on
time or space or expected time or space that an algorithm takes up .
Properties
It is a stable algorithm i.e does not change the relative order of elements with
equal keys
Algorithm:
i. Start
ii. Pass=1
iii. i=0
iv. Check that arr[i]>arr[i+1] if yes
Interchange arr[i] and arr[i+1]
v. i=i+1
vi. Check that i<=arr.length-pass goto step 4
vii. pass = pass+1
viii. Check that pass<arr.length, goto step 3
ix. Stop
Program:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
return list;
}
}
int elementlist[] = new int[arrlist.size()];
Iterator<Integer> iter = arrlist.iterator();
for (int j=0;iter.hasNext();j++) {
elementlist[j] = iter.next();
}
elementlist=bubbleSort(elementlist);
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println("Values after Bubble Sort : ");
for (int j=0;j<elementlist.length;j++) {
System.out.println(elementlist[j]+" ");
}
}
}
2. Selection Sort
The algorithm divides the input lists into two parts ; the sublist of the items are
already sorted ,which is built up from left to right at the front(left) of the list , and
the sublist of the items remaining to be sorted that occupy the rest of the list.
Initially the sorted sublist is empty and the unsorted sublist is the entire input
list .The algorithm proceeds by finding the smallest (or largest depending on the
sorting order) element in the unsorted sublist exchanging it with the leftmost
unsorted element (putting in sorted order) and moving the sublist boundaries one
element to the right.
It generally performs worse than the insertion sort though it has performance
advantages over more complicated algorithms especially when auxiliary memory
is limited.
When to Use
Selection sort is good to use for small number of input elements(less than 1000) .
You can see by running the below algorithm for
(Input)(Space) (Time taken)
10 elements : Selection sort took 0 milliseconds
100 elements : Selection sort took 0 milliseconds
1000 elements : Selection sort took 5 milliseconds
10000 elements : Selection sort took 154 milliseconds
100000 elements : Selection sort took 13764 milliseconds
So , you can see it is not wise decision to use Selection sort for large number of
elements as other algorithms take much less time .
Big O Notation
In simple words , Big O allows us to say something about how the size of inputs
affect the runtime of a program .This technique can also be applied to other
resources that an algorithm takes up (such as memory)
and we can analyze other bounds than the upper bound such as lower bound on
time or space or expected time or space that an algorithm takes up .
Properties
* It is not a Stable algorithm i.e does change the relative order of elements with
equal keys
Program:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
}
int elementlist[] = new int[arrlist.size()];
Iterator<Integer> iter = arrlist.iterator();
for (int j=0;iter.hasNext();j++) {
elementlist[j] = iter.next();
}
elementlist=selectionSort(elementlist);
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println("Values after Selection Sort : ");
for (int j=0;j<elementlist.length;j++) {
System.out.println(elementlist[j]+" ");
}
}
}
3. Insertion Sort
Insertion sort is good to use for small number of input elements(less than 1000) .
You can see by running the below algorithm for
(Input)(Space) (Time taken)
10 elements : Insertion sort took 0 milliseconds
100 elements : Insertion sort took 0 milliseconds
1000 elements : Insertion sort took 0 milliseconds
10000 elements : Insertion sort took 1 milliseconds
100000 elements : Insertion sort took 3 milliseconds
So , you can see it is not wise decision to use Insertion sort for large number of
elements as other algorithms take much less time .
Big O Notation
In simple words , Big O allows us to say something about how the size of inputs
affect the runtime of a program .This technique can also be applied to other
resources that an algorithm takes up (such as memory)
and we can analyze other bounds than the upper bound such as lower bound on
time or space or expected time or space that an algorithm takes up .
Properties
* It is a Stable algorithm i.e does not change the relative order of elements with
equal keys
* Very low overhead
* Online i.e can sort a list as it receives it
Best , Average,Worst Cases
Algorithm:
Program:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
}
int elementlist[] = new int[arrlist.size()];
Iterator<Integer> iter = arrlist.iterator();
for (int j=0;iter.hasNext();j++)
{
elementlist[j] = iter.next();
}
elementlist=insertionSort(elementlist);
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println("Values after Insertion Sort : ");
for (int j=0;j<elementlist.length;j++) {
System.out.println(elementlist[j]+" ");
}
}
}
4. Quick Sort
Quick sort is based on one principle divide and conquer. Quick sort first divides
the large lists into two sub smaller lists, the low elements and the high
elements .Quick sort can then recursively sort the sublist.
* Choose an element and called it as pivot, in the given list.
* Reorder the list so that all elements with values less than the pivot come before
the pivot, while all elements
with values greater than the pivot come after it (equal values can go either
way). After this partitioning pivot is in its final position. This is called
the partition operation.
* Recursively apply the above steps to the sublist of the elements with smaller
values and separately the sublist of elements with greater values.
When to Use
Big O Notation
In simple words , Big O allows us to say something about how the size of inputs
affect the runtime of a program .This technique can also be applied to other
resources that an algorithm takes up (such as memory)
and we can analyze other bounds than the upper bound such as lower bound on
time or space or expected time or space that an algorithm takes up .
Properties
Algorithm:
Program:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
// Choose a pivot value and then partition the array so that every value
// less than the pivot is positioned before the pivot in the array and
// every value greater than the pivot is positioned after the pivot in
// the array.
i++;
while (list[i] < pivot)
{
i++;
}
// Keep decrementing from the end of the range so long as the values
// are greater than the pivot.
j--;
while (list[j] > pivot)
{
j--;
}
// So long at the indexes have not crossed, swap the pivot with the
// value that was out of place.
if (i < j)
{
swap(list, i, j);
}
}
// Recursively sort the two portions of the array
quicksort(list, from, j);
quicksort(list, j + 1, to);
}
5. Merger Sort
Merge sort is a divide and conquer algorithm that was invented by John von
Neumann in 1945.
When to Use
Here runtime mostly depends upon the pivotal element you choose .You can see
by running the below algorithm for
(Input)(Space) (Time taken)
10 elements : Merge sort took 0 milliseconds
100 elements : Merge sort took 0 milliseconds
1000 elements : Merge sort took 1 milliseconds
10000 elements : Merge sort took 3 milliseconds
100000 elements : Merge sort took 97 milliseconds
Big O Notation
In simple words , Big O allows us to say something about how the size of inputs
affect the runtime of a program .This technique can also be applied to other
resources that an algorithm takes up (such as memory)
and we can analyze other bounds than the upper bound such as lower bound on
time or space or expected time or space that an algorithm takes up .
Properties
* It is a Stable algorithm i.e does not change the relative order of elements with
equal keys
* It does not require random access to data
Algorithm:
Program:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
elementlist=mergeSort(elementlist);
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println("Values after Merge Sort : ");
for (int j=0;j<elementlist.length;j++) {
System.out.println(elementlist[j]+" ");
}
}
}