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) {
int i = arr.length;
for (int j = 1; j < i; j++) {
int value = arr[j];
int k = j - 1;
while ((j > 0) && (arr[i] > value)) {
arr[i + 1] = arr[k];
i--;
}
arr[i+1] = value;
}
}
public void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
Error in
public void insertionSort(int[] arr) {
int i = arr.length;
for (int j = 1; j < i; j++) {
int value = arr[j];
int k = j - 1;
while ((k >= 0) && (arr[k] > value)) { // for checking of first value it must be >= 0
arr[k + 1] = arr[k];
k--;
}
arr[k+1] = value;
}
I'm trying to find out what is wrong with my code? package DriverClass; public class DriveClass...
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...
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...
I'm writing this class called CharArrayProject_3 that removes repeating elements from an array of chars. However, when I run the driver class, it just outputs two sets of dashed lines. What am I getting wrong? Is it the deleteRepeats() method?: public class CharArrayProject_3 { private char[] array; privateint length; privateintnumberOfRepeats; public CharArrayProject_3( char[] arr ) { length = arr.length; array = new char[ length ]; numberOfRepeats = 0; for( int k = 0; k < arr.length; k++ ) { array[k]...
import java.util.Arrays; public class lab { public static void main(String args[]) { int arr[] = {10, 7, 8, 9, 1, 5,6,7}; int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1}; int arr3[] = {1, 3, 5, 3, 2, 6, 20}; quicksort(arr,0,arr.length-1); quicksort(arr2,0,arr2.length-1); quicksort(arr3,0,arr3.length-1); System.out.println(Arrays.toString(arr)); System.out.println(Arrays.toString(arr2)); System.out.println(Arrays.toString(arr3)); } private static int partition(int[] items,int low, int high) { int i=0; int j=0;...
I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...
Draw a flowchart for this program public class InsertionSort { public static void main(String a[]) { int[] array = {7, 1, 3, 2, 42, 76, 9}; insertionSort(array); } public static int[] insertionSort(int[] input) { int temp; for (int i = 1; i < input.length; i++) { for (int j = i; j > 0; j--) { if (input[j] < input[j - 1]) { temp = input[j]; input[j] = input[j - 1]; input[j - 1] = temp; } } display(input, i);...
please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...
must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...
Can you help me with this code in Java??? import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int rows = 3; int columns = 4; int[][] arr = new int[rows][columns]; for(int i = 0; i < rows; ++i) { for(int j = 0; j < columns; ++j) { System.out.println("Enter a value: "); arr[i][j] = scan.nextInt(); } } System.out.println("The entered matrix:"); for(int i = 0; i < rows; ++i) { for(int j...
Help with a question in Java: What is the output from the following program? public class Methods2 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { for (int j = 0; j <= i; j++){ System.out.print(fun(i, j) + "\t"); } System.out.println(); } } static long fun(int n, int k) { long p = 1; while (k > 0){ p *= n; } return p; } }