常见的排序算法有七种,分别是:
插入排序: 直接插入排序,希尔排序
选择排序: 选择排序,堆排序
交换排序: 冒泡排序,快速排序
归并排序: 归并排序
直接插入排序
原理: 每一步将一个待排序的元素,按其排序码的大小,插入到前面已经排好序的一组元素的适当位置上去,直到元素全部插入为止。
时间复杂度:
最坏情况是:O(n^2) 最好情况(有序的情况)是:O(n)
空间复杂度: O(1)
稳定性: 稳定
关于稳定性: 如果一个排序是稳定的排序,那么他就可以变为不稳定的排序。但是如果一个排序本身就是不稳定的排序,你是不可能把他变为稳定的排序的。
如果他是稳定的排序,在比较的过程当中就没有跳跃式的交换。
public static void insertSort(int[] array){
for (int i = 1; i < array.length; i++) {
int tmp = array[i];
int j = i-1;
for (; j >= 0; j--) {
if (array[j] > tmp){
array[j+1] = array[j];
}else {
//array[j+1] = tmp;
break;
}
}
array[j+1] = tmp;
}
}
插入排序,初始数据越接近有序,时间效率越高。
希尔排序
原理: 希尔排序法又称缩小增量法。希尔排序法的基本思想是:先选定一个整数,把待排序文件中所有记录分成个组,所有距离相同的记录分在同一组内,并对每一组内的记录进行排序。然后,取出数据,重复上述分组和排序的工作。当到达=1时,所有记录在统一组内排好序。
时间复杂度: O(n^2)
空间复杂度: O(1)
稳定性: 不稳定
public static void shellSort(int[] array) {
int[] drr = {5,3,1};
for (int i = 0; i < drr.length; i++) {
shell(array,drr[i]);
}
}
public static void shell(int[] array,int gap) {
for (int i = gap; i < array.length; i++) {
int tmp = array[i];
int j = i-gap
for (; j >= 0 ; j = j - gap) {
if (array[j] > tmp){
array[j + gap] = array[j];
}else {
break;
}
}
array[j + gap] = tmp;
}
}
选择排序
原理: 每一次从无序区间选出最大(或最小)的一个元素,存放在无序区间的最后(或最前),直到全部待排序的数据元
素排完 。
时间复杂度: O(n^2)
空间复杂度: O(1)
稳定性: 不稳定
public static void selectSort(int[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = i+1; j < array.length; j++) {
if (array[i] > array[j]){
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
}
}
堆排序
原理: 是选择排序,只是不在使用遍历的方式查找无序区间的最大的数,而是通过堆来选择无序区间的最大的数。
注意:排升序要建大堆,排降序要建小堆。
时间复杂度: O(nlog2(n))
不管有序和无序都是
空间复杂度: O(1)
稳定性: 不稳定
public static void heapSort(int[] array){
createHeap(array);
int end = array.length-1;
while (end > 0){
int tmp = array[0];
array[0] = array[end];
array[end] = tmp;
adjustDown(array,0,end);
end--;
}
}
public static void createHeap(int[] array){
for (int i = (array.length-1-1)/2; i >= 0; i--) {
adjustDown(array,i,array.length);
}
}
public static void adjustDown(int[] array,int root,int len){
int parent = root;
int child = 2*parent+1;
while (child < len){
if (child+1 < len && array[child] < array[child+1]){
child++;
}
if (array[child] > array[parent]){
int tmp = array[child];
array[child] = array[parent];
array[parent] = tmp;
parent = child;
child = 2*parent+1;
}else {
break;
}
}
}
冒泡排序
原理: 在无序区间,通过相邻数的比较,将最大的数冒泡到无序区间的最后,持续这个过程,直到数组整体有序。
时间复杂度: O(n^2)
空间复杂度: O(1)
稳定性: 稳定
public static void bubbleSort(int[] array){
for (int i = 0; i < array.length-1; i++) {
for (int j = 0; j < array.length-1-i; j++) {
if (array[j] > array[j+1]){
int tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
}
}
}
}
//优化后
public static void bubbleSort1(int[] array){
boolean flg = false;
for (int i = 0; i < array.length-1; i++) {
for (int j = 0; j < array.length-1-i; j++) {
if (array[j] > array[j+1]){
int tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
flg = true;
}
}
if (flg == false){
return;
}
}
}
快速排序
原理:
- 从待排序区间选择一个数,作为基准值(pivot);
- Partition: 遍历整个待排序区间,将比基准值小的(可以包含相等的)放到基准值的左边,将比基准值大的
(可以包含相等的)放到基准值的右边; - 采用分治思想,对左右两个小区间按照同样的方式处理,直到小区间的长度 == 1,代表已经有序,或者小区间的长度==0,代表没有数据。
时间复杂度:
O(nlog2(n)) 最坏情况下:1234567/7654321 O(n^2)
空间复杂度:
O(log2(n)) 最坏情况下:O(n)
稳定性: 不稳定
public static int partition(int[] array,int low,int high) {
int tmp = array[low];
while (low < high){
while (low < high && array[high] >= tmp){
high--;
}
array[low] = array[high];
while (low < high && array[low] <= tmp){
low++;
}
array[high] = array[low];
}
array[low] = tmp;
return low;
}
public static void quick(int[] array,int left,int right) {
if(left >= right) {
return;
}
int par = partition(array, left, right);
quick(array,left,par-1);
quick(array,par+1,right);
}
public static void quickSort(int[] array) {
quick(array,0,array.length-1);
}
//快速排序(非递归)
public static void quickSort1(int[] array) {
Stack<Integer> stack = new Stack<>();
int left = 0;
int right = array.length-1;
int par = partition(array,left,right);
if (par > left + 1) {
stack.push(left);
stack.push(par-1);
}
if (par < right - 1) {
stack.push(par+1);
stack.push(right);
}
while (!stack.isEmpty()) {
right = stack.pop();
left = stack.pop();
par = partition(array,left,right);
if (par > left + 1) {
stack.push(left);
stack.push(par-1);
}
if (par < right - 1) {
stack.push(par+1);
stack.push(right);
}
}
}
注意: 快排要快,那么每次划分序列的时候,如果都可以均匀的进行划分,那么效率最好。
归并排序
原理: 归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为二路归并。
时间复杂度: O(nlog2(n))
空间复杂度: O(n)
稳定性: 稳定
外排序:磁盘。
public static void mergeSortInternal(int[] array,int low,int high){
if (low >= high){
return;
}
//分解
int mid = (low+high)/2;
mergeSortInternal(array,low,mid);
mergeSortInternal(array,mid+1,high);
//合并
merge(array,low,mid,high);
}
//归并:将两个有序段归并为一个有序段
public static void merge(int[] array,int low,int mid,int high){
int s1 = low;
int s2 = mid+1;
int len = high-low+1;
int[] ret = new int[len];
int i = 0;//用来表示ret的下标
while (s1 <= mid && s2 <= high){
if (array[s1] <= array[s2]){
ret[i++] = array[s1++];
/* ret[i] = array[s1];
i++;
s1++;*/
}else {
ret[i++] = array[s2++];
}
}
while (s1 <= mid){
ret[i++] = array[s1++];
}
while (s2 <= high){
ret[i++] = array[s2++];
}
for (int j = 0; j < ret.length; j++) {
array[j+low] = ret[j];
}
}
public static void mergeSort1(int[] array){
mergeSortInternal(array,0,array.length-1);
}
//归并排序(非递归)
public static void mergeSort(int[] array) {
for (int gap = 1; gap < array.length; gap *= 2) {
mergeNor(array,gap);
}
}
public static void mergeNor(int[] array,int gap) {
int[] ret = new int[array.length];
int k = 0;//ret的下标
int s1 = 0;
int e1 = s1+gap-1;
int s2 = e1+1;
int e2 = s2+gap-1 < array.length ? s2+gap-1 : array.length-1;
//1、肯定是有两个归并段的
while (s2 < array.length) {
//2、对应的s1位置和s2位置进行比较
while (s1 <= e1 && s2 <= e2){
if (array[s1] < array[s2]){
ret[k++] = array[s1++];
}else {
ret[k++] = array[s2++];
}
}
//3、上述第2步在比较的过程当中,肯定会有一个下标先走完一个归并段
//4、判断是谁没走完,把剩下的数据拷贝到结果数组当中
while (s1 <= e1){
ret[k++] = array[s1++];
}
while (s2 <= e2){
ret[k++] = array[s2++];
}
//5、接着确定新的s1,e1,s2,e2
s1 = s2;
e1 = s1+gap-1 < array.length ? s1+gap-1 : array.length-1;
s2 = e1+1;
e2 = s2+gap-1 < array.length ? s2+gap-1 : array.length-1;
}
while (s1 <= array.length-1){
ret[k++] = array[s1++];
}
for (int i = 0; i < ret.length; i++) {
array[i] = ret[i];
}
}