Java常用アルゴリズム挿入、泡立ち、快速ソート大放送
2250 ワード
基礎思想は復述していないが、javaの実現は以下の通りである.
import java.util.Arrays;
/**
* Created by ykanghe on 12/27/16.
*/
public class algorithm {
/**
*
*
* @param a
* @param left
* @param right
* @return
*/
public static int quickPostSort(int[] a, int left, int right) {
int low = left;
int high = right;
int value = a[low];
while (low < high) {
while (low < high && value <= a[high]) {
high--;
}
a[low] = a[high];
while (low < high && value >= a[low]) {
low++;
}
a[high] = a[low];
}
a[low] = value;
return low;
}
public static void quickSort(int[] a, int left, int right) {
if (left < right) {
int q = quickPostSort(a, left, right);
quickSort(a, left, q - 1);
quickSort(a, q + 1, right);
}
}
/**
*
*
* @param a
* @return
*/
public static void blumSort(int[] a) {
if (a.length > 0) {
int tmp = 0;
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
if (a[j] > a[j + 1]) {
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
}
}
/**
*
*
* @param a
*/
public static void insertSort(int[] a) {
for (int i = 1; i < a.length; i++) {
int key = a[i];
int j = i - 1;
while (j >= 0 && a[j] > key) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = key;
}
}
public static void main(String[] arg) {
int[] a = {49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 34, 15, 35, 25, 53, 51};
// blumSort(a);//
// quickSort(a, 0, a.length - 1);//
insertSort(a);//
String s = Arrays.toString(a);
System.out.println(s);
}
}