簡體   English   中英

選擇按數組升序排序

[英]Selection sort in ascending order of array

我可以讓我的程序以降序排列,但不能升序排列。 我無法在我的代碼之間進行切換。 我不應該能夠將-轉換為++嗎? 我需要使用選擇排序。 在我的冒泡排序代碼中,我可以更改一些小東西。 是這樣嗎? 還是我必須寫一個完整的不同部分?

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


}

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

在您的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;
            } 

由於要按升序排列,因此應該找到最大的數目,而不是最小的數目。

您的代碼中還有其他錯誤,但是我不會嘗試顛倒整個排序算法,而只是嘗試比較。 與其將最小的數字排序到數組的開頭,不如將最大的數字作為目標。 因此,如果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