使用Java在IntSort类中3个同名方法sort,实现排序。 sort(int a,int b); sort(int a,int b,int c); sort(int a[]);
时间: 2023-12-12 07:05:43 浏览: 28
可以在IntSort类中定义如下三个同名方法sort来实现排序:
```
public class IntSort {
// 排序方法1:对两个整数进行排序
public static void sort(int a, int b) {
if (a > b) {
System.out.println(b + " " + a);
} else {
System.out.println(a + " " + b);
}
}
// 排序方法2:对三个整数进行排序
public static void sort(int a, int b, int c) {
int temp;
if (a > b) {
temp = a;
a = b;
b = temp;
}
if (a > c) {
temp = a;
a = c;
c = temp;
}
if (b > c) {
temp = b;
b = c;
c = temp;
}
System.out.println(a + " " + b + " " + c);
}
// 排序方法3:对整型数组进行排序
public static void sort(int a[]) {
int temp;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
```
方法1和方法2都是直接对传入的参数进行比较,然后输出排序结果。方法3是使用了冒泡排序进行排序,然后输出排序结果。