Java:反向排序方法
Java: Reverse sorting methods
所以这些排序方法按照从小到大的顺序对数字进行排序,我该如何反转这些?我试过反转操作,但似乎不起作用:/
仍在努力学习Java,感谢您的帮助
//Selection Sort Method
public static void SelectionSort(int a[], int n) {
int smallest;
for (int i = 0; i < a.length - 1; i++) {
smallest = i;
//see if there is a smaller number further in the array
for (int index = i + 1; index < a.length; index++) {
if (a[index] < a[smallest]) {
swap(a, smallest, index);
}
}
}
}
//Selection Sort swap method to call on
public static void swap(int array2[], int first, int second) {
int hold = array2[first];
array2[first] = array2[second];
array2[second] = hold;
}
//Bubble Sort Method
public static void BubbleSort(int a[], int n) {
int i, j, t = 0;
for (i = 0; i < n; i++) {
for (j = 1; j < (n - i); j++) {
if (a[j - 1] > a[j]) {
t = a[j - 1];
a[j - 1] = a[j];
a[j] = t;
}
}
}
}
//Insertion Sort Method
public static void InsertionSort(int a[], int n) {
int insert;
for (int next = 1; next < a.length; next++) {
insert = a[next];
int moveItem = next;
while (moveItem > 0 && a[moveItem - 1] > insert) {
a[moveItem] = a[moveItem - 1];
moveItem--;
}
a[moveItem] = insert;
}
}
//Quick Sort Method
public static void QuickSort(int a[], int low, int high) {
int partitionLoc;
if (low < high) {
partitionLoc = partition(a, low, high);
QuickSort(a, low, partitionLoc - 1);
QuickSort(a, partitionLoc + 1, high);
}
}
public static int partition(int a2[], int left, int right) {
boolean moveLeft = true;
int separator = a2[left];
while (left < right) {
if (moveLeft == true) {
while ((a2[right] >= separator) && (left < right)) {
right--;
}
a2[left] = a2[right];
moveLeft = false;
} else {
while ((a2[left] <= separator) && (left < right)) {
left++;
}
a2[right] = a2[left];
moveLeft = true;
}
}
a2[left] = separator;
return left;
}
您可以保留您的方法并反转数组:
public static void reverse(int[] a) {
for (int i = 0 ; i < a.length / 2 ; i++) {
int t = a[i];
a[i] = a[a.length - 1 - i];
a[a.length - 1 - i] = t;
}
}
int[] a = // ...
whateverSort(a);
reverse(a);
只需反转每个排序中的比较:
选择排序
a[index] < a[smallest] => a[index] > a[smallest]
冒泡排序
a[j - 1] > a[j] => a[j - 1] < a[j]
插入排序
a[moveItem - 1] > insert => a[moveItem - 1] < insert
快速排序
(a2[right] >= separator) => (a2[right] <= separator)
(a2[left] <= separator) => (a2[left] >= separator)
所以这些排序方法按照从小到大的顺序对数字进行排序,我该如何反转这些?我试过反转操作,但似乎不起作用:/
仍在努力学习Java,感谢您的帮助
//Selection Sort Method
public static void SelectionSort(int a[], int n) {
int smallest;
for (int i = 0; i < a.length - 1; i++) {
smallest = i;
//see if there is a smaller number further in the array
for (int index = i + 1; index < a.length; index++) {
if (a[index] < a[smallest]) {
swap(a, smallest, index);
}
}
}
}
//Selection Sort swap method to call on
public static void swap(int array2[], int first, int second) {
int hold = array2[first];
array2[first] = array2[second];
array2[second] = hold;
}
//Bubble Sort Method
public static void BubbleSort(int a[], int n) {
int i, j, t = 0;
for (i = 0; i < n; i++) {
for (j = 1; j < (n - i); j++) {
if (a[j - 1] > a[j]) {
t = a[j - 1];
a[j - 1] = a[j];
a[j] = t;
}
}
}
}
//Insertion Sort Method
public static void InsertionSort(int a[], int n) {
int insert;
for (int next = 1; next < a.length; next++) {
insert = a[next];
int moveItem = next;
while (moveItem > 0 && a[moveItem - 1] > insert) {
a[moveItem] = a[moveItem - 1];
moveItem--;
}
a[moveItem] = insert;
}
}
//Quick Sort Method
public static void QuickSort(int a[], int low, int high) {
int partitionLoc;
if (low < high) {
partitionLoc = partition(a, low, high);
QuickSort(a, low, partitionLoc - 1);
QuickSort(a, partitionLoc + 1, high);
}
}
public static int partition(int a2[], int left, int right) {
boolean moveLeft = true;
int separator = a2[left];
while (left < right) {
if (moveLeft == true) {
while ((a2[right] >= separator) && (left < right)) {
right--;
}
a2[left] = a2[right];
moveLeft = false;
} else {
while ((a2[left] <= separator) && (left < right)) {
left++;
}
a2[right] = a2[left];
moveLeft = true;
}
}
a2[left] = separator;
return left;
}
您可以保留您的方法并反转数组:
public static void reverse(int[] a) {
for (int i = 0 ; i < a.length / 2 ; i++) {
int t = a[i];
a[i] = a[a.length - 1 - i];
a[a.length - 1 - i] = t;
}
}
int[] a = // ...
whateverSort(a);
reverse(a);
只需反转每个排序中的比较:
选择排序
a[index] < a[smallest] => a[index] > a[smallest]
冒泡排序
a[j - 1] > a[j] => a[j - 1] < a[j]
插入排序
a[moveItem - 1] > insert => a[moveItem - 1] < insert
快速排序
(a2[right] >= separator) => (a2[right] <= separator) (a2[left] <= separator) => (a2[left] >= separator)