参考1:参考
参考2:十大经典排序算法动画与解析,看我就够了!(配代码完全版)
选择排序
public class kk {
public static void main(String[] args) {
int[] arr = new int[]{8, 2, 5, 9, 7};
int length = arr.length;
for (int i = 0; i < length; i++) {
int min = i;
for (int j = i+1;j < length; j++) {
if (arr[j] < arr[min])
{
min = j;
}
}
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
for (int nn:arr) {
System.out.println(nn);
}
}
}
冒泡排序
public class kk {
public static void main(String[] args) {
int[] arr = new int[]{8, 2, 5, 9, 7};
int length = arr.length;
for (int i = 0; i < length; i++) {
int flag = 0;
for (int j = 0; j < length-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
flag = 1;
}
}
if(flag == 0)
{
break;
}
}
}
}
希尔排序
缩减增量排序
public class kk {
public static void main(String[] args) {
int[] arr = new int[]{8, 2, 5, 9, 7};
int length = arr.length;
int j = 0;
int k =0;
int gap = 1;
if(gap < length)
{
gap = gap*3+1;
}
while(gap > 0)
{
for (int i = gap; i < length; i++) {
int label = arr[i];
for (j = i-gap; j >=0; j=j-gap) {
if(arr[j] > label)
{
arr[j+gap] = arr[j];
}
else
break;
}
arr[j+gap] = label;
}
gap = gap/3;
}
for (int nn:arr) {
System.out.println(nn);
}
}
}
插入排序
将未排序数字插入到已排序数组中,第一个数作为已排好数
public class kk {
public static void main(String[] args) {
int[] arr = new int[]{8, 2, 5, 9, 7};
int k = 0;
//将j作为全局变量
int j = 0;
for (int i = 1; i < arr.length ; i++) {
int label = arr[i];
for (j = i-1; j >= 0 ; j--) {
if(arr[j] > label)
{
arr[j+1] = arr[j];
k = j;
}else
{
break;
}
}
arr[j+1] = label;
}
for (int nn:arr) {
System.out.println(nn);
}
}
}
归并排序:
先将数组二分,直到分到最小为单个数组
再从下往上合并
需要利用额外的数组来辅助
public class kk {
public static void main(String[] args) {
int[] arr = new int[]{8, 2, 5, 9, 7};
int[] arr_copy = new int[arr.length];
//int length = arr.length;
sort(arr, arr_copy, 0, arr.length - 1);
for (int nn : arr) {
System.out.println(nn);
}
}
public static void sort(int[] arr, int[] arr_copy, int start, int end) {
if (start >= end) {
return;
}
int mid = start + (end - start) / 2;
sort(arr, arr_copy, start, mid);
sort(arr, arr_copy, mid + 1, end);
merge(arr, arr_copy, start, mid, end);
}
public static void merge(int[] arr, int[] arr_copy, int start, int mid, int end) {
for (int i = start; i <= end; i++) {
arr_copy[i] = arr[i];
}
int left = start;
int right = mid + 1;
for (int k = start; k <= end; k++) {
if (left > mid) {
arr[k] = arr_copy[right++];
} else if (right > end) {
arr[k] = arr_copy[left++];
} else if (arr_copy[right] < arr_copy[left]) {
arr[k] = arr_copy[right++];
} else {
arr[k] = arr_copy[left++];
}
}
}
}
快速排序
随机选取一个数,小的放在这个数左边,大的放这个数右边,循环,直到最好的元素为单个元素
用双指针,从左边和右边遍历,左边找到第一个大于label的数,右边找到第一个小于label的数,交换,最后将label与右指针的数交换
修正
快速排序参考:快速排序Java代码实现
package com;
class kuaipai_xx {
public static void main(String[] args) {
// int a[]= {3,4,11,2,0,9,8,5,7};
int a[]= {7, 2, 4, 9, 8, 6};
Quicksort(a,0,a.length-1);
for (int i : a) {
System.out.print(i+" ");
}
}
private static void Quicksort(int[] a, int left, int right) {
if(left>right)
return;
int i=left;
int j=right;
int base=a[left];
while(i!=j) {
while(a[j]>=base&&i<j)
j--;
while(a[i]<=base&&i<j)
i++;
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
a[left]=a[i];
a[i]=base;
Quicksort(a,left,i-1);
Quicksort(a,i+1,right);
}
}
public class kk {
public static void main(String[] args) {
int[] arr = new int[]{8, 2, 5, 9, 7};
//int length = arr.length;
sort(arr,0,arr.length-1);
for (int nn:arr
) {
System.out.println(nn);
}
}
public static void sort(int[] arr,int start, int end)
{
if(start >= end)
{
return;
}
int partition = mergesort(arr,start, end);
sort(arr,start,partition-1);
sort(arr,partition+1,end);
}
public static int mergesort(int[] arr, int start, int end) {
int left = start;
int right = end;
int label = start;
while (true) {
while (arr[left] <= arr[label]){
left++;
if (left == right) {
break;
}
}
while (arr[right] >= arr[label]){
right--;
if (right == left) {
break;
}
}
if (left >= right) {
break;
}
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
int temp = arr[label];
arr[label] = arr[right];
arr[right] = temp;
return right;
}
}
堆排序
public class kk {
public static void main(String[] args) {
int[] arr = new int[]{8, 2, 5, 9, 7};
int length = arr.length;
//构建堆
buildHeap(arr, 0, length);
for(int i = length-1; i>=0;i--)
{
//将堆元素与末尾元素调换
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
//数组长度-1,隐藏堆尾元素
length--;
sink(arr,0,length);
}
//int n = nums.length;
for (int nn : arr) {
System.out.println(nn);
}
}
public static void buildHeap(int[] arr, int start, int length)
{
for(int j = length/2; j>=0; j--)
{
sink(arr,j,length);
}
}
public static void sink(int[] arr, int index, int length)
{
int left = index*2+1;
int right = index*2+2;
int present = index;
//下沉左边
if(left < length && arr[left] > arr[present])
{
present = left;
}
//下沉右边
if(right < length && arr[right] > arr[present])
{
present = left;
}
//如果下标不相同,证明调换过了
if(present != index)
{
int temp2 = arr[index];
arr[index] = arr[present];
arr[present] = temp2;
sink(arr,present,length);
}
}
}