对整型数组排序的静态方法代码如下:
class SortedInts {
public static void newsort(int[] numbers, SortMethod s) {
s.sort(numbers);
for (int n : numbers) {
System.out.printf("%d ", n);
}
System.out.println();
}
}
其中SortMethod是一个接口,请定义该接口,并定义2个类实现该接口,并分别在这两个实现类中使用直接插入排序和冒泡排序实现sort方法。
然后在main方法中输入一个长度为8的数组,分别用两个实现类的对象作为实际参数调用newsort方法进行排序。例如:
int[] ns = new int[8];
......
InsertSort is = new InsertSort();
SortedInts.newsort(ns, is);
程序输入输出如下:
9 3 5 2 1 7 23 8
1 2 3 5 7 8 9 23
1 2 3 5 7 8 9 23
(注意:最后有一行空行)
import java.util.*;
public class Test {
public static void main(String[] args){
int[] ns1 = new int[8];
int[] ns2 = new int[8];
Scanner sc = new Scanner(System.in);
for(int i = 0;i < 8;i++){
ns1[i] = sc.nextInt();
ns2[i] = ns1[i];
}
sc.close();
InsertSort is = new InsertSort();
SortedInts.newsort(ns1, is);
BubbleSort bs = new BubbleSort();
SortedInts.newsort(ns2, bs);
}
}
class SortedInts {
public static void newsort(int[] numbers, SortMethod s) {
s.sort(numbers);
for (int n : numbers) {
System.out.printf("%d ", n);
}
System.out.println();
}
}
public interface SortMethod {
public void sort(int[] number);
}
public class InsertSort implements SortMethod{
@Override
public void sort(int[] number) {
for(int i=1; i<number.length; i++){
for(int j=i; j>0; j--){
if(number[j] < number[j-1]){
int temp = number[j-1];
number[j-1] = number[j];
number[j] = temp;
}
}
}
}
}
public class BubbleSort implements SortMethod{
@Override
public void sort(int[] number) {
for(int i = 0;i < number.length - 1;i++){
for(int j = 0;j < number.length - 1 - i;j++){
if(number[j] > number[j+1]){
int temp = number[j];
number[j] = number[j+1];
number[j+1] = temp;
}
}
}
}
}
SortMethod
public interface SortMethod {
void sort(int[] numbers);
}
PopSort
public class PopSort implements SortMethod{
@Override
public void sort(int[] num) {
int temp ;
for(int i = 0 ; i < num.length-1; i++){
for(int j = 0; j <num.length-1;j++){
if(num[j]>num[j+1]){
temp = num[j];
num[j] = num[j+1];
num[j+1] = temp;
}
}
}
}
}
InsertSort
public class InsertSort implements SortMethod {
@Override
public void sort(int[] num) {
int j;
for (int i = 1; i < num.length ; i++) {
int temp = num[i];
for (j = i - 1;j >= 0&&num[j] > temp ;j--)
{
num[j + 1] = num[j];
}
num[j + 1] = temp;
}
}
}
Test 测试类
public class Test {
public static void main(String[] args) {
newsort(new int[]{9 ,3, 5, 2, 1, 7, 23, 8}, new InsertSort());
newsort(new int[]{9 ,3, 5, 2, 1, 7, 23, 8}, new PopSort());
}
public static void newsort(int[] numbers, SortMethod s) {
s.sort(numbers);
for (int n : numbers) {
System.out.printf("%d ", n);
}
System.out.println();
}
}
共四个文件
接口
public interface SortMethod {
int[] sort(int[] numbers);
}
冒泡排序实现类
public class BubbleSort implements SortMethod {
@Override
public int[] sort(int[] numbers) {
for(int i=0;i<numbers.length-1;i++)
{
for(int j=0;j<numbers.length-1-i;j++)
{
if(numbers[j]>numbers[j+1])
{
int temp=numbers[j];
numbers[j]=numbers[j+1];
numbers[j+1]=temp;
}
}
}
return numbers;
}
}
插入排序实现类
public class InsertSort implements SortMethod {
@Override
public int[] sort(int[] numbers) {
for(int index = 1; index<numbers.length; index++){//外层向右的index,即作为比较对象的数据的index
int temp = numbers[index];//用作比较的数据
int leftindex = index-1;
while(leftindex>=0 && numbers[leftindex]>temp){//当比到最左边或者遇到比temp小的数据时,结束循环
numbers[leftindex+1] = numbers[leftindex];
leftindex--;
}
numbers[leftindex+1] = temp;//把temp放到空位上
}
return numbers;
}
}
使用类
public class SortedInts {
public static void newsort(int[] numbers, SortMethod s) {
s.sort(numbers);
for (int n : numbers) {
System.out.printf("%d ", n);
}
System.out.println();
}
}
实验代码
@Test
public void test4(){
Random random = new Random();
Set<Integer> integerSet = new HashSet<>();
int[] ns = new int[8];
for (int i = 0 ; i < 8 ; i++){
integerSet.add(random.nextInt(50));
}
System.out.println("原始数列");
System.out.println(integerSet);
int i = 0;
for (Integer integer : integerSet){
ns[i] = integer;
i++;
}
System.out.println(ns.toString());
InsertSort is = new InsertSort();
System.out.println("插入排序");
SortedInts.newsort(ns, is);
BubbleSort bubbleSort = new BubbleSort();
System.out.println("冒泡排序");
SortedInts.newsort(ns, bubbleSort);
}
运行结果
原始数列
[0, 32, 33, 49, 21, 10, 11, 43]
[I@2cfb4a64
插入排序
0 10 11 21 32 33 43 49
冒泡排序
0 10 11 21 32 33 43 49
完全按照你的要求来的哦,希望题主采纳一下^-^