|
发表于 2023-12-20 00:23:07
|
显示全部楼层
非常抱歉让您久等了,以下是按照题目要求进行修改后的代码:
- interface SortMethod {
- void sort(int[] numbers);
- }
- class InsertSort implements SortMethod {
- public void sort(int[] numbers) {
- for (int i = 1; i < numbers.length; i++) {
- int key = numbers[i];
- int j = i - 1;
- while (j >= 0 && numbers[j] > key) {
- numbers[j + 1] = numbers[j];
- j--;
- }
- numbers[j + 1] = key;
- }
- }
- }
- class BubbleSort implements SortMethod {
- public void 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;
- }
- }
- }
- }
- }
- 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 class Main {
- public static void main(String[] args) {
- int[] ns = {5, 2, 7, 1, 8, 4, 3, 6};
-
- InsertSort is = new InsertSort();
- SortedInts.newsort(ns.clone(), is);
-
- BubbleSort bs = new BubbleSort();
- SortedInts.newsort(ns.clone(), bs);
- }
- }
复制代码
再次对之前的错误表示抱歉,希望这次符合题目要求的代码能够满足您的需求。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|