八种排序算法
package basicalgorithm.sort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SortUtil {
public static void bucketSort(int[] nums){
if (isEmpty(nums)){
return ;
}
int min = Integer.MAX_VALUE ;
int max = Integer.MIN_VALUE ;
for (int i=0 ; i<nums.length ; i++){
min = min > nums[i] ? nums[i] : min ;
max = max < nums[i] ? nums[i] : max ;
}
int bucketNum = (max - min) / nums.length + 1 ;
List<List<Integer>> bucket = new ArrayList<>(bucketNum);
for (int i=0 ; i<bucketNum ; i++){
bucket.add(new ArrayList<>()) ;
}
for (int i=0 ; i<nums.length ; i++){
int num = (nums[i] - min) / nums.length ;
bucket.get(num).add(nums[i]) ;
}
int index = 0 ;
List<Integer> pre ;
for (int i=0 ; i<bucket.size() ; i++){
pre = bucket.get(i) ;
Collections.sort(pre);
for (int j=0 ; j<pre.size() ; j++){
nums[index++] = pre.get(j) ;
}
}
}
public static void countSort(int[] nums){
if (isEmpty(nums)){
return ;
}
int min = Integer.MAX_VALUE ;
int max = Integer.MIN_VALUE ;
for (int i=0 ; i<nums.length ; i++){
min = min > nums[i] ? nums[i] : min ;
max = max < nums[i] ? nums[i] : max ;
}
int index ;
int[] bucket = new int[max - min + 1] ;
for (int i=0 ; i<nums.length ; i++){
bucket[nums[i] - min] ++ ;
}
index = 0 ;
for (int i=0 ; i<bucket.length ; i++){
if (bucket[i] != 0){
while(bucket[i]-- > 0){
nums[index++] = i + min ;
}
}
}
}
public static void fastSort(int[] nums){
if (isEmpty(nums)){
return ;
}
fastSort(nums , 0 , nums.length-1);
}
public static void fastSort(int[] array , int f , int l){
int start = f ;
int end = l ;
int key = array[f];
while(end>start){
while(array[end] > key && end > start){
end -- ;
}
if (array[end] <= key){
int temp = array[start];
array[start] = array[end];
array[end] = temp ;
}
while(array[start] <= key && end > start){
start ++ ;
}
if (array[start] >= key){
int temp = array[start];
array[start] = array[end];
array[end] = temp ;
}
}
if (start>f){
fastSort(array,f,start-1);
}
if (end<l){
fastSort(array,end+1,l);
}
}
public static void mergeSort(int[] nums){
if (isEmpty(nums)){
return ;
}
mergeSort(nums , 0 , nums.length-1) ;
}
private static void mergeSort(int[] nums , int left , int right){
if (left >= right){
return ;
}
int middle = left + ((right - left) >> 1) ;
mergeSort(nums , left , middle);
mergeSort(nums , middle+1 , right);
merge(nums , left , middle , right);
}
private static void merge(int[] nums , int left , int middle , int right){
int[] temp = new int[right - left + 1] ;
int index = 0 ;
int start = left ;
int end = middle + 1 ;
while(start <= middle && end <= right){
temp[index++] = nums[start] > nums[end] ? nums[end++] : nums[start++] ;
}
while(start <= middle){
temp[index++] = nums[start++] ;
}
while(end <= right){
temp[index++] = nums[end++] ;
}
for (int i=0 ; i<temp.length ; i++){
nums[left+i] = temp[i] ;
}
}
public static void shellSort(int[] nums){
int len = nums.length ;
for (int gap = (int) Math.floor(len/2); gap > 0 ; gap = (int) Math.floor(gap/2)){
for (int i=gap ; i<len ; i++){
int j = i ;
int current = nums[i] ;
while(j - gap >= 0 && current < nums[j - gap]){
nums[j] = nums[j - gap] ;
j = j - gap ;
}
nums[j] = current ;
}
}
}
public static void selectSort(int[] nums){
if (isEmpty(nums)){
return ;
}
for (int i=0 ; i<nums.length ; i++){
int minIndex = i ;
for (int j=i ; j<nums.length ; j++){
minIndex = nums[minIndex] > nums[j] ? j : minIndex ;
}
int temp = nums[minIndex] ;
nums[minIndex] = nums[i] ;
nums[i] = temp ;
}
}
public static void bubbleSort(int[] nums){
if (isEmpty(nums)){
return ;
}
int temp ;
for (int i=0 ; i<nums.length ; i++){
for (int j=0 ; j < nums.length-i-1 ; j++ ){
temp = nums[j] ;
nums[j] = nums[j+1] ;
nums[j+1] = temp ;
}
}
}
public static void insertSort(int[] nums){
if (isEmpty(nums)){
return ;
}
for (int i=0 ; i<nums.length ; i++){
int preIndex = i-1 ;
int current = nums[i] ;
while(preIndex >= 0 && nums[preIndex] > current){
nums[preIndex+1] = nums[preIndex] ;
preIndex -- ;
}
nums[preIndex+1] = current ;
}
}
public static boolean isEmpty(int[] nums){
return nums == null || nums.length == 0 ;
}
public static void main(String[] args) {
int[] nums = new int[]{4,3,2,1,1} ;
bubbleSort(nums) ;
System.out.println("冒泡排序:");
for (int num : nums) {
System.out.print(num+" ");
}
System.out.println();
System.out.println("=====================");
nums = new int[]{4, 3, 2, 1, 1};
bucketSort(nums) ;
System.out.println("桶排序:");
for (int num : nums) {
System.out.print(num+" ");
}
System.out.println();
System.out.println("=====================");
nums = new int[]{4, 3, 2, 1, 1};
countSort(nums);
System.out.println("计数排序:");
for (int num : nums) {
System.out.print(num+" ");
}
System.out.println();
System.out.println("=====================");
nums = new int[]{4, 3, 2, 1, 1};
fastSort(nums);
System.out.println("快速排序:");
for (int num : nums) {
System.out.print(num+" ");
}
System.out.println();
System.out.println("=====================");
nums = new int[]{4, 3, 2, 1, 1};
insertSort(nums);
System.out.println("插入排序:");
for (int num : nums) {
System.out.print(num+" ");
}
System.out.println();
System.out.println("=====================");
nums = new int[]{4, 3, 2, 1, 1};
mergeSort(nums);
System.out.println("归并排序:");
for (int num : nums) {
System.out.print(num+" ");
}
System.out.println();
System.out.println("=====================");
nums = new int[]{4, 3, 2, 1, 1};
selectSort(nums);
System.out.println("选择排序:");
for (int num : nums) {
System.out.print(num+" ");
}
System.out.println();
System.out.println("=====================");
nums = new int[]{4, 3, 2, 1, 1};
shellSort(nums);
System.out.println("希尔排序:");
for (int num : nums) {
System.out.print(num+" ");
}
System.out.println();
System.out.println("=====================");
}
}
冒泡排序:
1 1 2 3 4
=====================
桶排序:
1 1 2 3 4
=====================
计数排序:
1 1 2 3 4
=====================
快速排序:
1 1 2 3 4
=====================
插入排序:
1 1 2 3 4
=====================
归并排序:
1 1 2 3 4
=====================
选择排序:
1 1 2 3 4
=====================
希尔排序:
1 1 2 3 4
=====================