package ds.interview.dataStructure.Algo;
//Insertion sort
//starts from index 1, and compares itself with previous values to adjust itself
import java.util.Arrays;
public class InsertionSort {
public static void main(String[] args)
{
int temp;
int arr[] = {4,8,6,2,9,1,7,3};
for(int i = 1;i<arr.length;i++)
{
for(int j =i ;j>0 ;j--)
{
if(arr[j]<arr[j-1])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1]= temp;
}
}
}
System.out.println(Arrays.toString(arr));
}
}
//Insertion sort
//starts from index 1, and compares itself with previous values to adjust itself
import java.util.Arrays;
public class InsertionSort {
public static void main(String[] args)
{
int temp;
int arr[] = {4,8,6,2,9,1,7,3};
for(int i = 1;i<arr.length;i++)
{
for(int j =i ;j>0 ;j--)
{
if(arr[j]<arr[j-1])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1]= temp;
}
}
}
System.out.println(Arrays.toString(arr));
}
}
No comments:
Post a Comment