### Insertionsort - Sorting Algorithm - JAVA

Program:
import java.io.*;
class arraylist
{
private int[] a;
private int n;
private int[] temp;
public arraylist(int x)
{
a = new int[x];
n=0;
temp = new int[x];
}
public void insert(int e)
{
n++;
a[n]=e;
}
public void display()
{
for(int i=1;i<=n;i++)
System.out.print(a[i]+" ");
System.out.println("\n");
}
public void insertion()
{
System.out.print("(insertionsort)\n\n");
display();
for(int j=2;j<=n;j++)
{
int key=a[j];int i=j-1;
while((i>0)&&(a[i]>key))
{
a[i+1]=a[i];
i--;
}
a[i+1]=key;
display();
}
}

}

class insertionsort
{
public static void main(String arg[]) throws IOException
{
DataInputStream x1= new DataInputStream(System.in);

System.out.print("\nEnter the number of elements:");
arraylist ob=new arraylist(na+1);
System.out.println("Enter the elements:");
for (int k=1;k<=na;k++)

System.out.println("Before sorting:\n");
ob.display();
System.out.println("Sorting.............");

ob.insertion();
System.out.println("After sorting:\n");
ob.display();
}
}

Output:
nn@linuxmint ~ \$ javac insertionsort.java
Note: insertionsort.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ \$ java insertionsort

Enter the number of elements:5
Enter the elements:
5
4
3
2
1
Before sorting:

5 4 3 2 1

Sorting.............
(insertionsort)

5 4 3 2 1

4 5 3 2 1

3 4 5 2 1

2 3 4 5 1

1 2 3 4 5

After sorting:

1 2 3 4 5

nn@linuxmint ~ \$

### Selectionsort - Sorting Algorithm - JAVA

Program:
import java.io.*;
class arraylist
{
private int[] a;
private int n;
private int[] temp;
public arraylist(int x)
{
a = new int[x];
n=0;
temp = new int[x];
}
public void insert(int e)
{
n++;
a[n]=e;
}
public void display()
{
for(int i=1;i<=n;i++)
System.out.print(a[i]+" ");
System.out.println("\n");
}

public void swap(int x,int y)
{
int temp=a[x];
a[x]=a[y];
a[y]=temp;
}
public void selection()
{
System.out.print("(selectionsort)\n\n");
display();
for(int i=1;i<n;i++)
{
int min=i;
for(int j=i+1;j<=n;j++)
if(a[j]<a[min])
min=j;
swap(i,min);
display();
}
}

}

class selection
{
public static void main(String arg[]) throws IOException
{
DataInputStream x1= new DataInputStream(System.in);
System.out.print("\nEnter the number of elements:");
arraylist ob=new arraylist(na+1);
System.out.println("Enter the elements:");
for (int k=1;k<=na;k++)
System.out.println("Before sorting:\n");
ob.display();
System.out.println("Sorting.............");

ob.selection();
System.out.println("After sorting:\n");
ob.display();
}
}

Output:
nn@linuxmint ~ \$ javac selection.java
Note: selection.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ \$ java selection

Enter the number of elements:5
Enter the elements:
5
4
3
2
1
Before sorting:

5 4 3 2 1

Sorting.............
(selectionsort)

5 4 3 2 1

1 4 3 2 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

After sorting:

1 2 3 4 5

nn@linuxmint ~ \$

### Bubblesort - Java

Program:
import java.io.*;
class arraylist
{
int a[];
int n;
arraylist(int max)
{
a=new int[max];
n=0;
}
void insert(int x)
{
a[n]=x;
n++;
}
public void display()
{
for(int i=1;i<=n;i++)
System.out.print(a[i]+" ");
System.out.print("\n");
}
public void bblsort()
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(a[j]>a[j+1])
{
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}

class sort
{
public static void main(String args[])throws IOException
{
System.out.println("Enter how many elements:");
int n=Integer.parseInt(s);
arraylist a1=new arraylist(100);
System.out.println("Enter the elements:");
for(int i=0;i<n;i++)
{
int k=Integer.parseInt(f);
a1.insert(k);
}
a1.bblsort();
System.out.println("\nThe sorted elements are:- ");
a1.display();
}
}

Output:
nn@linuxmint ~ \$ javac sort.java
nn@linuxmint ~ \$ java sort
Enter how many elements:
5
Enter the elements:
5
4
3
2
1

The sorted elements are:-
1 2 3 4 5
nn@linuxmint ~ \$