快排
import java.util.Arrays; public class QuickSort { public static void main(String[] args) { int array []= {34,65,12,45,6,79,9}; quickSort(array); for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } System.out.println(Arrays.toString(array)); } public static void quickSort(int[] array){ if(array != null){ quickSort(array, 0, array.length-1); } } private static void quickSort(int[] array,int beg,int end){ if(beg >= end || array == null) return; int p = partition(array, beg, end); quickSort(array, beg, p-1); quickSort(array, p+1, end); } private static int partition(int[] array, int beg, int end) { int pivot = array[beg]; while (beg < end) { while (beg < end && array[end] >= pivot) end--; if (beg < end) array[beg++] = array[end]; while (beg < end && array[beg] <= pivot) beg++; if (beg < end) array[end--] = array[beg]; } array[beg] = pivot; return beg; } }
快排空间复杂度O(log2(n)) 归并O(n)
心情不稳当,快些选一对好友来聊天-----------快速,希尔,简单选择,堆排序---------------不稳当
快些以 nlog2(n)归队------------------------快速,希尔,归并,堆排序----------------------时间负载度 nlog2(n)
直接插容易插成O(n),起泡起的好编程O(n)------------------容易插,起的好,都是初始化序列已经有序