Question

Can anyone help me with this code? I've never worked with insertion sort and I'm having trouble. I posted the instructions and the included code. Thank you!

Insertion Sort Given an array of integers, sort the array in ascending/descending order by using Insertion sort. Reference: h

DriverMain.java Problem Solution.java 1- import java.io.*; 2 import java.util.*; Ovau WN 4. public class ProblemSolution { pu

DriverMain.java Problem Solution.java 1 import java.io.*; 2 import java.util.*; 4 //Your program will be evaluated by this Dr

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

class ProblemSolution {
   public int solution(int[] A, int N) {
       for (int i = 1; i < N; ++i) {
           int key = A[i];
           int j = i - 1;

           /*
           * Move elements of A[0..i-1], that are greater than key, to one position
           * ahead of their current position
           */
           while (j >= 0 && A[j] > key) {
               A[j + 1] = A[j];
               j = j - 1;
           }
           A[j + 1] = key;
       }
       return N;
   }
}

public class DriverMain {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int N = sc.nextInt();
       int A[] = new int[N];
       for (int i = 0; i < N; i++) {
           A[i] = sc.nextInt();
       }
       ProblemSolution problemSolution = new ProblemSolution();
       System.out.println(problemSolution.solution(A, N));
   }
}

<ler tillateu> DriverMallu avd Application C:\Proglalti Flies

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
Can anyone help me with this code? I've never worked with insertion sort and I'm having...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write where its written write your code here!! please use java for the code! please...

    Please write where its written write your code here!! please use java for the code! please use the given code and I will rate thanks Magic index in an array a[1..n] is defined to be an index such that a[ i ] = i. Given an array of integers, write a recursive method to find the first magic index from left to right. If one exists in the given array, return the index number i, otherwise return -1. Here are...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • can some one help me solve this and explain it please Input Format A line indicating...

    can some one help me solve this and explain it please Input Format A line indicating the size of the array the array on the next line Constraints n < 10000 Output Format One line printing the array in order input (given to you) One line printing the array, followed by its maximum value Sample Input 0 5 1 3 5 7 9 Sample Output 0 1 3 5 7 9 9 7 5 3 1 9 Contest ends in...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • Please explain if the following code is actually correct. If the following code correct, please explain...

    Please explain if the following code is actually correct. If the following code correct, please explain why the code works and is also correct. Don’t use * Java’s Integer .toBinaryString(int) in this program./* According to the textbook and the instructor, I am not supposed to use arrays such as int binary[] = new int[25]; I guess this is the reason why this problem is starting to look kind of hard.   Chapter 5 Exercise 37: Java Programming * * (Decimal to...

  • Provide comments for this code explaining what each line of code does import java.util.*; public class...

    Provide comments for this code explaining what each line of code does import java.util.*; public class Chpt8_Project {    public static void main(String[] args)       {        Scanner sc=new Scanner(System.in);        int [][]courses=new int [10][2];        for(int i=0;i<10;i++)        {            while(true)            {                System.out.print("Enter classes and graduation year for student "+(i+1)+":");                courses[i][0]=sc.nextInt();                courses[i][1]=sc.nextInt();                if(courses[i][0]>=1...

  • use the same code. but the code needs some modifications. so use this same code and...

    use the same code. but the code needs some modifications. so use this same code and modify it and provide a output Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT