java - 我想通过使用另一个带有交换方法的类而不是通常可用的通用交换函数来交换两个数字

标签 java algorithm sorting oop object

<分区>

这是冒泡排序算法的 Java 实现,我使用了另一个带有交换方法的类,当我在我的交换器类中不使用构造函数时这段代码工作正常但如果构造函数根本不交换数组存在。

import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;

class swapper {
  int x, y;

  void swap() {
    int temp = x;
    x = y;
    y = temp;
  }
}

public class Solution {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int[] a = new int[n];
    for (int a_i = 0; a_i < n; a_i++) {
      a[a_i] = in.nextInt();
    }
    int swaps = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n - i - 1; j++) {
        if (a[j] > a[j + 1]) {
          swaps++;
          swapper s = new swapper();
          s.x = a[j];
          s.y = a[j + 1];
          a[j] = s.y;
          a[j + 1] = s.x;
        }
      }
    }
    System.out.println(
        "Array is sorted in "
            + swaps
            + " swaps.\nFirst Element: "
            + a[0]
            + "\nLast Element: "
            + a[n - 1]);
  }
}

但是当我使用构造函数为我的“对象”分配 x 和 y 的值时,这段代码根本不交换任何元素。

import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;

class swapper {
  int x, y;

  swapper(int a, int b) {
    x = a;
    y = b;
  }

  void swap() {
    int temp = x;
    x = y;
    y = temp;
  }
}

public class Solution {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int[] a = new int[n];
    for (int a_i = 0; a_i < n; a_i++) {
      a[a_i] = in.nextInt();
    }
    int swaps = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n - i - 1; j++) {
        if (a[j] > a[j + 1]) {
          swaps++;
          swapper s = new swapper(a[j], a[j + 1]);
          s.swap();
          a[j] = s.y;
          a[j + 1] = s.x;
        }
      }
    }
    System.out.println(
        "Array is sorted in "
            + swaps
            + " swaps.\nFirst Element: "
            + a[0]
            + "\nLast Element: "
            + a[n - 1]);
  }
}

这两种代码的唯一区别是存在一个构造函数来为实例变量赋值。

第一段代码手动赋值,而第二段代码使用构造函数。

最佳答案

您实质上是将两个数字交换两次,因此根本不交换它们:

swapper s = new swapper(a[j],a[j+1]); // this assigns a[j] to s.x and a[j+1] to s.y
s.swap(); // this swaps s.x and s.y
a[j] = s.y; // this assigns the original value of s.x (a[j]) to a[j]
a[j+1] = s.x; // this assigns the original value of s.y (a[j+1]) to a[j+1]

为了使交换按预期工作,将其更改为:

swapper s = new swapper(a[j],a[j+1]); 
s.swap();
a[j] = s.x;
a[j+1] = s.y;

关于java - 我想通过使用另一个带有交换方法的类而不是通常可用的通用交换函数来交换两个数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47772348/

相关文章:

algorithm - 黑客排名 : Inserting a Node Into a Sorted Doubly Linked List - Kotlin

java - 如何对elasticsearch中其他索引中的字段进行排序和过滤?

java - 需要一个 Java TreeMap<Integer, Character> 的快速替代品,它可以在不降低速度的情况下容纳许多映射

java - 防止命令行参数显示在 bash 历史记录或带有 bash 脚本的 ps -ef 中

java - 类对象的 equals() 和 ==

php - 查找字符串数组的公共(public)前缀

java - 读取int数组并检查它是否已排序

java - 更新叠加层时发生 MapView ConcurrentModificationException

java - 如何从其他方法访问java中的方法?

python - 迭代分而治之算法