简体   繁体   English

选择按数组升序排序

[英]Selection sort in ascending order of array

I can get my program to sort in descending, but not ascending. 我可以让我的程序以降序排列,但不能升序排列。 I can't get my code to switch between them. 我无法在我的代码之间进行切换。 Shouldn't I just be able to swicth the -- to ++? 我不应该能够将-转换为++吗? I need to use selection sort. 我需要使用选择排序。 In my bubble sort code, I could just change a few small things. 在我的冒泡排序代码中,我可以更改一些小东西。 Is that the case here? 是这样吗? Or do I have to write an entire different section? 还是我必须写一个完整的不同部分?

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

}

This is where the ascending code starts 这是升序代码开始的地方

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);
}


}

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

In your sortA method, the following line has errors 在您的sortA方法中,以下行有错误

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

You can't start at the last index and then increment your counter upward. 您不能从最后一个索引开始,然后向上递增计数器。 Try this: 尝试这个:

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

Also, the inner lines in that for loop should be changed to 此外,for循环中的内线应更改为

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

Since you want in ascending order, it should find the greatest number, not the smallest. 由于要按升序排列,因此应该找到最大的数目,而不是最小的数目。

There are other errors in your code, but I wouldn't attempt to reverse the whole sorting algorithm, just the comparison. 您的代码中还有其他错误,但是我不会尝试颠倒整个排序算法,而只是尝试比较。 Instead of sorting the smallest number to the head of your array, aim for the largest. 与其将最小的数字排序到数组的开头,不如将最大的数字作为目标。 So if sortAscending is a boolean that determines the sort direction, use the following code: 因此,如果sortAscending是确定排序方向的布尔值,请使用以下代码:

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

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

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