Question

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 arr[] )
{
int N = arr.length;
int i, j, temp;
for (i = 1; i< N; i++)
{
j = i;
temp = arr[i];
while (j > 0 && temp < arr[j-1])
{
arr[j] = arr[j-1];
j = j-1;
}
arr[j] = temp;
}
}
/* Main method */
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
System.out.println("Insertion Sort Test\n");
int n, i;
/* Accept number of elements */
System.out.println("Enter number of integer elements");
n = scan.nextInt();
/* Create integer array on n elements */
int arr[] = new int[ n ];
/* Accept elements */
System.out.println("\nEnter "+ n +" integer elements");
for (i = 0; i < n; i++)
arr[i] = scan.nextInt();
/* Call method sort */
sort(arr);
/* Print sorted Array */
System.out.println("\nElements after sorting ");
for (i = 0; i < n; i++)
System.out.print(arr[i]+" ");
System.out.println();   
}
}

PLEASE make sure the code runs properly with no errors!

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


import java.util.Scanner;

/* Class InsertionSort */
public class InsertionSortTwo {
   /* 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 = arr[i];
           while (j > 0 && temp < arr[j - 1]) {
               arr[j] = arr[j - 1];
               j = j - 1;
           }
           arr[j] = temp;
       }
   }

   /* Main method */
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println("Insertion Sort Test\n");
       int n, i;
       /* Accept number of elements */
       System.out.println("Enter number of integer elements");
       n = scan.nextInt();
       if (n > 10) {
           System.err.println("Can't accept more than 10 numbers : ");
           return;
       }
       /* Create integer array on n elements */
       int arr[] = new int[n];
       /* Accept elements */
       System.out.println("\nEnter " + n + " integer elements");
       for (i = 0; i < n; i++)
           arr[i] = scan.nextInt();
       /* Call method sort */
       sort(arr);
       /* Print sorted Array */
       System.out.println("\nElements after sorting ");
       for (i = 0; i < n; i++)
           System.out.print(arr[i] + " ");
       System.out.println("\nRange "+ arr[arr.length - 1] +"-"+arr[0]+" = " +(arr[arr.length - 1] - arr[0]));
       System.out.println();
   }
}

Add a comment
Know the answer?
Add Answer to:
The following code is a Java code for insertion sort. I would like this code to...
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
  • 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...

  • 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...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  •   Given a bubble sort and the following array: [10,7,19,5,16] What are the values after the first...

      Given a bubble sort and the following array: [10,7,19,5,16] What are the values after the first iteration? Given an insertion sort and the following array: [50,5,35,70,6] What does the array look like after the first insertion:    Fill in the missing code for InsertionSort method // Insertion Sort method //sorts an array of Auto objects public static void insertionSort(Auto [] arr) { Auto temp; int j; for (int i=1; i<arr.length; i++) {     j=i;     temp=arr[i];                while (j!=0 &&(temp.getModel().compareTo(arr[[j-1].getModel())<0)...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • I'm trying to find out what is wrong with my code? package DriverClass; public class DriveClass...

    I'm trying to find out what is wrong with my code? package DriverClass; public class DriveClass { public static void main(String[] args) { int a[] = {3, 2, 5, 6, 1}; InsertionSortClass insertion = new InsertionSortClass(); System.out.print("The original list : "); System.out.println(); insertion.printArray(a); System.out.println(); System.out.println("The list after insertionSort : "); System.out.println(); insertion.insertionSort(a); } } package DriverClass; public interface SortADTInterface { public void insertionSort(int arr[]); public void printArray(int arr[]); } package DriverClass; public class InsertionSortClass implements SortADTInterface{ public void insertionSort(int[] arr)...

  • Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Jav...

    Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer {    // Please expand method main() to meet the lab requirements.       // You have the following sorting methods available:    // insertionSort(int[] a);    // selectionSort(int[] a);    // mergeSort(int[] a);    // quickSort(int[] a);    // The array will be in sorted order after the routines are called!   ...

  • Can anyone help me with this code? I've never worked with insertion sort and I'm having...

    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: http://www.algolist.net/Algorithms/Sorting/Insertion_sort Input 4368 92157 Where, • First line represents the type of ordering. • Second line represents the size of the array. • Third line represents the array elements. Output 123456789 + Test Case(s) DriverMain.java...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • 6. (4 points) The following code for insertion sort has been modified to print out the...

    6. (4 points) The following code for insertion sort has been modified to print out the sorted component of the array during the sort. What will be the output of running the main method of the following code? Try to trace by hand and then verify by executing the code public class Insertion ( public static void sort (String[] a) f int n- a.length; for (int i-1; i < n; i++) f for (int j i; j 0; j--) if...

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