Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Insertion_Sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// INSERTION SORT IN JAVA
import java.util.Scanner;
public class ISort
{

public static void Sort(int a[])
{
int n=a.length,i,j,p,temp;
for (i = 1;i < n; i++)
{

for (j=i-1; j >=0 && a[j+1]<a[j]; j--)
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;

}

}
}
public static void printarray(int a[])
{
for(int i=0; i < a.length; i++)
{

System.out.print(a[i]+" ");
}

}
public static void main(String[] args)
{
int n, res,i;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of elements in the array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter "+n+" elements ");
for( i=0; i < n; i++)
{
a[i] = s.nextInt();
}

System.out.println( "elements in array ");
printarray(a);
Sort(a);
System.out.println( "\nelements after sorting");
printarray(a);

}


}