7种基本排序算法--java实现

7种基本排序算法有:

直接插入排序、希尔排序;直接选择排序、堆排序;冒泡排序、快速排序;归并排序。实现如下:

import java.util.Arrays;

public class SortAlogrithm {
	
	public static void main(String[] args) {
		int[] array1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
		int[] array2 = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
		int[] array3 = {1, 3, 4, 2, 0, 11, 1, 15, 7, 6, 5, 13, 18, 20, 12};
		int[] array4 = {1, 3, 4, 2, 30, 11, 1, 15, 6, 5, 13, 18, 20, 12};
		mergeSort(array1, 0, array1.length-1);
		mergeSort(array2, 0, array2.length-1);
		mergeSort(array3, 0, array3.length-1);
		mergeSort(array4, 0, array4.length-1);
//		quickSort(array1, 0, array1.length-1);
//		quickSort(array2, 0, array2.length-1);
//		quickSort(array3, 0, array3.length-1);
//		quickSort(array4, 0, array4.length-1);
//		buddleSort(array1);
//		buddleSort(array2);
//		buddleSort(array3);
//		buddleSort(array4);
		print(array1);
		print(array2);
		print(array3);
		print(array4);
	}
	
	public static void print(int[] inputs){
		System.out.println(Arrays.toString(inputs));
	}
	//直接插入排序
	public static void insertSort(int[] inputs){
		int size = inputs.length;
		for(int i=1; i=0&&(inputs[j]>temp); j--){
				inputs[j+1] = inputs[j];
			}
			inputs[j+1] = temp;
		}
	}
	//希尔排序
	public static void shellSort(int[] inputs){
		int size = inputs.length;
		int d=1;
		while(d < size){
			d = 3*d+1;
		}
		d = (d-1)/3;
		while(d > 0){
			for(int i=d; i=0&&(inputs[j]>temp); j-=d){
					inputs[j+d] = inputs[j];
				}
				inputs[j+d] = temp;
			}
			d = (d-1)/3;
		}
		
	}
	//直接选择排序
	public static void selectSort(int[] inputs){
		int size = inputs.length;
		for(int i=0; i inputs[j]){
					min = inputs[j];
					minIndex = j;
				}
			}
			if(minIndex != i){
				inputs[i] = inputs[minIndex];
				inputs[minIndex] = temp;
			}
		}
	}
	//堆排序
	public static void heapSort(int[] inputs){
		int size = inputs.length;
		for(int i=size; i>0; i--){
			heapSortBuildHeap(inputs, i);
			heapSortSwap(inputs, i-1);
		}
		
	}
	
	private static void heapSortBuildHeap(int[] inputs, int end){
		int size = end;
		int start = size/2 - 1;
		for(int i=start; i>=0; i--){
			if(2*i+2 inputs[2*i+2]){
					if(inputs[2*i+1] > inputs[i]){
						int temp = inputs[2*i+1];
						inputs[2*i+1] = inputs[i];
						inputs[i] = temp;
					}
				}else{
					if(inputs[2*i+2] > inputs[i]){
						int temp = inputs[2*i+2];
						inputs[2*i+2] = inputs[i];
						inputs[i] = temp;
					}
				}
			}else{
				if(inputs[2*i+1] > inputs[i]){
					int temp = inputs[2*i+1];
					inputs[2*i+1] = inputs[i];
					inputs[i] = temp;
				}
			}
		}
	}
	private static void heapSortSwap(int[] inputs, int end){
		int temp = inputs[end];
		inputs[end] = inputs[0];
		inputs[0] = temp;
	}
	//冒泡排序
	public static void buddleSort(int[] inputs){
		int size = inputs.length;
		for(int i=0; i= right)
			return;
		int base = inputs[left];
		int baseIndex = left;
		int end = right;
		while(left < right){
			while(right>left && inputs[right]>=base){
				right--;
			}
			inputs[left] = inputs[right];
			while(left= high){
			return;
		}
		int mid = (low+high)/2;
		mergeSort(inputs, low, mid);
		mergeSort(inputs, mid+1, high);
		merge(inputs, low, mid, high);
	}
	
	private static void merge(int[] inputs, int low, int mid, int high){
		int[] temp = new int[high-low+1];
		int i = low;
		int j = mid + 1;
		int k = 0;
		while(i<=mid && j<=high){
			if(inputs[i] <= inputs[j]){
				temp[k] = inputs[i];
				i++;
			}else{
				temp[k] = inputs[j];
				j++;
			}
			k++;
		}
		while(i <= mid){
			temp[k] = inputs[i];
			k++;
			i++;
		}
		while(j <= high){
			temp[k] = inputs[j];
			k++;
			j++;
		}
		for(int m=0; m

你可能感兴趣的:(算法)