import javax.swing.*;
import java.lang.reflect.Array;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;
public class RandExample {
public static void main(String[] args) {
int MethodChoice = Integer.parseInt(JOptionPane.showInputDialog("What method would you like to use to sort the random numbers" + "\n" + "1 - Selection Sort" + "\n" + "2 - Bubble Sort" + "\n" + "3 - Insertion Sort" + "\n" + "4 - Quick Sort"));
if (MethodChoice == 1) {
int iTotalCount = Integer.parseInt(JOptionPane.showInputDialog("What is the total number of integers?"));
int SortOrder = Integer.parseInt(JOptionPane.showInputDialog("1 - Ascending "
+ "2 - Descending"));
if (SortOrder == 2) {
int[] array = new int[iTotalCount];
System.out.println("After sorting using the Selection Sort, " + "Using Descending Order" + "the array is: ");
for(int count = array.length-1; count >= 0; count--)
System.out.print(array[count] + " ");
selectionSortReverse(array);
}
int[] array = new int[iTotalCount];
Random randomGenerator = new Random();
for (int i = 0; i < iTotalCount; i++) {
array[i] = randomGenerator.nextInt(1001);
System.out.print(" " + array[i]);
}
System.out.println("\n---------------------------------");
selectionSort(array);
//print out sorted list
System.out.println("After sorting using the Selection Sort," + " the array is:");
for (int count = 0; count < array.length; count++) {
System.out.print(array[count] + " ");
}
}
我有一个调用selectionSortReverse(array)的子程序;当用户选择2使其按降序排序时,但是当我点击2并继续时,它会以递增的方式发布数字。我把它放在哪里错了吗?这是我的selectionSortReverse子程序:
public static void selectionSortReverse(int data[]) {
int smallest;
for (int i = 0; i < data.length - 1; i++) {
smallest = i;
//see if there is a smaller number further in the array
for (int index = i + 1; index < data.length; index++) {
if (data[index] > data[smallest]) {
swap(data, smallest, index);
}
}
}
}
使用cricket_007建议的更新代码
import javax.swing.*;
import java.lang.reflect.Array;
import java.util.Collections;
import java.util.Random;
public class RandExample {
private static int[] generateRandomArray(int size, int randomMax) {
int[] array = new int[size];
Random randomGenerator = new Random();
for (int i = 0; i < size; i++) {
array[i] = randomGenerator.nextInt(randomMax);
}
return array;
}
public static void main(String[] args) {
int MethodChoice = Integer.parseInt(JOptionPane.showInputDialog("What method would you like to use to sort the random numbers" + "\n" + "1 - Selection Sort" + "\n" + "2 - Bubble Sort" + "\n" + "3 - Insertion Sort" + "\n" + "4 - Quick Sort"));
if (MethodChoice == 1) {
int iTotalCount = Integer.parseInt(JOptionPane.showInputDialog("What is the total number of integers?"));
int SortOrder = Integer.parseInt(JOptionPane.showInputDialog("1 - Ascending "
+ "2 - Descending"));
if (SortOrder == 2) {
int[] array = generateRandomArray(iTotalCount, 1001);
selectionSortReverse(array);
for(int count = array.length-1; count >= 0; count--)
System.out.print(array[count] + " ");
System.out.println("\n---------------------------------");
System.out.println("After sorting using the Selection Sort, " + "Using Descending Order" + " " + "the array is: ");
for(int i : array) {
System.out.print(i + " ");
}
} else if (SortOrder == 1) {
int[] array = generateRandomArray(iTotalCount, 1001);
selectionSort(array);
for(int count = array.length-1; count >= 0; count--)
System.out.print(array[count] + " ");
System.out.println("\n---------------------------------");
System.out.println("After sorting using the Selection Sort," + " the array is:");
for(int i : array) {
System.out.print(i + " ");
}
}
更新3
代码适用于选择---&gt;泡泡,插入不是那么多。随机整数的打印格式与排序列表的格式相同。
以下是代码:
} else if (MethodChoice == 3) {
if (SortOrder == 2) {
insertionSortReverse(array);
System.out.println("After sorting using the Insertion Sort, " + "Using Descending Order" + " " + "the array is: ");
} else if (SortOrder == 1) {
insertionSort(array);
System.out.println("After sorting using the Insertion Sort," + " the array is:");
}
for (int i : array) {
System.out.print(i + " ");
}
}
这是我的insertionSort()和insertionSortReverse()subs:
public static void insertionSort(int data[]) {
int insert;
for (int next = 1; next < data.length; next++) {
insert = data[next];
int moveItem = next;
while (moveItem > 0 && data[moveItem - 1] > insert) {
data[moveItem] = data[moveItem - 1];
moveItem--;
}
data[moveItem] = insert;
}
}
public static void insertionSortReverse(int data[]) {
int insert;
for (int next = 1; next < data.length; next++) {
insert = data[next];
int moveItem = next;
while (moveItem < 0 && data[moveItem - 1] < insert) {
data[moveItem] = data[moveItem - 1];
moveItem--;
}
data[moveItem] = insert;
}
}
答案 0 :(得分:1)
你的问题是当你点击2然后继续并按升序发布数字是因为你没有围绕升序代码的else语句,或者你正在反向排序数组,然后打印向后列出。此外,您发布的降序代码正在排序零列表...
private static int[] generateRandomArray(int size, int randomMax) {
int[] array = new int[size];
Random randomGenerator = new Random();
for (int i = 0; i < size; i++) {
array[i] = randomGenerator.nextInt(randomMax);
}
return array;
}
public static void main(String[] args) {
int MethodChoice = Integer.parseInt(JOptionPane.showInputDialog("What method would you like to use to sort the random numbers" + "\n" + "1 - Selection Sort" + "\n" + "2 - Bubble Sort" + "\n" + "3 - Insertion Sort" + "\n" + "4 - Quick Sort"));
int iTotalCount = Integer.parseInt(JOptionPane.showInputDialog("What is the total number of integers?"));
int SortOrder = Integer.parseInt(JOptionPane.showInputDialog("1 - Ascending, " + "2 - Descending"));
int[] array = generateRandomArray(iTotalCount, 1001);
System.out.println("Randomly Generated number list: ");
for (int i : array) {
System.out.print(i + " ");
}
System.out.println("\n---------------------------------");
if (MethodChoice == 1) {
if (SortOrder == 2) {
selectionSortReverse(array);
System.out.println("After sorting using the Selection Sort, " + "Using Descending Order" + "the array is: ");
} else if (SortOrder == 1) {
selectionSort(array);
System.out.println("After sorting using the Selection Sort," + " the array is:");
}
} else if (MethodChoice == 2) {
// bubble-sort
}
for (int i : array) {
System.out.print(i + " ");
}
}