Java实现排序算法,目前只实现了冒泡和快速
package sort;
import java.util.Arrays;
/**
*
* @author QuPeng
*
*/
public class SortAlgorithm {
/**
* @param args
*/
public static void main(String[] args) {
int[] array = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
int[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int[] array2 = { 6, 8, 2, 4, 9, 3, 5, 90, 3, 67, 3, 3, 78 };
int[] array3 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
System.out.println(Arrays.toString(array));
quickSort(array, 0, array.length - 1);
System.out.println(Arrays.toString(array));
System.out.println(Arrays.toString(array3));
bubbleSort(array3);
System.out.println(Arrays.toString(array3));
}
public static void bubbleSort(int[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j + 1] < array[j]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
public static void quickSort(int[] array, int lowIndex, int highIndex) {
if (lowIndex >= highIndex) {
return;
}
int i = lowIndex;
int j = highIndex;
int key = array[lowIndex];
while (true) {
while (i < j) {
if (array[j] < key) {
array[i] = array[j];
break;
}
j--;
}
while (i < j) {
if (array[i] > key) {
array[j] = array[i];
break;
}
i++;
}
if (i == j) {
array[j] = key;
quickSort(array, lowIndex, j - 1);
quickSort(array, j + 1, highIndex);
return;
}
System.out.println(Arrays.toString(array));
}
}
}