1、冒泡排序(bubbleSort)
import java.util.*;
public class BubbleSort{
public static void swap(int[] a,int i,int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void bubbleSort(int[] a){
int len = a.length;
for(int j=len-1;j>0;j--){
for(int i=0;i<j;i++){
if(a[i]>a[i+1]){
swap(a,i,i+1);
}
}
}
}
public static void main(String[] args) {
int[] a = {3,12,76,23,17,8,54};
bubbleSort(a);
System.out.println(Arrays.toString(a));
}
}
2、堆排序(heapSort)
import java.util.*;
public class HeapSort {
public static void swap(int[] a,int i,int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void heapSort(int[] a){
buildMaxHeap(a); // 构造大顶堆
for(int i=a.length-1;i>0;i--){
swap(a,i,0); // 将顶元素和当前最后一个元素交换位置
adjustDown(a,0,i); // 从顶元素开始向下进行调整
}
}
public static void buildMaxHeap(int[] a){
int len = a.length;
// 构造大顶堆时,第一个要比较的元素下标正好是(数组长度-1)/2
for(int i=(len-1)/2;i>=0;i--){
adjustDown(a,i,len);
}
}
public static void adjustDown(int[] a,int i,int len){
int temp = a[i];
// 下标为i的元素的左右孩子节点分别是2*i+1和2*i+2
for(int j=2*i+1;j<len;j=2*j+1){
if(a[j+1]>a[j]&&j+1<len)
j++;
if(a[j]<=temp){
break;
}else{
a[i] = a[j];
i = j;
}
}
a[i] = temp;
}
public static void main(String[] args) {
int[] a = {3,12,76,23,17,8,54};
heapSort(a);
System.out.println(Arrays.toString(a));
}
}
3、插入排序(InsertSort)
import java.util.*;
public class InsertSort {
public static void insertSort(int[] a){
int j,temp,len=a.length;
for(int i=1;i<len;i++){
if(a[i-1]>a[i]){
temp = a[i];
for(j=i;j>=1&&a[j-1]>temp;j-=1){
a[j] = a[j-1];
}
a[j] = temp;
}
}
}
public static void main(String[] args) {
int[] a = {3,12,76,23,17,8,54};
insertSort(a);
System.out.println(Arrays.toString(a));
}
}
4、归并排序(MergeSort)
import java.util.*;
public class MergeSort {
public static void merge(int[] a,int low,int mid,int high){
int[] temp = new int[high-low+1];
int k = 0;
int l = low;
int r = mid+1;
while(l<=mid&&r<=high){
if(a[l]<=a[r]){
temp[k++] = a[l++];
}else{
temp[k++] = a[r++];
}
}
while(l<=mid){
temp[k++] = a[l++];
}
while(r<=high){
temp[k++] = a[r++];
}
for(int i=0;i<temp.length;i++){
a[i+low] = temp[i];
}
}
public static void mergeSort(int[] a,int low,int high){
if(low<high){
int mid = (low+high)/2;
mergeSort(a,low,mid);
mergeSort(a,mid+1,high);
merge(a,low,mid,high);
}
}
public static void main(String[] args) {
int[] a = {3,12,76,23,17,8,54};
mergeSort(a,0,a.length-1);
System.out.println(Arrays.toString(a));
}
}
5、快速排序(QuickSort)
import java.util.*;
public class QuickSort {
public static void quickSort(int[] a,int low,int high){
if(low<high){
int pivotIndex = partion(a,low,high);
quickSort(a,low,pivotIndex-1);
quickSort(a,pivotIndex+1,high);
}
}
public static int partion(int[] a,int low,int high){
int temp = a[low];
while(low<high){
while(low<high&&a[high]>=temp) high--;
a[low] = a[high];
while(low<high&&a[low]<=temp) low++;
a[high] = a[low];
}
a[low] = temp;
return low;
}
public static void main(String[] args) {
int[] a = {3,12,76,23,17,8,54};
quickSort(a,0,a.length-1);
System.out.println(Arrays.toString(a));
}
}
6、选择排序(SelectSort)
import java.util.*;
public class SelectSort {
public static void swap(int[] a,int i,int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void selectSort(int[] a){
int min,len=a.length;
for(int i=0;i<len;i++){
min = i;
for(int j=i+1;j<len;j++){
if(a[j]<a[min])
min = j;
}
if(min!=i){
swap(a,i,min);
}
}
}
public static void main(String[] args) {
int[] a = {3,12,76,23,17,8,54};
selectSort(a);
System.out.println(Arrays.toString(a));
}
}
7、希尔排序(ShellSort)
注:本质是分段的插入排序
import java.util.*;
public class ShellSort {
public static void shellSort(int[] a){
int j,temp,len=a.length;
for(int dk=len/2;dk>=1;dk/=2){
for(int i=dk;i<len;i++){
if(a[i-dk]>a[i]){
temp = a[i];
for(j=i;j>=dk&&a[j-dk]>temp;j-=dk){
a[j] = a[j-dk];
}
a[j] = temp;
}
}
}
}
public static void main(String[] args) {
int[] a = {3,12,76,23,17,8,54};
shellSort(a);
System.out.println(Arrays.toString(a));
}
}