简体   繁体   中英

Bubble Sort numbers from file in java

I am trying to bubble sort numbers from a text file, I understand how to bubble sort and how to use a text file. But have never used both of them at the same time. I tried bubble sorting an array and just trying to figure out how to replace that array with a text file. If someone can explain to me how to get the bubble sort to read a text file it would be greatly appreciated. I am new to java and it is sometimes confusing to combine 2 different things I have learned into 1 program.

Here is my bubble sort that solves the array:

  public static void main(String[] args)
 {

  int number[]={7,13,4,5,62,3,1,3,45};

  int temp;
  boolean fixed=false;
  while(fixed==false){
      fixed = true;

  for (int i=0; i <number.length-1;i++){ 
      if (number[i]>number[i+1]){

      temp = number [i+1];

      number[i+1]=number[i];

      number[i]=temp;
      fixed=false;
      }
 }
}
  for (int i=0; i<number.length;i++){
      System.out.println(number[i]);
   }
  }

}

Use Scanner class !

File file=new File("file.txt");
Scanner sc=new Scanner(file);
int arr[]=new int[100];
int i=0;
while(sc.hasNextLine()){
   arr[i]=sc.nextInt();
   i++;
}

Don't just hard code the array..!

Suppose the content of your file is a list of number separated by some delimiter say single space " "

Use:

File file=new File("file.txt");
Scanner sc=new Scanner(file);
String arr[] = sc.nextLine().split(" ");

That is it. Once you have got the array u can play around with it..!

You can do like this:

package test;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class Test {

    public static void bubbleSort(int[] num ) {
        int j;
        boolean flag = true;   // set flag to true to begin first pass
        int temp;   //holding variable

        while ( flag ) {
            flag= false;    //set flag to false awaiting a possible swap
            for( j=0;  j < num.length -1;  j++ ) {
                if ( num[ j ] < num[j+1] )  {
                    temp = num[ j ];                //swap elements
                    num[ j ] = num[ j+1 ];
                    num[ j+1 ] = temp;
                    flag = true;              //shows a swap occurred  
                } 
            } 
        } 
    } 

    public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File("numbers.txt"));
        int [] numbers = new int [256];
        int i = 0;
        while(scanner.hasNextInt()){
           numbers[i++] = scanner.nextInt();
        }

        bubbleSort(numbers);

        System.out.println(Arrays.toString(numbers));

    }
}

Reading a file has nothing to do with bubble sort. You can read the file to create an array of integers and then use the usual bubble sort algorithm to sort it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM