Insertionsort

mail@pastecode.io avatar
unknown
java
2 months ago
706 B
2
Indexable
Never
import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
    public static void main(String args[]){//Insertionsort
        int []arr={3,6,2,1,8,7,4,5,3,1};
        for(int index=1;index<arr.length;index++){//because indexof 0 is already sorted
        int temp=arr[index];//mean unsorted index 1 ele is =6 tepk in temp
        int previous_index=index-1;
        while(previous_index>=0&&arr[previous_index]<temp){
            arr[previous_index+1]=arr[previous_index];
            previous_index--;
        }
        arr[previous_index+1]=temp;
        }
        for(int i=0;i<arr.length;i++){
            System.out.print(arr[i]);
            System.out.print(" ");
        }
    }
}