【问题标题】:Selection sort in ascending order of array按数组升序排序
【发布时间】:2013-12-09 09:38:20
【问题描述】:

我可以让我的程序按降序排序,但不能按升序排序。我无法让我的代码在它们之间切换。我不应该只是能够将 -- 切换到 ++ 吗?我需要使用选择排序。在我的冒泡排序代码中,我可以更改一些小东西。这里是这样吗?还是我必须写一个完全不同的部分?

import javax.swing.*;
import java.io.*;
import java.util.*;

public class Gates_sortingSelection{

public static void main(String[] args){

  //ask user for the amount of values in the array      
  String amountS;
  amountS = JOptionPane.showInputDialog(null, "How many numbers would you like to sort?", 
        "Sorting Arrays", JOptionPane.INFORMATION_MESSAGE);
  int amount = Integer.parseInt(amountS);

  JOptionPane.showMessageDialog (null, "Please enter " + amount + " numbers you wish to sort.", 
        "Sorting Arrays", JOptionPane.INFORMATION_MESSAGE);

  int[] numbers = new int [amount];

  //a loop that will ask for as many numbers the user specified
  for(int i = 1; i <= amount; i++){
     String tempS = JOptionPane.showInputDialog("Number " + i + ": ");
     int temp = Integer.parseInt(tempS);
     numbers[i - 1] = temp; 
  }

  String which = JOptionPane.showInputDialog(null, "Would you like your values sorted in ascending (A), or descending (D) order?", 
        "Sorting Arrays", JOptionPane.INFORMATION_MESSAGE);

  if(!(which.equals("A") || which.equals("D"))){
     which = JOptionPane.showInputDialog(null, "Please choose ascending (A) or descending (D).", 
        "Sorting Arrays", JOptionPane.INFORMATION_MESSAGE);
  }

  if(which.equals("D")){
     //initialize the descending method
     sortD(numbers);
  }

  if(which.equals("A")){
     //initialize the ascending method
     sortA(numbers);
  }

}

public static void sortD(int[] tosort){

  int[] original = tosort.clone();      //copy the original array to send into the next method

  int i, j, first, temp;
  for(i = tosort.length-1; i > 0; i--){

     first = 0;     //initializes to subscript of first element
     for(j=1; j<=i; j++){    //locates the smallest elememt between position 1 and i 
        if(tosort[j] < tosort[first])
        first = j;
     }

     temp = tosort[first];   //swaps the smallest number found with the element in position i

     tosort[first] = tosort[i];
     tosort[i] = temp;
  }

print(tosort, original);      //send both the original array and the sorted array to the next method to print

}

这是升序代码开始的地方

public static void sortA(int[] tosort){

  int[] original = tosort.clone();      //copy the original array to send into the next method

  int i, j, first, temp;
  for(i = tosort.length-1; i > 0; i++){

     first = 0;     //initializes to subscript of first element
     for(j=1; j>=i; j++){    //locates the smallest elememt between position 1 and i 
        if(tosort[j] < tosort[first])
        first = j;
     }

     temp = tosort[first];   //swaps the smallest number found with the element in position i

     tosort[first] = tosort[i];
     tosort[i] = temp;
  }

print(tosort, original);      //send both the original array and the sorted array to the next method to print

}


public static void print(int[] sorted, int[] unsorted){

  //print the original array, and the sorted array
  JOptionPane.showMessageDialog (null, "Your original five numbers are: " 
           + Arrays.toString(unsorted) + ". \nYour new five numbers are: " 
           + Arrays.toString(sorted) + ".", "Sorted Arrays", JOptionPane.INFORMATION_MESSAGE);
}


}

【问题讨论】:

  • 可能值得跟踪数组之前是否已排序,如果已排序,则当前是降序还是升序。如果数组只是降序排序并且没有添加任何内容,如果用户想要再次降序排序,只需将其返回原样而不进行修改。如果用户想要升序排序,只需将其返回而不是重新排序。

标签: java arrays sorting selection


【解决方案1】:

如果你只需要排序,你可以使用 Arrays.sort(anArray) 而不是自己实现。

【讨论】:

    【解决方案2】:

    在您的 sortA 方法中,以下行有错误

    for(i = tosort.length-1; i > 0; i++){
    

    您不能从最后一个索引开始,然后向上增加您的计数器。试试这个:

    for(i = tosort.length-1; i > 0; i--){
    

    此外,for 循环中的内部行应更改为

    for (j = 0; j <= i; j++) { // locates the greatest elememt between
                                            // position 0 and i
                    if (tosort[j] > tosort[first])
                        first = j;
                } 
    

    既然你想按升序排列,它应该找到最大的数字,而不是最小的。

    【讨论】:

      【解决方案3】:

      您的代码中还有其他错误,但我不会尝试反转整个排序算法,只是比较。不要将最小的数字排序到数组的头部,而是瞄准最大的数字。因此,如果 sortAscending 是一个确定排序方向的布尔值,请使用以下代码:

      if((tosort[j] < tosort[first] && sortAscending) || (tosort[j] > tosort[first] && !sortAscending) {
          first = j;
      }
      

      【讨论】:

      • 我用这个替换什么?内部for循环?还是 if 语句?我很困惑,因为我不明白为什么它与降序排序方法如此不同。
      • 是的,只是 if 语句。这将根据布尔值进行升序和降序排序(您可能会将其传递给方法。或者您可以将 if 语句替换为: if(tosort[j] > tosort[first]) first = j;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-14
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多