G. ANKAMMA RAO
Sorting’s
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Rasmus Lerdorf
Creator of PHP Scripting Language
Sorting
’
s
Sorting
Sorting is to organize a collection of data elements based on the order of a comparable property of each element.
Bubble Sort
In this bubble sort, in each iteration we find largest un-sorted element in its proper position by comparing each element i.e. in first iteration we put first largest element in position n-1. In second iteration the second largest element is placed at n-2 position in general after ithiteration the largest element is placed
in n-ithposition.
Here each element bubbles up to its proper position because of that this method is called as the bubble sort. Consider the unsorted array A 10 20 7 8 32 12
First iteration
A[0] and A[1], 10 > 20 false,
10 20 7 8 32 12
A[1] and A[2], 20 > 7 true, interchange of elements 10 7 20 8 32 12
A[2] and A[3], 20 > 8 true, interchange of elements 10 7 8 20 32 12
A[3] and A[4], 20 > 32 false,
10 7 8 20 32 12
A[4] and A[5], 32 > 12 true, interchange of elements
Chapter
After completion of first iteration the elements are 10 7 20 8 12 32
i.e the first largest element 32 is reached its proper position (n-1).
After completion of second iteration the second largest element 20 is placed its proper (n-2) position. i.e. 10 7 8 12 20 32
After completion of third iteration the third largest element 12 is placed its proper (n-3) position. i.e. 10 7 8 12 20 32
After completion of fourth iteration the fourth largest element 10 is placed its proper (n-4) position. i.e. 7 8 10 12 20 32
Now all the elements are in its proper position so that there is no need of rest of iterations.
Implementation of Bubble Sort
//Java program to sort the list by using bubble sort class BubbleSort
{
public static void main(String args[]) {
int temp;
int number[]={10,20,7,8,32,12}; int n=number.length;
System.out.println("Array length is"+n); //printig Array elements
System.out.println("Array elements before sorting"); for(int i=0;i<n;i++)
{
System.out.print("\t"+number[i]); }
//LOGIC FOR BUBBLE SORT for(int i=n-1;i>0;i--) {
for(int j=0;j<i;j++) {
temp=number[j];
number[j]=number[j+1]; number[j+1]=temp;
} } }
//printing Sorted Array elements
System.out.println("\n"+"Array elements after sorting"); for(int i=0;i<n;i++)
{
System.out.print("\t"+number[i]); }
}//main close }//class close OUTPUT:
The time complexity of Bubble sort is O(n2), where n number of elements.
Selection Sort
In this sorting, in first iteration we find smallest element and it is interchanged with first element of the list. And in second iteration we find second smallest and it is interchanged with second element and so on. This process is repeated until all elements are sorted. This sorting is done through selection because of that this sorting technique is called as selection sort.
For example consider the array A of elements: Original Array List
After first iteration
7 40 10 8 32 12
After second iteration
7 8 10 40 32 12 After third iteration
7 8 10 40 32 12 After fourth iteration
7 8 10 12 32 40 After fifth iteration
7 8 10 12 32 40 After sixth iteration
7 8 10 12 32 40
Implementation of Selection Sort
// Program to sort the given list using Selection sort
class SelectionSort {
public static void main(String args[]) {
int number[]={10,40,7,8,32,12}; int temp;
int n=number.length;
System.out.println("Array length is"+n); System.out.println("Array before sorting"); for(int i=0;i<n;i++)
{
System.out.print("\t"+number[i]); }
//LOGIC FOR SELECTION SORT for(int i=0;i<n;i++)
{
{
if(number[i]>number[j]) {
temp=number[i];
number[i]=number[j]; number[j]=temp;
} } }
System.out.println("\n"+"Array elements after sorting"); for(int i=0;i<n;i++)
{
System.out.print("\t"+number[i]); }
}//main close }//class close OUTPUT:
The time complexity of quick sort is O(n2), where n number of elements.
Insertion Sort
initial data. If you enter data greater than or less than the existing data then we interchange the positions of these two elements until list is sorted.
Implementation of Insertion Sort
// Program to sort the given elements using Insertion Sort import java.io.*;
class InsertionSort {
public static void main(String args[])throws IOException {
DataInputStream d=new DataInputStream(System.in); int a[]=new int[10];
int i=0,k,j;
System.out.println("Enter one number or -99 to terminate"); int x=Integer.parseInt(d.readLine());
while(x!=-99) {
k=i-1;
while((k>=0) && (x<a[k])) {
a[k+1]= a[k]; k--;
}
a[k+1]=x;
System.out.println("Array list insertion of"+x); for(j=0;j<=i;j++)
{
System.out.println("\t"+a[j]); }
System.out.println("Enter one number or -99 to terminate"); x=Integer.parseInt(d.readLine());
++i;
}//class close OUTPUT:
The time complexity of quick sort is O(n2), where n number of elements.
Merge Sort
Merging means combing two arrays. In this method consider two arrays. First sort the two arrays individually in ascending order.
For example array A is 8 10 3 50 After sorting
For example array B is
20 4 14 7 9 After sorting
4 7 9 14 20
In this method we compare A[0] and B[0]
If A[0] < B[0] then copy A[0] to C[0] then we increment array A and C. If A[0] > B[0] then copy B[0] to C[0] then we increment array B and C.
This process is repeated until all elements in each array is completed. If one array is completed then simply copy the elements of another array to C.
i.e C array elements are
3 4 7 8 9 14 20 50
Implementation of merge sort
// Program to merge two arrays and sort them by using Merge Sort class MergeSort
{
public static void main(String args[]) {
int i,j,k,temp;
int a[]={8,10,3,50}; int b[]={20,4,14,7,9}; int n=a.length;
int m=b.length;
int c[]=new int[n+m]; //printing arrayelements
System.out.println("A array elements"); for(i=0;i<n;i++)
{
System.out.print("\t"+a[i]); }
System.out.println("\n"+"B array elements"); for(j=0;j<m;j++)
{
//sorting A array for(i=0;i<n;i++) {
for(j=i+1;j<n;j++) {
if(a[i]>a[j]) {
temp=a[i]; a[i]=a[j]; a[j]=temp; }
} }
//sorting B array for(i=0;i<m;i++) {
for(j=i+1;j<m;j++) {
if(b[i]>b[j]) {
temp=b[i]; b[i]=b[j]; b[j]=temp; }
} }
//LOGIC FOR MERGE SORT i=0;j=0;k=0;
while((i<n) && (j<m)) {
if(a[i]<b[j]) {
i++; }
if(a[i]>b[j]) {
c[k]=b[j]; k++;
j++; } }
while(i<n) {
c[k]=a[i]; k++;
i++; }
while(j<m) {
c[k]=b[j]; k++;
j++; }
//printing merged array elements
System.out.println("\n"+"c Array elements"); for(i=0;i<(n+m);i++)
{
System.out.print(("\t"+c[i])); }
The time complexity of quick sort is O(nlogn), where n number of elements.
Quick Sort
It is also called as Partition Exchange sort contains the technique of divide and conquer. Let us consider array contains the n elements in quick we select first element as pivot or key element and also we place this pivot element in proper place. i.e the element left to the pivot element are less than and the elements right to the pivot element are greater than. Therefore the array is divided into two sub lists.
Sub list- 1 contains the elements less than the key, sub list-2 contains greater than the key. This process is repeated until each sub list contains only one element.
Example for Quick Sort
Let us consider the following elements, n=8.
Start Left Scan,
Here our aim is to find the element greater than the key i.e., we increment i value as long as we get an element greater than the key.
Now start the right scan,
Here i<j, then swap a[i], a[j] and continue left scan and right scan.
Now apply left scan,
Now apply right scan,
Here i>j therefore we swap a[j] and key element.
Here we again apply the same process for sub list-1and sub list-2 until each sub list contains only one element. Finally we get