Question

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();
}
   }
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;

}
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
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;
                }
            }
        }
    }
}

Add a comment
Know the answer?
Add Answer to:
Hello this is my java sorting algorithm program i need all of my errors corrected so...
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...

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

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

  • Computer Science - Java Explain the code step by step (in detailed order). Also explain what...

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

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

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

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

  • Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {...

    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);...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • please illistrate a UML diagram for the following code bellow, it should have 3 rows, 1...

    please illistrate a UML diagram for the following code bellow, it should have 3 rows, 1 colum like the one bellow, first box should have the class name, second box is for class attributes, and third box should have the class operations/methods and please put a (+) for public and a (-) for private example: class +-attributes +-Operations Also make a JAVADOCs, but not generated by a compiler, to explain what is going on in the code import java.util.Random; public...

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