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();
}
}
public static void insertionSort(int array[]) {
int n=array.length;
for (int i =1; i<n;++i){
int key=array[i];
int j=i-1;
/*
* Move elements of array [0..i-1], that are greater than key, to
one position
* ahead of their current position */
while (j>=0 && array [j]>key){
array[j+1]=array[j];
j=j-1;
}
array[j+1]=key;
}
}
public static void insertionSort(int array[]){
int n = array.length;
for (int i=1; i<n; ++i){
int key = array[i];
int j=i-1;
/*
* Move elements of array [0..i-1], that are greater than key, to
one
position ahead of their current position */
while (j>=0 && array[j]>key){
array[j+1]=array[j];
j=j-1;
}
array[j+1]=key;
}
}
private static void print(int[]aArray){
for (int i=0; i<aArray.length;i++)
System.out.print(aArray[i]+",");
}
static void selectionSort(int array[]){
int n = array.length;
//One by one move bondary of unsorted subarray
int min_idx=i;
for (int j=i+1; j<n;j++)
if(array[j]<array[min_idx])
min_idx=j;
//to swap the minimum element with the first element
int temp=array[min_idx];
array[min_idx]=array[i];
array[i]=temp;
public static void bubbleSort(int array[]){
int n = array.length;
for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;j++)
if(array[j]>array[j+1]){
//swap temp and array[i]
int temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
}
import java.util.Scanner; public class SortingAlogs { public static void main(String[] args) { Scanner KB = new Scanner(System.in); int ch; while (true) { int[] array = {9, 11, 15, 34, 1}; System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch == 1) bubbleSort(array); if (ch == 2) insertionSort(array); if (ch == 3) selectionSort(array); if (ch == 4) break; print(array); System.out.println(); } } public static void insertionSort(int[] array) { int n = array.length; for (int i = 1; i < n; ++i) { int key = array[i]; int j = i - 1; /* * Move elements of array [0..i-1], that are greater than key, to one position * ahead of their current position */ while (j >= 0 && array[j] > key) { array[j + 1] = array[j]; j = j - 1; } array[j + 1] = key; } } private static void print(int[] aArray) { for (int i = 0; i < aArray.length; i++) System.out.print(aArray[i] + ","); } public static void selectionSort(int[] array) { int n = array.length; //One by one move bondary of unsorted subarray for (int i = 0; i < array.length; i++) { int min_idx = i; for (int j = i + 1; j < n; j++) if (array[j] < array[min_idx]) min_idx = j; //to swap the minimum element with the first element int temp = array[min_idx]; array[min_idx] = array[i]; array[i] = temp; } } public static void bubbleSort(int[] array) { int n = array.length; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (array[j] > array[j + 1]) { //swap temp and array[i] int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } } }
Hello this is my java sorting algorithm program i need all of my errors corrected so...
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...
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....
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...
import java.util.Scanner; public class Chpt7_Project { public static void bubbleSort(int[] list) { int temp; for (int i = list.length - 1; i > 0; i--) { for (int j = 0; j < i; j++) { if (list[j] > list[j + 1]) { temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; } } } ...
Need // descriptions for each line of code that is relevant (Example, a description of "arr[i]=r.nextInt(100)+1;".Will give positive rate import java.util.Random; public class TestApp { public static void main(String[] args) { int arr[]=new int[11]; Random r = new Random(); //Random function for generation of random numbers for(int i=0;i<arr.length;i++) // arr[i]=r.nextInt(100)+1; printArray(arr); System.out.println(); bubbleSort(arr); printArray(arr); System.out.println(); System.out.println("Median : "+(arr[arr.length/2])); } private static void printArray(int[] aArray) { for (int i...
Aim To improve the algorithm provided in Snippet 2.3 by reducing the number of passes. public void sortImprovement1(int[] numbers) { for (int i = 1; i < numbers.length; i++) { for (int j = 0; j < numbers.length - i; j++) { if (numbers[j] > numbers[j + 1]) { swap(numbers, j, j + 1); } } } } Snippet 2.3: Bubble sort improvement 1 Change the bubble sort method so that it stops sorting if the array is untouched after...
Computer Science - Java
Explain the code step by step (in detailed order). Also explain
what the program required. Thanks
7 import java.io.File; 8 import java.util.Scanner; 9 import java.util.Arrays; 10 import java.io.FileNotFoundException; 12 public class insertionSort 13 14 public static void insertionSort (double array]) 15 int n -array.length; for (int j-1; j < n; j++) 17 18 19 20 21 double key - array[j]; int i-_1 while ( (i > -1) && ( array [i] > key array [i+1] -...
Using the following Java program, modify the code so that every time you run your program, it generates random numbers for your array, and then prints it (insertion sort) import java.awt.Graphics; import java.applet.Applet; public class SortingProg extends Applet { int a[] = { 55, 25, 66, 45, 8, 10, 12, 89, 68, 37 }; public void paint(Graphics g) { print(g,"Data items in original order",a,25,25); sort(); print(g,"Data items in ascending order",a,25,55); } public void sort() {...
Design a program that allows you to experiment with different sort algorithms in Java. This program should allow you to easily plug-in new sort algorithms and compare them. Assume that input data is generated randomly and stored in a text file (have no less than 2000 items to sort). Do not restrict your program to only one data type, or to one ordering relationship. The data type, ordering relationship, and the sorting method must be input parameters for your program....
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);...