1.冒泡排序
public void bubbleSort(int[] score){
System.out.println("通过冒泡排序方法对数组进行排序:");
for (int i = 0; i < score.length - 1; i++) {
for (int j = 0; j < score.length - 1 - i; j++) {
if (score[j] > score[j + 1]) {
int temp = score[j + 1];
score[j + 1] = score[j];
score[j] = temp;
}
System.out.print(score[j] + " ");
}
System.out.print("【");
for (int j = score.length - 1 - i; j < score.length; j++) {
System.out.print(score[j] + " ");
}
System.out.println("】");
}
}
2.选择排除
public static void main(String[] args) {
int[] number = {13,15,24,99,4,1};
String end = "\n";
int index;
for (int i = 1;i < number.length;i++) {
index = 0;
for(int j = 1;j <= number.length-i;j++) {
if (number[j] > number[index]) {
index = j;
}
}
end = number[index] + " " + end;
int temp = number[number.length-i];
number[number.length-1] = number[index];
number[index] = temp;
System.out.print("【");
for (int j = 0;j < number.length-i;j++) {
System.out.print(number[j]+" ");
}
System.out.print("】"+end);
}
}
3.快速排序(三种写法)
public static void main(String[] args) {
int[] number={13,15,24,99,14,11,1,2,3};
System.out.println("排序前:");
for(int val:number) {
System.out.print(val+" ");
}
quickSort(number,0,number.length-1);
System.out.println("\n排序后:");
for(int val:number) {
System.out.print(val +" ");
}
}
public static void quickSort(int[] arr, int startIndex, int endIndex) {
if (startIndex >= endIndex) {
return;
}
int pivotIndex = singlePointer(arr, startIndex, endIndex);
quickSort(arr, startIndex, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, endIndex);
}
private static int doublePointerSwap(int[] arr, int startIndex, int endIndex) {
int pivot = arr[startIndex];
int leftPoint = startIndex;
int rightPoint = endIndex;
while (leftPoint < rightPoint) {
while (leftPoint < rightPoint
&& arr[rightPoint] > pivot) {
rightPoint--;
}
while (leftPoint < rightPoint
&& arr[leftPoint] <= pivot) {
leftPoint++;
}
if (leftPoint < rightPoint) {
int temp = arr[leftPoint];
arr[leftPoint] = arr[rightPoint];
arr[rightPoint] = temp;
}
}
arr[startIndex] = arr[rightPoint];
arr[rightPoint] = pivot;
return rightPoint;
}
private static int doublePointerHole(int[] arr, int startIndex, int endIndex) {
int pivot = arr[startIndex];
int leftPoint = startIndex;
int rightPoint = endIndex;
while (leftPoint < rightPoint) {
while (leftPoint < rightPoint
&& arr[rightPoint] > pivot) {
rightPoint--;
}
if (leftPoint < rightPoint) {
arr[leftPoint] = arr[rightPoint];
leftPoint++;
}
while (leftPoint < rightPoint
&& arr[leftPoint] <= pivot) {
leftPoint++;
}
if (leftPoint < rightPoint) {
arr[rightPoint] = arr[leftPoint];
rightPoint--;
}
}
arr[rightPoint] = pivot;
return rightPoint;
}
private static int singlePointer(int[] arr, int startIndex, int endIndex) {
int pivot = arr[startIndex];
int markPoint = startIndex;
for (int i = startIndex + 1; i <= endIndex; i++) {
if (arr[i] < pivot) {
markPoint++;
int temp = arr[markPoint];
arr[markPoint] = arr[i];
arr[i] = temp;
}
}
arr[startIndex] = arr[markPoint];
arr[markPoint] = pivot;
return markPoint;
}