如何摆脱排序整数末尾的零

时间:2012-12-15 10:13:48

标签: java sorting

我的代码正在运行,但它给了我不必要的号码,我不知道如何摆脱它们。请任何建议将受到高度赞赏

import java.util.Scanner;
import java.io.*;


 public class ArraySort1 {

  public static void main(String[] args) throws IOException {
  Scanner inputFile; 
  inputFile = new Scanner(new File("arrayex1.txt"));
  PrintWriter outputFile;
outputFile = new PrintWriter(new FileWriter("sorted.txt"));

      /* Declare the array */

int[] numbers = new int[10000];
int index = 0;

      int temp, smallest, smallest_index;

      /* Read the first number */
      numbers[index] = inputFile.nextInt();

      while(inputFile.hasNextInt()) {
        numbers[index] = inputFile.nextInt();
        index++;
        }
// Local variable
int swValue;

// Display menu graphics
System.out.println("============================");
System.out.println("|   MENU SELECTION DEMO    |");
System.out.println("============================");
System.out.println("| Options:                 |");
System.out.println("|        1. Selection Sort |");
System.out.println("|        2.  Bubble Sort   |");
System.out.println("|        3. Exit           |");
System.out.println("============================");
swValue = Keyin.inInt(" Select option: ");

// Switch construct
switch (swValue) {
case 1:
  System.out.println(" Selection Sort Starting....");
  for (int i = 0; i < index - 1; i++) {

      smallest = numbers[i];
      smallest_index = i;

      for (int j = i + 1; j < index; j++) {

        if (numbers[j] < smallest) { 
              smallest = numbers[j];
              smallest_index = j;
          }
      }

      /* If needed switch numbers[i] and numbers[smallest_index] */

      if (numbers[i] != numbers[smallest_index]) {

          temp = numbers[i];
          numbers[i] = numbers[smallest_index];
          numbers[smallest_index] = temp;
      }
  }
  for (int i = 0; i < index; i++) {          
    System.out.print (numbers[i] + " ");
          outputFile.print (numbers[i] + " ");
      }

      inputFile.close();
      outputFile.close();



  break;
case 2:
 //2 System.out.println("Bubble Sort Sorting 2 selected");

    //int i = 0, t=0;
    boolean swapped = true;

    while(swapped){

    swapped = false;

    for(int i1 = 0; i1 < index  - 1; i1++){
        smallest = numbers[i1];
        smallest_index = i1;

    if(numbers[i1] > numbers[i1 + 1]){
    swapped = true;
    int tmp = numbers[i1];
    numbers[i1] = numbers[i1 + 1];
    numbers[i1 + 1] = tmp;
    } 
    } 

    for(int i1 = 0; i1 < numbers.length; i1++){
   System.out.print(numbers[i1] + "\t");
    } 
   System.out.println();

    }




 break;

case 3:
  System.out.println("Exit selected");
  break;
default:
  System.out.println("Invalid selection");
  break; // This break is not really necessary
   }
 }

}

如果我选择选项2:冒泡排序我

1   2   4   1   5   7   9   56  67  0   0   0
1   2   4   5   7   9   56  67   0               0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0       0   0   0   0   0   0   0   0   0   0   0   0   0   0   
1   1   2   4   5   7   9   56  67  0

请问我有问题的代码是冒泡排序。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

问题是您的数组长度为10000。

int[] numbers = new int[10000];

这是在选择1

时生成输出的方式
for (int i = 0; i < index; i++) {  

您最多只能打印index(这是您从文件中读取的整数数量)。

当您选择选项2时,这是您生成输出的方式:

for(int i1 = 0; i1 < numbers.length; i1++){

其中numbers.length表示数组的每个元素。我现在不是在写这个解决方案,因为我认为你应该能够自己解决这个问题。