SortImp.java
package jp.ac.utsunomiya_u.is;
public interface SortImp {
/**
* 整列
* @param x 整列対象
*/
void sort(int[] x);
/**
* 配列の表示
* @param x 配列
*/
void show(int[] x);
}
BubbleSortImp.java
package jp.ac.utsunomiya_u.is;
public class BubbleSortImp implements SortImp {
}
SortImpTest.java
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
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
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の以下の部分を修正して実行してみましょう.
SortImpTest.javaの修正
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
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
package jp.ac.utsunomiya_u.is;
public class BubbleSortExt extends SortExt {
}
SortExtTest.java
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への追加部分
@Override
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);
}