如何在java中实现降序选择排序?
how to implement a descending selection sort in java?
我想实现一个选择排序方法,它接受一个整数数组并按降序对它进行排序。然而,诀窍是保持原来的选择排序方法不变,而是使用简单的算术运算,并且不添加额外的循环来在数组完成排序后交换元素。这是我的代码,想法是将最大值和最小值的位置存储在局部变量中,并在内循环完成迭代后将它们与相应的位置交换。我什至尝试只使用一个变量来找到最低值并将其放在数组的末尾,但我失败了,我得到了错误的结果,我需要帮助来发现错误。这是我的代码
public static void newSortMethod(int[]a){
for(int i = 0; i < a.length-1; i++){
int maxPosition=i;
int minPosition=i;
for(int j = i+1; j < a.length; j++){
if(a[j] < a[minPosition]){
minPosition = j;
}
if(a[j] > a[maxPosition]){
maxPosition = j;
}
}
swap(a,maxPosition,i);
swap(a,minPosition,a.length-i-1);
}
System.out.println();
}
public static void swap(int[]a, int i, int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void main(String[] args) {
int[] a = {2,6,3,9,5,4,8,7,0,13,-3,1};
newSortMethod(a);
}
这是到目前为止程序的输出
-3 8 2 9 13 5 4 6 3 1 7 0
你原来的算法是错误的。首先,if
块应该与 minPosition
和 maxPosition
进行比较,而不是 i
。其次,如果您同时选择最小值 和最大值 ,那么您的内部 for 循环应该停止在 a.length - i
,而不是 a.length
(因为顶部 i
元素也被排序)。两者都做,这就是升序算法。
public static void newSortMethod(int[]a){
for(int i = 0; i < a.length; i++){
int maxPosition=i;
int minPosition=i;
for(int j = i+1; j < a.length - i; j++){
if(a[j] < a[minPosition]){
minPosition = j;
}
if(a[j] > a[maxPosition]){
maxPosition = j;
}
}
swap(a,maxPosition,i);
swap(a,minPosition,a.length-i-1);
}
}
要切换到降序,只需添加一行。
public static void newSortMethod(int[]a){
for(int i = 0; i < a.length; i++){
int maxPosition=i;
int minPosition=i;
for(int j = i+1; j < a.length - i; j++){
if(a[j] < a[minPosition]){
minPosition = j;
}
if(a[j] > a[maxPosition]){
maxPosition = j;
}
}
swap(a,minPosition,maxPosition); // <-- this line
swap(a,maxPosition,i);
swap(a,minPosition,a.length-i-1);
}
}
错误
首先,我们来查找您的代码中的问题。有一些,这在编程中经常发生。
- 您的代码仍在尝试使用
swap(a,minPosition,i)
进行升序排序,然后尝试将最大值放在末尾,这不是您想要的:您想将最大值放在开头。
- 您的
n
永远不会被修改,因此您将继续打印 0
。
示例解决方案
现在让我们看看有用的东西。我不太确定你的 升序 选择排序是什么样的,但我想它应该是这样的:
public static void ascendingSortMethod(int[]a){
int n = 0; // this is only to count how many times the swap method was called
for(int i = 0; i < a.length-1; i++){
int minPosition = i;
for(int j = i+1; j < a.length; j++){
if(a[j] < a[minPosition]){
minPosition = j;
}
}
if(minPosition != i){ // check whether swap is necessary
swap(a,minPosition,i);
n ++;
}
}
System.out.println(n);
}
要使其按降序排序,只需切换比较运算符(为清楚起见,可能还有 minPosition
标识符)。
public static void newSortMethod(int[]a){
int n = 0; // this is only to count how many times the swap method was called
for(int i = 0; i < a.length-1; i++){
int maxPosition = i;
for(int j = i+1; j < a.length; j++){
if(a[j] > a[maxPosition]){ // switched comparison operator
maxPosition = j;
}
}
if(maxPosition != i){ // check whether swap is necessary
swap(a,maxPosition,i);
n ++;
}
}
System.out.println(n);
}
我想实现一个选择排序方法,它接受一个整数数组并按降序对它进行排序。然而,诀窍是保持原来的选择排序方法不变,而是使用简单的算术运算,并且不添加额外的循环来在数组完成排序后交换元素。这是我的代码,想法是将最大值和最小值的位置存储在局部变量中,并在内循环完成迭代后将它们与相应的位置交换。我什至尝试只使用一个变量来找到最低值并将其放在数组的末尾,但我失败了,我得到了错误的结果,我需要帮助来发现错误。这是我的代码
public static void newSortMethod(int[]a){
for(int i = 0; i < a.length-1; i++){
int maxPosition=i;
int minPosition=i;
for(int j = i+1; j < a.length; j++){
if(a[j] < a[minPosition]){
minPosition = j;
}
if(a[j] > a[maxPosition]){
maxPosition = j;
}
}
swap(a,maxPosition,i);
swap(a,minPosition,a.length-i-1);
}
System.out.println();
}
public static void swap(int[]a, int i, int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void main(String[] args) {
int[] a = {2,6,3,9,5,4,8,7,0,13,-3,1};
newSortMethod(a);
}
这是到目前为止程序的输出 -3 8 2 9 13 5 4 6 3 1 7 0
你原来的算法是错误的。首先,if
块应该与 minPosition
和 maxPosition
进行比较,而不是 i
。其次,如果您同时选择最小值 和最大值 ,那么您的内部 for 循环应该停止在 a.length - i
,而不是 a.length
(因为顶部 i
元素也被排序)。两者都做,这就是升序算法。
public static void newSortMethod(int[]a){
for(int i = 0; i < a.length; i++){
int maxPosition=i;
int minPosition=i;
for(int j = i+1; j < a.length - i; j++){
if(a[j] < a[minPosition]){
minPosition = j;
}
if(a[j] > a[maxPosition]){
maxPosition = j;
}
}
swap(a,maxPosition,i);
swap(a,minPosition,a.length-i-1);
}
}
要切换到降序,只需添加一行。
public static void newSortMethod(int[]a){
for(int i = 0; i < a.length; i++){
int maxPosition=i;
int minPosition=i;
for(int j = i+1; j < a.length - i; j++){
if(a[j] < a[minPosition]){
minPosition = j;
}
if(a[j] > a[maxPosition]){
maxPosition = j;
}
}
swap(a,minPosition,maxPosition); // <-- this line
swap(a,maxPosition,i);
swap(a,minPosition,a.length-i-1);
}
}
错误
首先,我们来查找您的代码中的问题。有一些,这在编程中经常发生。
- 您的代码仍在尝试使用
swap(a,minPosition,i)
进行升序排序,然后尝试将最大值放在末尾,这不是您想要的:您想将最大值放在开头。 - 您的
n
永远不会被修改,因此您将继续打印0
。
示例解决方案
现在让我们看看有用的东西。我不太确定你的 升序 选择排序是什么样的,但我想它应该是这样的:
public static void ascendingSortMethod(int[]a){
int n = 0; // this is only to count how many times the swap method was called
for(int i = 0; i < a.length-1; i++){
int minPosition = i;
for(int j = i+1; j < a.length; j++){
if(a[j] < a[minPosition]){
minPosition = j;
}
}
if(minPosition != i){ // check whether swap is necessary
swap(a,minPosition,i);
n ++;
}
}
System.out.println(n);
}
要使其按降序排序,只需切换比较运算符(为清楚起见,可能还有 minPosition
标识符)。
public static void newSortMethod(int[]a){
int n = 0; // this is only to count how many times the swap method was called
for(int i = 0; i < a.length-1; i++){
int maxPosition = i;
for(int j = i+1; j < a.length; j++){
if(a[j] > a[maxPosition]){ // switched comparison operator
maxPosition = j;
}
}
if(maxPosition != i){ // check whether swap is necessary
swap(a,maxPosition,i);
n ++;
}
}
System.out.println(n);
}