insertion sorting in java ascending order

Data structure java

sorting in data structure example

Example Program

ascending order

        package sortingInJava;

public class InsertionSortingExample {

public int[] insertionSorting(int[] arr) {

 
 if(arr.length>0)
 {
  
  int i,j;
  
  for(i=1;i=0;j--)
   {
    if(temp>arr[j])
     break;
     arr[j+1]=arr[j];
     
   }//end inner loop
   arr[j+1]=temp;
  }//end outer loop
  
 }
 else {
  
  System.out.println("Array is empty");
 }
  return arr;
}//method ended
 
 public static void main(String ar[])
 {
  int[] arr= {4,10,5,3,1,2,6};
  System.out.println("Original arr:");
  for(int a:arr)
  {
   System.out.print(a+" ");
  }
  System.out.println("");
  System.out.println("Sorted array:");
  int[] sortedArr=new InsertionSortingExample().insertionSorting(arr);
  
  for(int a:sortedArr)
  {
   System.out.print(a+" ");
  }
 }
}

          

output

Comments

Popular posts from this blog

A publishing company that markets both book and audio-cassette in java. (Solution)

delete at last position in java

Create a class named Movie that can be used with your video rental business in java