将随机整数从高到低排序,而不是从低到高排序? import javax.swing.*; 导入java.util.Random; 导入java.util.Scanner; 公共类示例{ 公共静态void main(字符串[]args){ int MethodChoice=Integer.parseInt(JOptionPane.showInputDialog(“您希望使用什么方法对随机数进行排序”+”\n“+”1-选择排序“+”\n“+”2-冒泡排序“+”\n“+”3-插入排序“+”\n“+”4-快速排序”); if(MethodChoice==1){ int iTotalCount=Integer.parseInt(JOptionPane.showInputDialog(“整数的总数是多少?”); int[]数组=新的int[iTotalCount]; Random randomGenerator=新的Random(); 对于(int i=0;i
,只需向后打印数组即可将随机整数从高到低排序,而不是从低到高排序? import javax.swing.*; 导入java.util.Random; 导入java.util.Scanner; 公共类示例{ 公共静态void main(字符串[]args){ int MethodChoice=Integer.parseInt(JOptionPane.showInputDialog(“您希望使用什么方法对随机数进行排序”+”\n“+”1-选择排序“+”\n“+”2-冒泡排序“+”\n“+”3-插入排序“+”\n“+”4-快速排序”); if(MethodChoice==1){ int iTotalCount=Integer.parseInt(JOptionPane.showInputDialog(“整数的总数是多少?”); int[]数组=新的int[iTotalCount]; Random randomGenerator=新的Random(); 对于(int i=0;i,java,algorithm,sorting,Java,Algorithm,Sorting,,只需向后打印数组即可 import javax.swing.*; import java.util.Random; import java.util.Scanner; public class RandExample { public static void main(String[] args) { int MethodChoice = Integer.parseInt(JOptionPane.showInputDialog("What meth
import javax.swing.*;
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[] 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] + " ");
}
} else if (MethodChoice == 2) {
int iTotalCount = Integer.parseInt(JOptionPane.showInputDialog("What is the total number of integers?"));
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---------------------------------");
bubbleSort(array);
//print out sorted list
System.out.println("After sorting using the Bubble Sort,"
+ " the array is:");
for (int count = 0; count <array.length; count++) {
System.out.print(array[count] + " ");
}
} else if (MethodChoice == 3) {
int iTotalCount = Integer.parseInt(JOptionPane.showInputDialog("What is the total number of integers?"));
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---------------------------------");
bubbleSort(array);
//print out sorted list
System.out.println("After sorting using the Insertion Sort,"
+ " the array is:");
for (int count = 0; count <array.length; count++) {
System.out.print(array[count] + " ");
}
} else if (MethodChoice == 4) {
int iTotalCount = Integer.parseInt(JOptionPane.showInputDialog("What is the total number of integers?"));
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---------------------------------");
bubbleSort(array);
//print out sorted list
System.out.println("After sorting using the Quick Sort,"
+ " the array is:");
for (int count = 0; count <array.length; count++) {
System.out.print(array[count] + " ");
}
}
}
public static void quickSort(int data[], int low, int high) {
int partitionLoc;
if (low < high) {
partitionLoc = partition(data, low, high);
quickSort(data, low, partitionLoc - 1);
quickSort(data, partitionLoc + 1, high);
}
}
public static int partition(int data2[],int left,int right) {
boolean moveLeft = true;
int separator = data2[left];
while (left < right) {
if (moveLeft == true) {
while ((data2[right] >= separator) && (left < right)) {
right--;
}
data2[left] = data2[right];
moveLeft = false;
} else {
while ((data2[left] <= separator) && (left < right)) {
left++;
}
data2[right] = data2[left];
moveLeft = true;
}
}
data2[left] = separator;
return left;
}
public static void bubbleSort(int data[]) {
//Loop to control number of passes
for (int pass = 1; pass < data.length; pass++) {
//Loop to control # of comparisons for length of array-1
for (int element=0;element<data.length-1;element++) {
//compare side-by-side elements and swap them if
//first element is greater than second element
if (data[element] > data[element + 1]) {
swap(data, element, element + 1); //call swap method
}
}
}
}
public static void swapBubble(int array2[], int first, int second) {
int hold = array2[first];
array2[first] = array2[second];
array2[second] = hold;
}
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 selectionSort(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);
}
}
}
}
public static void swap(int array2[], int first, int second) {
int hold = array2[first];
array2[first] = array2[second];
array2[second] = hold;
}
}
你真的理解代码中的排序是如何工作的吗?对我来说,“你”这个词似乎很不寻常可以实现快速排序算法,但不知道如何更改排序方向。@wadda_wadda,我去年在学校用VB完成了这些算法,但从未学会如何更改顺序,只是如何编写它们。在我的学校,我们被灌输了如何使用C&P代码示例来完成所有事情,以及如何更改v变量,我知道这可能是不好的做法。我想我可能在代码块中的某个地方声明了我的数组,而在其他代码块中(告诉我初始化变量),我尝试在代码块中添加相同的数组(所有数组声明看起来都非常难看)当我尝试时,它以相同的方式打印数组,除了这次在随机生成的数字之前有4个零。
for(int i = array.length-1; i >= 0; i--)
System.out.print(array[i] + " ");