Skip to content

Instantly share code, notes, and snippets.

@hiroism007
Created April 23, 2013 14:40
Show Gist options
  • Save hiroism007/5444103 to your computer and use it in GitHub Desktop.
Save hiroism007/5444103 to your computer and use it in GitHub Desktop.
バブルソート
/**
* Created with IntelliJ IDEA.
* User: hiroism007
* Date: 4/23/13
* Time: 11:17 PM
* To change this template use File | Settings | File Templates.
*/
public class BubbleSort {
static int[]testNumbers;
public static void main(String[] args){
testNumbers = new int[10];
for (int i = 0; i < testNumbers.length; i++){
testNumbers[i]= (int) (Math.random()*testNumbers.length + 1);
}
int arrCounts = testNumbers.length;
/**
* バブル
* ソートは隣り合う数値を比較して、一番大きいものをどんどん選んで決定していくソートです。
*/
//配列-1回まわせばすべてのソートが完了する。
for (int i = arrCounts-1; i>1; i--){
for (int j= 0; j< i; j++){
if (testNumbers[j]>testNumbers[j+1]){
swap(j, j+1);
}
}
}
for (int i = 0; i<arrCounts; i++) System.out.println(testNumbers[i] + "Sortedです");
}
public static void swap(int a, int b){
int tmp = testNumbers[b];
testNumbers[b] = testNumbers[a];
testNumbers[a] = tmp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment