I have a program that sorts randomly generated numbers from least to greatest or greatest to least depending on the users choice. 2 problems are occurring.
When the user does Insertion Sorting with Descending, the randomly generated numbers and sorting numbers output like this for example:
Randomly Generated Numbers: 89 90 2 830 399
After sorting using the Insertion Sort, Using Descending Order, the array is: 89 90 2 830 399
It's weird because my other methods are EXACTLY the same, and they work fine, but for some reason this doesn't work.
Here is my code:
import javax.swing.*;
import java.lang.reflect.Array;
import java.util.Random;
public class RoutineSorter {
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) {
if (SortOrder == 2) {
bubbleSortReverse(array);
System.out.println("After sorting using the Bubble Sort, " + "Using Descending Order" + " " + "the array is: ");
} else if (SortOrder == 1) {
bubbleSort(array);
System.out.println("After sorting using the Bubble Sort," + " the array is:");
}
} 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:");
} else if (MethodChoice == 4) {
if (SortOrder == 2) {
}
}
for (int i: array) {
System.out.print(i + " ");
}
}
}
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 void quickSortReverse(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 bubbleSortReverse(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 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;
}
}
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 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);
}
}
}
}
public static void swap(int array2[], int first, int second) {
int hold = array2[first];
array2[first] = array2[second];
array2[second] = hold;
}
}
Aucun commentaire:
Enregistrer un commentaire