whos joe
whos joe

Reputation: 13

I'm trying to do a bubble sort on integers that are imput by the user (java)

As mentioned above, I tried to implement a bubble sort method but every time I try to run it, it returns ones and zeroes instead of the sorted numbers.

So far I've tried to look at another example of bubble sort from a complete and working program, but nothing seems to have worked. I'd really appreciate if you could help me come to a solution. The code below will include my entire program.

import java.util.*; 
public class task2Q3
{
    public static void main (String []args)
    {
        Scanner sc= new Scanner(System.in);
        sc.useDelimiter("\n");

        int [] myArr= new int[15];  
        int time= 0;

        // This asks the user to imput the amount of time spent infront of a tablet 

        for(int i=0; i<myArr.length; i++)
        {
            System.out.println("Enter the amount of time you spend on the tablet daily" +i);
            sc.nextInt();
            myArr[i] ++;
        }
        bubbleSort(myArr);
        for(int i=0; i<myArr.length; i++)
        {
            System.out.println(myArr[i]);


        }
    }

    public static void bubbleSort(int[] tms)
    {
        boolean isSwapped= false;
        do{
            isSwapped= false;
            for(int i=0; i<tms.length-1; i++)
            {
                if(tms[i]>tms[i+1])
                {
                    int tmp= tms[i];
                    tms[i]= tms[i+1];
                    tms[i+1]= tmp;
                }

            }
        }while(isSwapped== true);

        
    } 

}

Upvotes: 1

Views: 81

Answers (2)

Vikrant
Vikrant

Reputation: 427

I have notice 2 issues in your program

  • You are not storing input element in your array so replace these 2 lines

    sc.nextInt();
    myArr[i] ++;
    
    //Replace above line with this
    myArr[i] = sc.nextInt();
    
  • Add isSwapped = true after swapping the elements

     if(tms[i]>tms[i+1])
     {
       int tmp= tms[i];    
       tms[i]= tms[i+1];    
       tms[i+1]= tmp;
       isSwapped = true;    
     }
    

Upvotes: 0

Sash Sinha
Sash Sinha

Reputation: 22473

You aren't assigning the entered values to the array in your for loop that asks for user input. Instead, you call nextInt without storing the input.

Here's an improved version of your program, that fixes this issue and improves readability:

import java.util.Arrays;
import java.util.Scanner;
import java.util.InputMismatchException;

public class Task2Q3 {
  private static final int NUM_ENTRIES = 15;

  public static void main(String[] args) {
    int[] usageTimes = getUserInput();
    bubbleSort(usageTimes);
    System.out.printf("Sorted times %s%n", Arrays.toString(usageTimes));
  }

  private static int[] getUserInput() {
    Scanner scanner = new Scanner(System.in);
    int[] times = new int[NUM_ENTRIES];
    System.out.println("Enter the amount of time you spend on the tablet daily:");
    for (int i = 0; i < times.length; i++) {
      times[i] = getIntInput(scanner, i + 1);
    }
    return times;
  }

  private static int getIntInput(Scanner scanner, int day) {
    while (true) {
      System.out.printf("Day %d: ", day);
      try {
        return scanner.nextInt();
      } catch (InputMismatchException e) {
        System.out.println("Invalid input. Please enter an integer.");
        scanner.next();
      }
    }
  }

  private static void bubbleSort(int[] array) {
    for (int end = array.length - 1; end > 0; end--) {
      boolean isSwapped = false;
      for (int i = 0; i < end; i++) {
        if (array[i] > array[i + 1]) {
          swap(array, i, i + 1);
          isSwapped = true;
        }
      }
      if (!isSwapped) break;
    }
  }

  private static void swap(int[] array, int firstIndex, int secondIndex) {
    int temp = array[firstIndex];
    array[firstIndex] = array[secondIndex];
    array[secondIndex] = temp;
  }
}

Upvotes: 0

Related Questions