【问题标题】:Descending Sorting Algorithm降序排序算法
【发布时间】:2015-10-28 19:08:01
【问题描述】:
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

代码适用于选择 ---> 气泡,插入不多。随机整数的打印与排序列表的格式相同。

代码如下:

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

这是我的插入排序()和插入排序反向()子:

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

【问题讨论】:

  • 这就是为什么您不应该使用自己的比较功能,而应该使用比较器。如果你是,你可以使用 reverseOrder(): docs.oracle.com/javase/8/docs/api/java/util/…
  • @ControlAltDel - 假设这是 Java8
  • 我刚刚发现调用新的子程序似乎要容易得多。我有一种感觉,当他们选择他们想要的顺序时,它可能与 if 语句有关。也许它只是继续回到常规代码而不是我放在 if 代码块中的代码? @ControlAltDel

标签: java sorting


【解决方案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 + " ");
    }
}

【讨论】:

  • 当我尝试在我的代码中实现它时,它会在数组下划线并告诉我它是重复的,当我删除重复时会出现一大堆错误。是否可以按原样使用我的代码而不是切换循环?
  • 同一范围内只能有一个int[] array 声明。以下分配只需要array。我使用的 foreach 循环可以替换为您使用的 for 循环。只是没有必要使用您的 count 变量。 @Jcrow
  • 我将您发布的代码的第一部分放在哪里?在 SortOrder{} 或 MethodChoice{} 块中?
  • 这是RandExample 类中main 之外的单独方法。 @Jcrow
  • 我明白谢谢你的帮助!我修复了花括号的问题,只需要重新调整它的位置并添加一些额外的代码来平衡它。非常感谢你所做的一切,你太棒了!
猜你喜欢
  • 2017-10-06
  • 1970-01-01
  • 2013-01-10
  • 1970-01-01
  • 1970-01-01
  • 2020-01-07
  • 2017-10-03
  • 2020-10-12
  • 2021-04-26
相关资源
最近更新 更多