vendredi 1 décembre 2023

How can I ask a user with the scanner to insert the ammount of random numbers he/she would like to be created and sort it f. e. with bubblesort?

I am currently studying computer science in my first semester and I have an assignment I need some kind help with. I am supposed to create an array which fills in the amount of random numbers the user specifies using the java.util.Scanner. Currently my code looks like this:

package lia.prog1.exercises.set09;

import java.util.Scanner;
import java.util.Random;

public class SortingTool
{

    private void bubblesort(int[] arr)
    {
        int sortArray;
        int i;
        int j;
        int tempArray;

        Scanner scanner = new Scanner(System.in);
        sortArray = scanner.nextInt();
        System.out.println("How many numbers do you want to sort?");



        for (i = 0; i < (arr.length - 1); i++)
        {
            for (j = 0; j < arr.length - i - 1; j++)
            {
                if (arr[j] > arr[j + 1])
                {
                    tempArray = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tempArray;
                }
            }
        }
    }

    private void insertionsort(int [] data)
    {
        int [] sorted = new int[data.length];

        for (int i = 0; i < data.length; i++)
        {
            int min = Integer.MAX_VALUE;
            for (int j = 0; j < data.length; j++)
            {
                if (min > data[j])
                {
                    int temp = min;
                    min = data[j];
                    data[j] = temp;
                }
            }
            sorted[i] = min;
        }
    }

}



Aucun commentaire:

Enregistrer un commentaire