Java语言怎么使用选择排序,对Point对象进行排序?问题的关键是Point的比较大小的依据是什么?查了很多资料都没有
效果如图 :
分析 :
Java里使用选择排序对Point对象进行排序,需要实现Comparable接口来定义Point对象的比较方式。Comparable接口包含一个compareTo方法,用于比较两个对象的大小。
思路 :创建一个Point类,并实现Comparable接口。
代码:
import java.util.Arrays;
class Point implements Comparable<Point> {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override
public int compareTo(Point other) {
// 按照x坐标进行比较
if (this.x < other.x) {
return -1;
} else if (this.x > other.x) {
return 1;
} else {
// 如果x坐标相同,则按照y坐标进行比较
if (this.y < other.y) {
return -1;
} else if (this.y > other.y) {
return 1;
} else {
return 0;
}
}
}
}
class SelectionSort {
public static void selectionSort(Point[] arr) {
int n = arr.length;
// 遍历数组,每次找到最小的元素并与当前位置交换
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
// 使用compareTo方法比较两个Point对象的大小
if (arr[j].compareTo(arr[minIndex]) < 0) {
minIndex = j;
}
}
// 将最小的元素与当前位置交换
Point temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
public class Main {
public static void main(String[] args) {
Point[] points = new Point[5];
points[0] = new Point(3, 8);
points[1] = new Point(2, 5);
points[2] = new Point(4, 7);
points[3] = new Point(1, 6);
points[4] = new Point(2, 3);
SelectionSort.selectionSort(points);
for (Point point : points) {
System.out.println("(" + point.getX() + ", " + point.getY() + ")");
}
}
}
你看看这个能不能满足你的需求,如果不行的话,请你讲的更详细一点你的需求是什么?
import java.util.*;
class Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "(" + x + ", " + y + ")";
}
}
class PointComparator implements Comparator<Point> {
@Override
public int compare(Point p1, Point p2) {
return Integer.compare(p1.x, p2.x);
}
}
public class PointSorter {
public static void selectionSort(Point[] points, Comparator<Point> comparator) {
int n = points.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (comparator.compare(points[j], points[minIndex]) < 0) {
minIndex = j;
}
}
// 交换位置
Point temp = points[minIndex];
points[minIndex] = points[i];
points[i] = temp;
}
}
public static void main(String[] args) {
Point[] points = { new Point(4, 3), new Point(2, 5), new Point(1, 1), new Point(3, 2) };
// 使用PointComparator来比较Point对象
PointComparator comparator = new PointComparator();
System.out.println("排序前:");
for (int i = 0; i < points.length; i++) {
System.out.println(points[i]);
}
selectionSort(points, comparator);
System.out.println("排序后:");
for (int i = 0; i < points.length; i++) {
System.out.println(points[i]);
}
}
}
实现思路:在上述代码中,首先定义了一个Point类来表示二维坐标点。然后,实现了一个PointComparator类,该类实现了Comparator接口,并重写了compare方法。在compare方法中,根据Point对象的x坐标进行比较。
在PointSorter类中,实现了选择排序的selectionSort方法。该方法接受一个Point数组和一个Comparator对象作为参数。在每一次外循环中,找到当前剩余元素中最小的元素的索引,并与当前位置进行交换。
在main方法中,创建了一个Point数组并初始化,然后创建了一个PointComparator对象。首先输出排序前的数组元素,然后调用selectionSort方法对数组进行排序,并最后输出排序后的数组元素。
这是运行截图:
解决方案:
选择排序算法是一种简单且直观的排序算法,其基本思想是每次从待排序数组中选择最小(或最大)的元素,并将其放在已排序部分的末尾。对于Point对象的排序,我们可以选择根据Point对象的特定属性进行比较大小,例如x坐标或y坐标。
步骤如下:
public class Point implements Comparable<Point> {
int x;
int y;
public Point(int a, int b) {
if ((a >= 0 && a <= 100) && (b >= 0 && b <= 100)) {
x = a;
y = b;
} else {
System.out.println("横坐标或纵坐标的取值不符合规范");
x = 1;
y = 1;
}
}
public void show() {
System.out.printf("当前的坐标为%d:%d\n", x, y);
}
@Override
public int compareTo(Point other) {
// 比较规则:先比较x坐标,如果相同再比较y坐标
if (this.x == other.x) {
return this.y - other.y;
} else {
return this.x - other.x;
}
}
public static void main(String[] args) {
Point p1 = new Point(88, 77);
p1.show();
Point p2 = new Point(150, 200);
p2.show();
}
}
public class Main {
public static void main(String[] args) {
Point[] points = new Point[5]; // 假设有5个点
// 初始化点集
points[0] = new Point(88, 77);
points[1] = new Point(150, 200);
points[2] = new Point(50, 100);
points[3] = new Point(200, 150);
points[4] = new Point(100, 50);
// 选择排序算法对点集进行排序
for (int i = 0; i < points.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < points.length; j++) {
if (points[j].compareTo(points[minIndex]) < 0) {
minIndex = j;
}
}
// 将最小元素交换至已排序部分的末尾
Point temp = points[minIndex];
points[minIndex] = points[i];
points[i] = temp;
}
// 打印排序结果
for (Point point : points) {
point.show();
}
}
}
以上便是使用选择排序算法对Point对象进行排序的具体解决方案。根据定义的比较规则,选择排序算法会按照指定的属性对Point对象进行排序。