Insertion sorting technique in java.

import java.util.Scanner;
public class code
{
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("enter size of array=");
    int n=sc.nextInt();
    int[] arr=new int[n];
    System.out.println("enter elements of array=");
    for(int i=0; i<n; i++){
        arr[i]=sc.nextInt();
    }
    for(int i=1; i<n; i++){
        for(int j=i; j>=1; j--){
            if(arr[j]<arr[j-1]){
                int t=arr[j];
                arr[j]=arr[j-1];
                arr[j-1]=t;
            }
            else
                 break;
        }
    }
    System.out.println("sorted array is=");
    for(int ele : arr){
        System.out.print(ele+" ");
    }
}
}

Comments

Popular posts from this blog

Introduction of java Programming language.

Stack data structure.