Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/jp/co/cis/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package jp.co.cis;

import jp.co.cis.sort.BubbleSort;
import jp.co.cis.sort.SelectionSort;

public class Main {

Expand All @@ -9,7 +9,6 @@ public class Main {
* @param args
*/
public static void main(String[] args) {
BubbleSort bubbleSort = new BubbleSort();

// 並び替え対象配列生成
int in[] = createArray();
Expand All @@ -18,8 +17,13 @@ public static void main(String[] args) {
System.out.println("before---------");
print(in);

// 並び替え
int[] out = bubbleSort.sort(in);
// バブルソート
// BubbleSort bubbleSort = new BubbleSort();
// int[] out = bubbleSort.sort(in);

// 選択ソート
SelectionSort sort = new SelectionSort();
int[] out = sort.sort(in);

// 変更後表示
System.out.println("after---------");
Expand Down
47 changes: 29 additions & 18 deletions src/jp/co/cis/sort/BubbleSort.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
package jp.co.cis.sort;

/**
* バブルソートで並び替えるクラスです。
* 選択ソートで配列の値を並び替えるクラスです。
*
*/
public class BubbleSort {
public class SelectionSort {

/**
* 配列の値を小さい順に並び替えた結果を返却します。
* @param ary
* @return
*/
public int[] sort(int[] ary) {

/**
* aryLengthで配列の長さを取得
* changeNumは配列の入れ替えをする回数を表します
*/
int aryLength = ary.length;
int changeNum = aryLength - 1;
for(int i=changeNum; i>0; i--){
for(int j=0; j<i; j++){
if(ary[j]>ary[j+1]){
int temNum = ary[j+1];
ary[j+1] = ary[j];
ary[j] = temNum;
}
//受け取る配列の長さ
int aryLength = ary.length;
//最大値を探す処理をする回数
int changeNum = aryLength - 1;

for (int i = 1; i <= changeNum; i++) {
//左端(最大値を入れる場所)のindex番号
int tempAryIndex = i - 1;
//最大値があるindex番号を受け取る
int maxAryIndex = i - 1;
//最大値を受け取る
int maxAryNum = ary[i - 1];

//最大値をもつindex番号を調べる
for (int j = tempAryIndex; j <= changeNum; j++) {
if (maxAryNum < ary[j]) {
maxAryNum = ary[j];
maxAryIndex = j;
}
}

//左端と最大値を持つ配列要素を入れ替える
int tempNum = ary[tempAryIndex];
ary[tempAryIndex] = ary[maxAryIndex];
ary[maxAryIndex] = tempNum;
}
}
return ary;

return ary;
}

}
45 changes: 45 additions & 0 deletions src/jp/co/cis/sort/SelectionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package jp.co.cis.sort;

/**
* 選択ソートで配列の値を並び替えるクラスです。
*
*/
public class SelectionSort {

/**
* 配列の値を小さい順に並び替えた結果を返却します。
* @param ary
* @return
*/
public int[] sort(int[] ary) {
//受け取る配列の長さ
int aryLength = ary.length;
//最大値を探す処理をする回数
int changeNum = aryLength - 1;

for (int i = 1; i <= changeNum; i++) {
//左端(最大値を入れる場所)のindex番号
int tempAryIndex = i - 1;
//最大値があるindex番号を受け取る
int maxAryIndex = i - 1;
//最大値を受け取る
int maxAryNum = ary[i - 1];

//最大値をもつindex番号を調べる
for (int j = tempAryIndex; j <= changeNum; j++) {
if (maxAryNum < ary[j]) {
maxAryNum = ary[j];
maxAryIndex = j;
}
}

//左端と最大値を持つ配列要素を入れ替える
int tempNum = ary[tempAryIndex];
ary[tempAryIndex] = ary[maxAryIndex];
ary[maxAryIndex] = tempNum;
}

return ary;
}

}