SortImp.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package jp.ac.utsunomiya_u.is;public interface SortImp { /** * 整列 * @param x 整列対象 */ void sort(int[] x); /** * 配列の表示 * @param x 配列 */ void show(int[] x);} |
BubbleSortImp.java
1 2 3 4 | package jp.ac.utsunomiya_u.is;public class BubbleSortImp implements SortImp {} |
SortImpTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package jp.ac.utsunomiya_u.is;public class SortImpTest { public static void main(String[] args) { int[] x = {8, 3, 6, 4}; BubbleSortImp bubbleSort = new BubbleSortImp(); sortTest(x, bubbleSort); } static void sortTest(int[] x, SortImp sortImp) { sortImp.sort(x); }} |
BubbleSortImp.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | package jp.ac.utsunomiya_u.is;public class BubbleSortImp implements SortImp { @Override public void sort(int[] x) { int n = x.length; int t; for (int i = 0; i < n - 1; i++) { for (int j = n - 1; j > i; j--) { show(x); if (x[j - 1] > x[j]) { t = x[j]; x[j] = x[j - 1]; x[j - 1] = t; } } } show(x); } @Override public void show(int[] x) { System.out.println(""); for (int e : x) { System.out.print(e + " : "); for (int i = 0; i < e; ++i) { System.out.print("*"); } System.out.println(""); } }} |
出力
8 : ******** 3 : *** 6 : ****** 4 : **** 8 : ******** 3 : *** 4 : **** 6 : ****** 8 : ******** 3 : *** 4 : **** 6 : ****** 3 : *** 8 : ******** 4 : **** 6 : ****** 3 : *** 8 : ******** 4 : **** 6 : ****** 3 : *** 4 : **** 8 : ******** 6 : ****** 3 : *** 4 : **** 6 : ****** 8 : ********
SelectionSortImp.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package jp.ac.utsunomiya_u.is;public class SelectionSortImp implements SortImp { @Override public void sort(int[] x) { int n = x.length; int lowest, lowkey; int t; for (int i = 0; i < n - 1; i++) { lowest = i; lowkey = x[i]; for (int j = i + 1; j < n; j++) { show(x); if (x[j] < lowkey) { lowest = j; lowkey = x[j]; } } t = x[i]; x[i] = x[lowest]; x[lowest] = t; } show(x); } @Override public void show(int[] x) { System.out.println(""); for (int e : x) { System.out.print(e + " : "); for (int i = 0; i < e; ++i) { System.out.print("@"); } System.out.println(""); } }} |
SortImpTest.javaの修正
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package jp.ac.utsunomiya_u.is;public class SortImpTest { public static void main(String[] args) { int[] x = {8, 3, 6, 4}; /* BubbleSortImp bubbleSort = new BubbleSortImp(); sortTest(x, bubbleSort); */ SelectionSortImp selectSort = new SelectionSortImp(); sortTest(x, selectSort); } static void sortTest(int[] x, SortImp sortImp) { sortImp.sort(x); }} |
出力
8 : @@@@@@@@ 3 : @@@ 6 : @@@@@@ 4 : @@@@ 8 : @@@@@@@@ 3 : @@@ 6 : @@@@@@ 4 : @@@@ 8 : @@@@@@@@ 3 : @@@ 6 : @@@@@@ 4 : @@@@ 3 : @@@ 8 : @@@@@@@@ 6 : @@@@@@ 4 : @@@@ 3 : @@@ 8 : @@@@@@@@ 6 : @@@@@@ 4 : @@@@ 3 : @@@ 4 : @@@@ 6 : @@@@@@ 8 : @@@@@@@@ 3 : @@@ 4 : @@@@ 6 : @@@@@@ 8 : @@@@@@@@
SortExt.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | package jp.ac.utsunomiya_u.is;public abstract class SortExt { /** * 表示のためのマーク */ private String mark = "*"; /** * セッタ for マーク * * @param mark マーク */ public void setMark(String mark) { this.mark = mark; } /** * ゲッタ for マーク * * @return マーク */ public String getMark() { return mark; } /** * 整列の抽象メソッド * * @param x 整列対象の配列 */ abstract void sort(int[] x); /** * 配列の表示 * * @param x */ void show(int[] x) { System.out.println(""); for (int e : x) { System.out.print(e + " : "); for (int i = 0; i < e; ++i) { System.out.print(mark); } System.out.println(""); } }} |
BubbleSortExt.java
1 2 3 4 | package jp.ac.utsunomiya_u.is;public class BubbleSortExt extends SortExt {} |
SortExtTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package jp.ac.utsunomiya_u.is;public class SortExtTest { public static void main(String[] args) { int[] x = {8, 3, 6, 4}; BubbleSortExt bubbleSortExt = new BubbleSortExt(); sortTest(x, bubbleSortExt); } static void sortTest(int[] x, SortExt sortExt) { sortExt.sort(x); }} |
BubbleSortExt.javaへの追加部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @Overridevoid sort(int[] x) { int n = x.length; int t; for (int i = 0; i < n - 1; i++) { for (int j = n - 1; j > i; j--) { show(x); if (x[j - 1] > x[j]) { t = x[j]; x[j] = x[j - 1]; x[j - 1] = t; } } } show(x);} |