Friday 22 January 2016

Java program for merge sort



import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import static java.lang.System.out;
public class MergeSort {
public static void main(String args[])throws Exception
{
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
new MergeSort().mergeSort(a, 0, n - 1);
for(int i=0;i<b.length;i++)
if(b[i]!=0)
out.print(b[i]+" ");
else break;
}
public void mergeSort(int[] a, int i, int j) {

if(i>=j)
return;
else
{
int mid=(i+j)/2;
mergeSort(a,i,mid);
mergeSort(a,mid+1,j);
out.println("merging start with index "+i+" "+j);
merge(a,i,j,mid);
}
}
static int b[]=new int[1000];
public void merge(int[] a, int i, int j,int mid) {
//System.out.println("yes");
int start=i,end=j;
int k=mid+1;
int l=i;
while (start<=mid&&k<=end)
{
if(a[start]<=a[k])
{
b[l++]=a[start++];
}else {
b[l++]=a[k++];
}
}
if(k>end)
for(;start<=mid;) b[l++]=a[start++];
else if(start>mid) for(;k<=end;) b[l++]=a[k++];
//copying back is very important because without this it will not merge correctly.
for(l=i;l<=j;l++)
a[l]=b[l];
}
}

OUTPUT:-
C:\Users\saty\Desktop>javac MergeSort.java
C:\Users\saty\Desktop>java MergeSort
8

34

21

56

78

9

12

5

8

merging start with index 0 1

merging start with index 2 3

merging start with index 0 3

merging start with index 4 5

merging start with index 6 7

merging start with index 4 7

merging start with index 0 7

5 8 9 12 21 34 56 78

Sunday 17 January 2016

Java program for insertion sort



import java.util.Scanner;
class Main{
public static void main(String args[]){
int i,j,n,k,temp,number=8;
int list[]=new int[number];
Scanner input=new Scanner(System.in);
for(i=0;i<number;i++){
System.out.println("Enter the numbers ");
list[i]=input.nextInt();
}
for(k=1;k<=number-1;k++){
temp=list[k];
j=k-1;
while((temp<list[j])&&(j>=0)){
list[j+1]=list[j];
j=j-1;
}
list[j+1]=temp;
}
for(i=0;i<number;i++){
System.out.println("Insertin sorted elements are "+list[i]);
}
}
}

OUTPUT:-
C:\Users\saty\Desktop>javac 1.java
C:\Users\saty\Desktop>java Main
Enter the numbers

12

Enter the numbers

42

Enter the numbers

5

Enter the numbers

8

Enter the numbers

65

Enter the numbers

1

Enter the numbers

18

Enter the numbers

9

Selection sorted list are 1

Selection sorted list are 5

Selection sorted list are 8

Selection sorted list are 9

Selection sorted list are 12

Selection sorted list are 18

Selection sorted list are 42

Selection sorted list are 65

Java program for selection sort


import java.util.Scanner;
class Main{
public static void main(String args[]){
int i,j,min,loc,temp,number=8;
int list[]=new int[number];
Scanner input=new Scanner(System.in);
for(i=0;i<number;i++){
System.out.println("Enter the numbers ");
list[i]=input.nextInt();
}
min=list[0];
for(i=0;i<=number-1;i++){
min=list[i];
loc=i;
for(j=i+1;j<=number-1;j++){
if(list[j]<min){
min=list[j];
loc=j;
}
}
if(loc!=i){
temp=list[i];
list[i]=list[loc];
list[loc]=temp;
}
}
for(i=0;i<=number-1;i++){
System.out.println("Selection sorted list are "+list[i]);
}
}
}

OUTPUT:-
C:\Users\saty\Desktop>javac 1.java
C:\Users\saty\Desktop>java Main

Enter the numbers
45
Enter the numbers
2
Enter the numbers
12
Enter the numbers
58
Enter the numbers
64
Enter the numbers
98
Enter the numbers
22
Enter the numbers
8
Selection sorted list are 2
Selection sorted list are 8
Selection sorted list are 12
Selection sorted list are 22
Selection sorted list are 45
Selection sorted list are 58
Selection sorted list are 64
Selection sorted list are 98