1.冒泡排序
1.1.1 普通版本
public class BubbleSort1 {
public static void bubbleSort1(int[] arr){
if(arr.length < 2)
return;
for(int i = arr.length - 1;i > 0;i--){
for(int j = 0;j < i;j++){
if(arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] arr = new int[]{23,4,1,-5,-9,45,2,45,78,-14,0};
bubbleSort1(arr);
for(int n:arr){
System.out.print(n+" ");
}
}
}
1.1.2 优化版本
public class BubbleSort1OptimizedVersion {
public static void bubbleSort1OptimizedVersion(int[] arr){
if(arr.length < 2)
return;
boolean flag = true;
for(int i = arr.length - 1;i > 0;i--){
for(int j = 0;j < i;j++){
if(arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
flag = false;
}
}
if(flag)
return;
}
}
public static void main(String[] args) {
int[] arr = new int[]{23,4,1,-5,-9,45,2,45,78,-14,0};
bubbleSort1OptimizedVersion(arr);
for(int n:arr){
System.out.print(n+" ");
}
}
}
1.2.1 递归版本
public class BubbleSort2 {
public static void bubbleSort2(int[] arr,int length){
if(length < 2)
return;
for(int i = 0;i < length-1;i++){
if(arr[i] > arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
bubbleSort2(arr,length - 1);
}
public static void main(String[] args) {
int[] arr = new int[]{23,4,1,-5,-9,45,2,45,78,-14,0};
bubbleSort2(arr,arr.length);
for(int n:arr)
System.out.print(n+" ");
}
}
1.2.2 优化版本
public class BubbleSort2OptimizedVersion {
public static void bubbleSort2OptimizedVersion(int[] arr,int length){
if(length < 2)
return;
boolean flag = true;
for(int i = 0;i < length-1;i++){
if(arr[i] > arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
flag = false;
}
}
if(flag)
return;
bubbleSort2OptimizedVersion(arr,length - 1);
}
public static void main(String[] args) {
int[] arr = new int[]{23,4,1,-5,-9,45,2,45,78,-14,0};
bubbleSort2OptimizedVersion(arr,arr.length);
for(int n:arr)
System.out.print(n+" ");
}
}
2.选择排序
2.1.1 普通版本
public class SelectSort1 {
public static void selectSort1(int[] arr){
if(arr.length < 2)
return;
for(int i = 0;i < arr.length - 1;i++){
int index = i;
for(int j = i + 1;j < arr.length;j++){
if(arr[j] < arr[index]){
index = j;
}
}
if(index != i){
int temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
}
}
public static void main(String[] args) {
int[] arr = new int[]{23,4,1,-5,-9,45,2,45,78,-14,0};
selectSort1(arr);
for(int n:arr){
System.out.print(n+" ");
}
}
}
2.1.2 优化版本(一次找出最大和最小值)
public class SelectSort1OptimizedVersion {
public static void main(String[] args) {
int[] arr = new int[]{23,4,1,-5,-9,45,2,45,78,-14,0};
bubbleSort1OptimizedVersion(arr);
for(int n:arr){
System.out.print(n+" ");
}
}
public static void bubbleSort1OptimizedVersion(int[] arr){
if(arr.length < 2)
return;
int left = 0,right = arr.length - 1;
while(left < right){
int minpos = left,maxpos = left;
for(int i = left;i <= right;i++){
if(arr[i] < arr[minpos])
minpos = i;
if(arr[i] > arr[maxpos])
maxpos = i;
}
if(minpos != left){
int temp = arr[left];
arr[left] = arr[minpos];
arr[minpos] = temp;
}
if(maxpos == left){
maxpos = minpos;
}
if(maxpos != right){
int temp = arr[right];
arr[right] = arr[maxpos];
arr[maxpos] = temp;
}
left++;
right--;
}
}
}
2.2.1 递归版本
public class SelectSort2 {
public static void selectSort2(int[] arr,int start){
if(start >= arr.length - 1)
return;
int index = start;
for(int i = start + 1;i < arr.length;i++){
if(arr[i] < arr[index]){
index = i;
}
}
if(index != start){
int temp = arr[start];
arr[start] = arr[index];
arr[index] = temp;
}
selectSort2(arr,start + 1);
}
public static void main(String[] args) {
int[] arr = new int[]{23,4,1,-5,-9,45,2,45,78,-14,0};
selectSort2(arr,0);
for(int n:arr){
System.out.print(n+" ");
}
}
}
2.2.2 递归优化版本
public class SelectSort2OptimizedVersion {
public static void main(String[] args) {
int[] arr = new int[]{23,4,1,-5,-9,45,2,45,78,-14,0};
SelectSort2OptimizedVersion(arr,0,arr.length-1);
for(int n:arr){
System.out.print(n+" ");
}
}
public static void SelectSort2OptimizedVersion(int[] arr,int left,int right) {
if (left > right)
return;
int minpos = left, maxpos = left;
for (int i = left; i <= right; i++) {
if (arr[i] < arr[minpos]) {
minpos = i;
}
if (arr[i] > arr[maxpos]) {
maxpos = i;
}
}
if (minpos != left) {
int temp = arr[left];
arr[left] = arr[minpos];
arr[minpos] = temp;
}
if (maxpos == left) {
maxpos = minpos;
}
if (maxpos != right) {
int temp = arr[right];
arr[right] = arr[maxpos];
arr[maxpos] = temp;
}
SelectSort2OptimizedVersion(arr, left + 1, right - 1);
}
}
3 插入排序
public class InsertSort {
public static void main(String[] args) {
int[] arr = new int[]{23,4,1,-5,-9,45,2,45,78,-14,0};
insertSort(arr);
for(int n:arr){
System.out.print(n+" ");
}
}
public static void insertSort(int[] arr){
for(int i = 0;i < arr.length - 1;i++){
int temp = arr[i+1];
int j = i;
for(;j >= 0;j--){
if(temp < arr[j]){
arr[j+1] = arr[j];
}
else{
break;
}
}
arr[j+1] = temp;
}
}
}
4 希尔排序
public class ShellSort {
public static void groupSort(int[] arr,int step,int start){
for(int i = start + step;i < arr.length;i = i + step){
int temp = arr[i];
int j = i - step;
for(;j >=0;j = j - step){
if(arr[j] > temp){
arr[j+step] = arr[j];
}
else{
break;
}
}
arr[j+step] = temp;
}
}
public static void shellSort(int[] arr){
int step = arr.length/2;
for(;step > 0;step = step/2){
for(int i = 0;i < step;i++){
groupSort(arr,step,i);
}
}
}
public static void main(String[] args) {
int[] arr = new int[]{44,3,38,5,47,15,36,26,27,2,46,4,19,50,48};
shellSort(arr);
for(int n:arr){
System.out.print(n+" ");
}
}
}
5 快速排序
5.1 快速排序—前后指针法
public class QuickSort_FrontRearPointer {
public static void main(String[] args) {
int[] arr = new int[]{21,32,35,7,-90,-46,12,56,86,442,2,31,122,-2,0,0,0};
quickSort(arr,0,arr.length - 1);
for(int n:arr){
System.out.print(n+" ");
}
}
public static void quickSort(int[] arr,int start,int end){
if(start >= end)
return;
int pre = start,cur = pre + 1;
int key = start;
while(cur <= end){
if(arr[cur] < arr[key]){
pre++;
int temp = arr[cur];
arr[cur] = arr[pre];
arr[pre] = temp;
}
cur++;
}
int temp = arr[pre];
arr[pre] = arr[key];
arr[key] = temp;
quickSort(arr,start,pre - 1);
quickSort(arr,pre + 1,end);
}
}
5.2 快速排序—左右指针法
public class QuickSort_LeftRightPointer {
public static void main(String[] args) {
int[] arr = new int[]{-99,23,4,1,-5,-9,45,2,45,78,-14,0};
quickSort(arr,0,arr.length - 1);
for(int n:arr){
System.out.print(n+" ");
}
}
public static void quickSort(int[] arr,int start,int end){
if(start >= end)
return;
int left = start,right = end;
int key = left;
while(left < right){
while(arr[right] >= arr[key] && right > left){
right--;
}
while(arr[left] <= arr[key] && left < right){
left++;
}
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
int temp = arr[key];
arr[key] = arr[left];
arr[left] = temp;
key = left;
quickSort(arr,start,key - 1);
quickSort(arr,key + 1,end);
}
}