Insertion Sort 1 Comments
Comments
-
import java.util.*;
class Main{
static void doStuff(int[] ar){
//your code here
int n=ar[ar.length-1];
insertSort(ar,n);} static void insertSort(int[] ar,int n){
for(int i=ar.length-1;i>=1;i--){ if(n<ar[i-1]){ ar[i]=ar[i-1]; printarray(ar); }else{ ar[i]=n; printarray(ar); } } } static int[] swap(int[] ar, int i, int j) { int temp=ar[i]; ar[i]=ar[j]; ar[j]=temp; return ar; } static void printarray(int[] ar){ for (int i=0;i<ar.length;i++) { System.out.print(ar[i]+" "); } System.out.print("\n"); } //boilerplate code public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for(int c=0;c<t;c++){ int n = in.nextInt(); int[] ar = new int[n]; for(int i=0;i<n;i++){ ar[i]=in.nextInt(); } doStuff(ar); } }
}
-
Dear Admin,
As what i can see from the "Your Output" it is the same as "Correct Output", however the last array is not displayed so im not sure if im getting "Incorrect" due to the last array or something else.
Could the ''Your Output' output be increased to all results? I want to make sure i'm not getting "Incorrect" due to the last array.
Thanks
-
@David, it displays all the output. Make sure you're printing the final steps too.
-
Thanks Admin, i noticed just now... double checking on my end needs to be improved.
da
May 15, 8:35 AM1 3 3
1 2 3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Main.insertSort(Main.java:12)
at Main.doStuff(Main.java:6)
---------code-----------
import java.util.*;
class Main{
static void doStuff(int[] ar){
//your code here
int n=ar[ar.length-1];
insertSort(ar,n);
}