# java中常见的几种排序方式

1冒泡排序

package paixu;

public class maopao {

public static void main(String[] args) {

int[] numbers  = {10,12,2,3,5,78,6,21,99,88};
int len = numbers.length;
for(int j =0; j<len; j++){
for(int i = 0; i<len-1-j;i++){
int temp = 0;
//如果前面的数比后面的数大，则交换位置
if (numbers[i]>numbers[i+1]){
temp = numbers[i+1];
numbers[i+1] = numbers[i];
numbers[i] = temp;
}
}
}
for (int i = 0; i<len; i++){
System.out.print(numbers[i]);
System.out.print(' ');
}

}

}

2插入排序

package paixu;

public class InsertionSorting {

public static void main(String[] args) {

int[] numbers  = {10,12,2,3,5,78,6,21,99,88};
int len = numbers.length;

for (int i = 1; i < len; i++){
for(int j = 0; j < i; j++){
int temp = 0;
if (numbers[i] < numbers[j]){
temp = numbers[i];
//依次把numbers[m]的值赋给number[m+1]
for(int m = i-1; m > j-1; m--){
numbers[m+1] = numbers[m];
}
//交换number[i]和numbers[j]的值
numbers[j] = temp;
}
}
}
for(int i = 0; i <len; i++){
System.out.print(numbers[i]);
System.out.print(' ');
}

}

}

3堆排序

package paixu;

import java.util.Arrays;

public class HeapSorting {
public static void main(String[] args) {

int[] numbers  = {10,12,2,3,5,78,6,21,99,88};
int len = numbers.length;
System.out.println(len);
//每个节点进行排序
for (int i = 0; i <len-1; i++){
buildMaxHeap(numbers, len-1-i);
swap(numbers, 0, len-1-i);
System.out.println(Arrays.toString(numbers));

}
}
public static void buildMaxHeap(int[] data, int lastIndex){
for(int i = (lastIndex-1)/2; i>= 0; i--){
//保存当前正在判断的节点

int k = i;
//当前子节点存在
while(2*k+1 <= lastIndex){
//biggerIndex用来记录最大节点的索引
int biggerIndex = 2*k + 1;
//如果biggerIndex小于lastIndex，即biggerIndex+1代表的k节点的右子节点存在
if (biggerIndex < lastIndex){
//如果右子节点的值大于左子节点的值，那么将biggerIndex记录为右子节点的所索引
if (data[biggerIndex] < data[biggerIndex+1]){
biggerIndex++;
}
}
if(data[k]<data[biggerIndex]){
//如果子节点中的最大值大于父节点的值，那么交换它们。
swap(data, k, biggerIndex);
}
break;
}
System.out.println(Arrays.toString(data));
}
}
public static void swap(int[] data, int i, int j) {

int temp = data[i];
data[i] = data[j];
data[j] = temp;
}

}

4快速排序

package paixu;

import java.util.Arrays;

public class QickSort {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] numbers = {10,12,2,3,5,78,6,21,99,88};
int len = numbers.length;
//如果数组大于2的时候才开始排序
if(len>1){
quickSort(numbers,0,len-1);
}
System.out.println(Arrays.toString(numbers));
}
public static void  quickSort(int[] list, int low, int high) {
if(low < high){
int middle = getMiddle(list, low, high);
quickSort(list, low, middle - 1);
quickSort(list, middle + 1, high);
}
}
public static int  getMiddle(int[] list, int low, int high) {
//将首元素作为参考元素
int temp = list[low];
while(low < high){
while(low < high && temp <= list[high]){
high--;
}
//当退出上一个while的时候说明temp>=list[high]
list[low] = list[high];
while(low < high && list[low] <= temp){
low++;
}
//当退出上一个while循环的时候，说明list[low]>temp
list[high] = list[low];
}
list[low] = temp;
return low;
}
}

5归并排序

package paixu;

import java.util.Arrays;

public class mergingSort {
int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
public  mergingSort(){
sort(a,0,a.length-1);
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
public void sort(int[] data, int left, int right) {
// TODO Auto-generated method stub
if(left<right){
//找出中间索引
int center=(left+right)/2;
//对左边数组进行递归
sort(data,left,center);
//对右边数组进行递归
sort(data,center+1,right);
//合并
merge(data,left,center,right);

}
}
public void merge(int[] data, int left, int center, int right) {
// TODO Auto-generated method stub
int [] tmpArr=new int[data.length];
int mid=center+1;
//third记录中间数组的索引
int third=left;
int tmp=left;
while(left<=center&&mid<=right){

//从两个数组中取出最小的放入中间数组
if(data[left]<=data[mid]){
tmpArr[third++]=data[left++];
}else{
tmpArr[third++]=data[mid++];
}
}
//剩余部分依次放入中间数组
while(mid<=right){
tmpArr[third++]=data[mid++];
}
while(left<=center){
tmpArr[third++]=data[left++];
}
//将中间数组中的内容复制回原数组
while(tmp<=right){
data[tmp]=tmpArr[tmp++];
}
System.out.println(Arrays.toString(data));
}

}