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