After How many passes in insertion sort the given array can be sorted?34 56 11 51 33 30 5 99
Answers
Answer:
Explanation:
the number of passes it will take to sort 34 56 11 51 33 30 5 99 by using insertion sort is 16. you can use the below code that i made to do insertion sort and count of passes
Code:
import java.util.Scanner;
public class InsertionSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of array");
int n=sc.nextInt();
int[]a=new int[n];
System.out.println("Enter elements to array");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
System.out.print(a[i]+" ");
}
System.out.println();
int count=0;
for(int k=1; k<a.length-1; k++)
{
int temp = a[k];
int j= k-1;
while(j>=0 && temp <= a[j])
{
a[j+1] = a[j];
j = j-1;
count=count+1;
}
a[j+1] = temp;
}
System.out.println("printing sorted elements ...");
for(int i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}
System.out.println("count"+count);
sc.close();
}
}
hope you understood please let me know for any other quires all the best buddy:)