排序从最高到最低而不是从最低到最高的随机整数?

时间:2015-10-27 23:26:22

标签: 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 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;



    }
}

我试过看但是它们用于列表,我想提示用户看看他们是否想要从升序或降序输出排序。我不太确定如何做到这一点,我已经看到使用反向的功能,但我不知道如何将其应用到我的代码中。

1 个答案:

答案 0 :(得分:0)

您只需向后打印数组即可。

for(int i = array.length-1; i >= 0; i--)
 System.out.print(array[i] + " ");
相关问题