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

I have a subroutine that calls selectionSortReverse(array); when the users picks 2 to have it sort in descending, but when I hit 2, and proceed, it posts the numbers in ascending. Do I have it placed somewhere wrong? Here's my selectionSortReverse subroutine:

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

UPDATED CODE USING cricket_007'S SUGGESTION

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

UPDATE 3

Code works from Selection ---> Bubble, insertion not so much. The printing of random integers is in the same format as the sorted list.

Here's the code:

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

Here are my insertionSort() and 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;
    }
  }
3
  • This is why rather than doing your own compare function, you should be using a Comparator. If you were, you could just use reverseOrder(): docs.oracle.com/javase/8/docs/api/java/util/… Commented Oct 28, 2015 at 19:18
  • @ControlAltDel - That is assuming this is Java8 Commented Oct 28, 2015 at 19:22
  • I just found that calling the new subroutine seemed to be a lot more easier. I had the feeling that maybe it had something to do with the if-statement for when they select the order they want. Maybe it just keeps going back to the regular code instead of the one I put in the if code block? @ControlAltDel Commented Oct 28, 2015 at 19:23

1 Answer 1

1

Either your problem is when you hit 2 and proceed and it posts the numbers in ascending order is because you don't have an else statement around your ascending code, or you are sorting the array in reverse, then printing the list backwards. Also, your posted descending code was sorting a list of zeros anyways...

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 + " ");
    }
}
Sign up to request clarification or add additional context in comments.

18 Comments

When i try implementing this into my code it underlines array and tells me it's a duplicate, and when I remove the duplicates whole bunch of errors come up. Is it possible to do it as is with my code instead of having to switch the loops?
You can only have one declaration of int[] array in the same scope. The following assignments just need to be array. And the foreach loop that I used can be replaced with the for loop you used. There just was no need for use of your count variable. @Jcrow
where do I happen to put the first part of the code you posted? In the SortOrder{} or in the MethodChoice{} block?
It is a separate method outside of main in the RandExample class. @Jcrow
I understand thank you for the help! And I fixed the issue as far as the curly braces go, just had to readjust where it went and add some additional code to balance it out. Thank you so much for everything, you're awesome!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.