package selectsort;/**
* 选择排序,每次选择一个最小值放与数组第一个交换,在选出第二小的与第二个元素交换,直至排完为止,复杂度O(n^2 / 2)
*/
public class SelectSort {
public staticvoidmain(String[] args){int[] nums = new int[]{10,6,8,5,3,1,2,4,7,9};
SelectSort a = new SelectSort();
a.selectsort(nums);for(int i =0; i < nums.length; i++){
System.out.println(nums[i]);}}
private voidselectsort(int[] nums){for(int i =0; i < nums.length -1; i++){int min = i;for(int j = i +1; j < nums.length; j++){if(nums[j]< nums[min]){
min = j;}}swap(nums, i, min);}}
private voidswap(int[] nums,int i,int j){int tmp = nums[i];
nums[i]= nums[j];
nums[j]= tmp;}};
二、冒泡排序
package Bubblesort;/**
* 冒泡排序: 每次比较相邻的两个元素,将大的元素一直置换到最后一个元素,在比较前n - 1个,不断循环,知道排序完成
* 复杂度: O(n^2)
*/
public class BubbleSort {
public staticvoidmain(String[] args){int[] nums = new int[]{10,6,8,5,3,1,2,4,7,9};
BubbleSort a = new BubbleSort();
a.bubblesort(nums);for(int i =0; i < nums.length; i++){
System.out.println(nums[i]);}}
private voidbubblesort(int[] nums){
boolean issorted = false;for(int i = nums.length -1; i >0&&!issorted; i--){
issorted = true;for(int j =0; j < i; j++){if(nums[j]> nums[j +1]){
issorted = false;swap(nums, j, j +1);}}}}
private voidswap(int[] nums,int i,int j){int tmp = nums[i];
nums[i]= nums[j];
nums[j]= tmp;}}
三、插入排序
package insertsort;/**
* 插入排序: 每次排序前i个数,将第i+1 个数插入到前i个数中,确保其顺序不变,冒泡排序是每次把最大值放大最后。
* 复杂度: O(n^2)
*/
public class InsertSort {
public staticvoidmain(String[] args){int[] nums = new int[]{10,6,8,5,3,1,2,4,7,9};
InsertSort a = new InsertSort();
a.insertsort(nums);for(int i : nums){
System.out.println(i);}}
private voidinsertsort(int[] nums){for(int i =1; i < nums.length; i++){for(int j = i; j >0; j--){if(nums[j -1]> nums[j]){swap(nums, j, j -1);}}}}
private voidswap(int[] nums,int i,int j){int tmp = nums[i];
nums[i]= nums[j];
nums[j]= tmp;}}
四、希尔排序
package shellsort;/**
* 希尔排序: 由于插入排序接近正序时交换次数少很多,并且当数量很大时交换次数太多,希尔排序先通过交换不相邻的两个元素,次数少并且将排序趋近
* 正序,最后稍作调整就可以实现正序,可以降低时间复杂度。
*/
public class ShellSort {
public staticvoidmain(String[] args){int[] nums = new int[]{10,6,8,5,3,1,2,4,7,9};
ShellSort a = new ShellSort();
a.shellsort(nums);for(int i : nums){
System.out.println(i);}}
private voidshellsort(int[] nums){int h =1;while(h < nums.length /3){
h = h *3+1;//h = 1 ,4, 13, 40 ...}while(h >=1){for(int i = h; i < nums.length; i++){for(int j = i; j >= h; j -= h){if(nums[j - h]> nums[j]){swap(nums, j, j - h);}}}
h /=3;}}
private voidswap(int[] nums,int i,int j){int tmp = nums[i];
nums[i]= nums[j];
nums[j]= tmp;}}
五、归并排序
package mergesort;/**
* 归并排序: 不断细分,分成两部分,再按顺序合并,O(nlogn)
*/
public class MergeSort {
public staticvoidmain(String[] args){int[] nums = new int[]{10,6,8,5,3,1,2,4,7,9};
MergeSort a = new MergeSort();
a.merge(nums,0, nums.length -1);for(int i : nums){
System.out.println(i);}}
private voidmerge(int[] nums,int left,int right){if(left >= right)return;int mid = left +(right - left)/2;merge(nums, left, mid);merge(nums,mid +1, right);mergesort(nums, left, right, mid);}
private voidmergesort(int[] nums,int left,int right,int mid){int i = left, j = mid +1;int[] result = new int[right - left +1];int p =0;while(i <= mid && j <= right){if(nums[i]< nums[j]) result[p++]= nums[i++];else result[p++]= nums[j++];}while(i <= mid) result[p++]= nums[i++];while(j <= right) result[p++]= nums[j++];
System.arraycopy(result,0, nums, left, right - left +1);}}
六、快速排序
package quicksort;/**
快速排序: 选一个基准值,大的放后面,小的放前面,不断递归。O(nlogn)
**/
public class QuickSort {
public staticvoidmain(String[] args){int[] nums = new int[]{2,4,5,6,9,1,4,6,3,5,1,16,7,9};int length = nums.length;quicksort(nums,0, length -1);for(int i =0; i < length; i++){
System.out.printf("%d\n", nums[i]);}}
private staticvoidquicksort(int[] nums,int left,int right){if(left >= right)return;int i = left, j = right, base = nums[left];while(i < j){while(nums[j]>= base && i < j) j--;while(nums[i]<= base && i < j) i++;swap(nums, i, j);}
nums[left]= nums[j];
nums[j]= base;quicksort(nums, left, j -1);quicksort(nums, j +1, right);}
private staticvoidswap(int[] nums,int i,int j){int tmp = nums[i];
nums[i]= nums[j];
nums[j]= tmp;}}
七、堆排序
package heapsort;/**
* 堆排序: 先生成一个大根堆,然后将最大元素与最后一个元素交换,在隔开最后一个元素并继续生成大根堆,生成初始堆时O(n),后面不断重建堆O(logn),总复杂度O(nlogn)
*/
public class HeapSort {
public staticvoidmain(String[] args){int[] nums = new int[]{10,6,8,5,3,1,2,4,7,9};
HeapSort a = new HeapSort();
a.heapsort(nums);for(int i : nums){
System.out.println(i);}}
private voidheapsort(int[] nums){for(int i =(nums.length -1)/2; i >=0; i--){headadjust(nums, i, nums.length);}for(int i = nums.length -1; i >0; i--){swap(nums, i,0);headadjust(nums,0, i -1);}}
private voidheadadjust(int[] nums,int i,int length){int parent = i;int tmp = nums[parent];int lchild = i *2+1;while(lchild < length){int rchild = lchild +1;if(rchild < length && nums[rchild]> nums[lchild]){
lchild = rchild;}if(nums[parent]> nums[lchild])break;
nums[parent]= nums[lchild];
parent = lchild;
lchild = lchild *2+1;}
nums[parent]= tmp;}
private voidswap(int[] nums,int i,int j){int tmp = nums[i];
nums[i]= nums[j];
nums[j]= tmp;}}
八、桶排序
package bucketsort;
import java.util.ArrayList;/**
* 桶排序 :根据要排序的N个数据,生成M个桶,把对应数据放入桶中进行排序,在将桶内元素按顺序连接起来,空间换时间,时间复杂度O(n)
*/
public class BucketSort {
public staticvoidmain(String[] args){int[] nums = new int[]{10,6,8,5,3,1,2,4,7,9};
BucketSort a = new BucketSort();
a.bucketsort(nums);for(int i : nums){
System.out.println(i);}}
private voidbucketsort(int[] nums){int num = nums.length /2;
ArrayList<ArrayList<Integer>> buckets = new ArrayList<>(num);for(int i =0; i < num; i++){
buckets.add(new ArrayList<Integer>());}for(int i : nums){int index =getindex(i);insertsort(buckets.get(index), i);}int index =0;for(ArrayList<Integer> bucket : buckets){for(Integer data : bucket){
nums[index++]= data;}}}
private intgetindex(int data){return(data -1)/2;}
private voidinsertsort(ArrayList<Integer> bucket,int data){int l = bucket.size();
boolean issort = true;for(int i =0; i < l; i++){if(bucket.get(i)>= data){
issort = false;
bucket.add(i, data);break;}}if(issort) bucket.add(data);}}