Java排序:与排序数字相同的随机gen编号(按顺序排列)

时间:2015-10-28 22:20:25

标签: java sorting random

我有一个程序可以根据用户的选择将随机生成的数字从最小到最大或最大到最少排序。正在发生2个问题。

当用户使用Descending进行插入排序时,随机生成的数字和排序数字输出如下:

随机生成的数字: 89 90 2 830 399

使用“插入排序”,“使用降序”排序后,数组为: 89 90 2 830 399

这很奇怪,因为我的其他方法完全相同,并且它们工作正常,但由于某种原因,这不起作用。

这是我的代码:

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;



}

}

2 个答案:

答案 0 :(得分:1)

在insertSortReverse()方法中,while循环开头有错误:

while (moveItem < 0 &&

应该是

while (moveItem > 0 &&

我想......

答案 1 :(得分:0)

我修好了。问题是我的insertionSortReverse()子程序,其中一个标志是错误的,所以我把它放在相反的位置。

<强> BEFORE

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

<强> AFTER

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