繁体   English   中英

实现bubbleSort后未获得正确的输出

[英]Not getting correct output after implementing bubbleSort

该程序将创建一个名为datafile.txt的文件,并使用文本I / O将100个随机创建的整数写入该文件中。 我还实现了bubbleSort来对数字进行升序排序,但它没有对它们进行排序。 另外,命令行的输出为“已排序的数字为:[I @ f72617] 100次。 提前致谢。

   import java.io.*;
   import java.util.Random;

   public class Lab5 {

//sort array
static int[] bubbleSort(int[] array) {

    for (int pass = 1; pass <= 100; pass++) {
        for (int current = 0; current < 100-pass; current++) {

            //compare element with next element
            if (array[current] > array[current + 1]) {

                //swap array[current] > & array[current + 1]
                int temp = array[current];
                array[current] = array[current + 1];
                array[current + 1] = temp;
            } //end if
        }
    }
    return array;
}
public static void main(String args[]) {

    //Open file to write to
    try {
        FileOutputStream fout = new FileOutputStream("F:\\IT311\\datafile.txt");


    int index = 0;

    //Convert FileOutputStream into PrintStream 
    PrintStream myOutput = new PrintStream(fout);
    Random numbers = new Random();
        //Declare array
        int array[] = new int[100];
        for (int i = 0; i < array.length; i++)
        {
        //get the int from Random Class
        array[i] = (numbers.nextInt(100) + 1);

        myOutput.print(array[i] + " ");

        //sort numbers
        int[] sortedArray = bubbleSort(array);

        //print sorted numbers
        System.out.print("The sorted numbers are: ");
        System.out.print(sortedArray);
        }
    }
    catch (IOException e) {
        System.out.println("Error opening file: " + e);
        System.exit(1);
    }
}

}

在下面使用:

System.out.print(Arrays.toString(sortedArray));

代替

    System.out.print(sortedArray);

采用

for(int a : sortedArray)    
        System.out.println(a);

第一个是打印数组的地址。 第二个将打印数组的每个元素。

我对您的代码做了很少的更改,您在每次迭代中都打印数组对象。 对于打印数组,您需要遍历数组并打印其每个元素。 加上冒泡排序已修改,如果要使用,则还有附加代码,只需取消注释即可

import java.io.*;
import java.util.Arrays;
import java.util.Random;

public class Lab5 {

//sort array
static int[] bubbleSort(int[] array) {

 for (int pass = 0; pass < 100; pass++) {
     for (int current = 1; current < 100-pass; current++) {

         //compare element with next element
         if (array[current-1] > array[current ]) {

             //swap array[current] > & array[current + 1]
             int temp = array[current-1];
             array[current-1] = array[current];
             array[current ] = temp;
         } //end if
     }
 }
 return array;
}
public static void main(String args[]) {

 //Open file to write to
 try {
      FileOutputStream fout = new FileOutputStream("F:\\IT311\\datafile.txt");


 int index = 0;

 //Convert FileOutputStream into PrintStream 
 PrintStream myOutput = new PrintStream(fout);
 Random numbers = new Random();

 // code begin to print random number in file and output sorted array on sysout 
     //Declare array
     int array[] = new int[100];
     for (int i = 0; i < array.length; i++)
     {
     //get the int from Random Class
     array[i] = (numbers.nextInt(100) + 1);

     myOutput.print(array[i] + " ");
     }

     //sorted array using sort function 
//     Arrays.sort(array);

     // sortred using bubble sort
     array=bubbleSort(array);

   //print sorted numbers
     System.out.print("The sorted numbers are: ");
     for (int i = 0; i < array.length; i++)
        {
        int j = array[i];
        System.out.print(j +" " );
        }

  // code end to print random number in file and output sorted array on sysout     



//     code begin to print sorted number in file and output sorted array on sysout
     /*int array[] = new int[100];
     for (int i = 0; i < array.length; i++)
     {
     array[i] = i;
     }
     Arrays.sort(array);
     for (int j = 0; j < array.length; j++)
        {
        int j2 = array[j];
        myOutput.print(j2  + " ");
        System.out.println(j2+" ");
        }
        */

//   code end to print sorted number in file and output sorted array on sysout
 }
 catch (IOException e) {
     System.out.println("Error opening file: " + e);
     System.exit(1);
 }
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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