Java實現幾種常見排序方法
阿新 • • 發佈:2018-12-30
package test.sort;
import java.util.Random;
//Java實現的排序類 publicclass NumberSort {
//私有構造方法,禁止例項化 private NumberSort() {
super();
}
//冒泡法排序 publicstaticvoid bubbleSort(int[] numbers) {
int temp; // 記錄臨時中間值 int size = numbers.length; // 陣列大小 for (int i =0; i < size -1; i++) {
for (int j = i +1; j < size; j++) {
if (numbers[i] < numbers[j]) { // 交換兩數的位置 temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
}
//快速排序publicstaticvoid quickSort(int[] numbers, int start, int end) {
if (start < end) {
int base = numbers[start]; // 選定的基準值(第一個數值作為基準值) int temp; // 記錄臨時中間值 int i = start, j = end;
do {
while ((numbers[i] < base) && (i < end))
i ++;
while ((numbers[j] > base) && (j > start))
j--;
if (i <= j) {
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
i++;
j--;
}
} while (i <= j);
if (start < j)
quickSort(numbers, start, j);
if (end > i)
quickSort(numbers, i, end);
}
}
//選擇排序 publicstaticvoid selectSort(int[] numbers) {
int size = numbers.length, temp;
for (int i =0; i < size; i++) {
int k = i;
for (int j = size -1; j > i; j--) {
if (numbers[j] < numbers[k])
k = j;
}
temp = numbers[i];
numbers[i] = numbers[k];
numbers[k] = temp;
}
}
//插入排序
// @param numbers publicstaticvoid insertSort(int[] numbers) {
int size = numbers.length, temp, j;
for (int i =1; i < size; i++) {
temp = numbers[i];
for (j = i; j >0&& temp < numbers[j -1]; j--)
numbers[j] = numbers[j -1];
numbers[j] = temp;
}
}
//歸併排序 publicstaticvoid mergeSort(int[] numbers, int left, int right) {
int t =1;// 每組元素個數 int size = right - left +1;
while (t < size) {
int s = t;// 本次迴圈每組元素個數 t =2* s;
int i = left;
while (i + (t -1) < size) {
merge(numbers, i, i + (s -1), i + (t -1));
i += t;
}
if (i + (s -1) < right)
merge(numbers, i, i + (s -1), right);
}
}
//歸併演算法實現 privatestaticvoid merge(int[] data, int p, int q, int r) {
int[] B =newint[data.length];
int s = p;
int t = q +1;
int k = p;
while (s <= q && t <= r) {
if (data[s] <= data[t]) {
B[k] = data[s];
s++;
} else {
B[k] = data[t];
t++;
}
k++;
}
if (s == q +1)
B[k++] = data[t++];
else
B[k++] = data[s++];
for (int i = p; i <= r; i++)
data[i] = B[i];
}
}
import java.util.Random;
//Java實現的排序類 publicclass NumberSort {
//私有構造方法,禁止例項化 private NumberSort() {
super();
}
//冒泡法排序 publicstaticvoid bubbleSort(int[] numbers) {
int temp; // 記錄臨時中間值 int size = numbers.length; // 陣列大小 for (int i =0; i
for (int j = i +1; j < size; j++) {
if (numbers[i] < numbers[j]) { // 交換兩數的位置 temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
}
if (start < end) {
int base = numbers[start]; // 選定的基準值(第一個數值作為基準值) int temp; // 記錄臨時中間值 int i = start, j = end;
do {
while ((numbers[i] < base) && (i < end))
i
while ((numbers[j] > base) && (j > start))
j--;
if (i <= j) {
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
i++;
j--;
}
} while (i <= j);
if (start < j)
quickSort(numbers, start, j);
if (end > i)
quickSort(numbers, i, end);
}
}
//選擇排序 publicstaticvoid selectSort(int[] numbers) {
int size = numbers.length, temp;
for (int i =0; i < size; i++) {
int k = i;
for (int j = size -1; j > i; j--) {
if (numbers[j] < numbers[k])
k = j;
}
temp = numbers[i];
numbers[i] = numbers[k];
numbers[k] = temp;
}
}
//插入排序
// @param numbers publicstaticvoid insertSort(int[] numbers) {
int size = numbers.length, temp, j;
for (int i =1; i < size; i++) {
temp = numbers[i];
for (j = i; j >0&& temp < numbers[j -1]; j--)
numbers[j] = numbers[j -1];
numbers[j] = temp;
}
}
//歸併排序 publicstaticvoid mergeSort(int[] numbers, int left, int right) {
int t =1;// 每組元素個數 int size = right - left +1;
while (t < size) {
int s = t;// 本次迴圈每組元素個數 t =2* s;
int i = left;
while (i + (t -1) < size) {
merge(numbers, i, i + (s -1), i + (t -1));
i += t;
}
if (i + (s -1) < right)
merge(numbers, i, i + (s -1), right);
}
}
//歸併演算法實現 privatestaticvoid merge(int[] data, int p, int q, int r) {
int[] B =newint[data.length];
int s = p;
int t = q +1;
int k = p;
while (s <= q && t <= r) {
if (data[s] <= data[t]) {
B[k] = data[s];
s++;
} else {
B[k] = data[t];
t++;
}
k++;
}
if (s == q +1)
B[k++] = data[t++];
else
B[k++] = data[s++];
for (int i = p; i <= r; i++)
data[i] = B[i];
}
}