0.总结
常见算法复杂度.jpg
O(1) < O(logn) < O(n) < O(nlogn) < O(n^2) < O(n^3) < O(2^n),logn的底数为2
1.归并排序
package DailyPractice;
import java.util.*;
public class Test1 {
/**
* 归并排序的思路:先将数组的左边和右边分开排完序之后再合并,
* 其中左边和右边的排序也是递归用这种先分开排序再合并的思想。
* 步骤:
* 1.MergeSort左边
* 2.MergeSort右边
* 3.Merge两边(两边的头先开始比较,把较小的放进临时数组,
* 然后将左边剩余的移进数组,再将右边剩余的移进数组,最后将临时数组覆盖特定部分的旧数组)
* @param args
*/
public static void main(String[] args) {
int[] a = new int[]{2,45,8,147,312,42,478};
MyMergeSort(a,0,a.length-1);
System.out.println(Arrays.toString(a));
}
private static void MyMergeSort(int[] a, int low, int high) {
int middle = (high+low)/2;
if (low
MyMergeSort(a,low,middle);
MyMergeSort(a,middle+1,high);
MyMerge(a,low,middle,high);
}
}
private static void MyMerge(int[] a, int low, int middle, int high) {
int[] temp = new int[high-low+1];
int i = low;
int j = middle+1;
int k = 0;
//把较小的先移进数组
while(i<=middle && j <=high){
if (a[i]
temp[k++] = a[i++];
}else {
temp[k++] = a[j++];
}
}
while(i<=middle){
temp[k++] = a[i++];
}
while (j<=high){
temp[k++] = a[j++];
}
//将新数组覆盖旧数组
for (int l = 0; l < temp.length; l++) {
a[l+low] = temp[l];
}
}
}
2.快速排序
package DailyPractice;
import java.util.*;
public class Test1 {
/**
* 快速排序的思路:首先选择一个锚点,将比他小的数移到他的左边,比他大的数移到右边,
* 然后用同样的思想对左边和右边进行排序
* @param args
*/
public static void main(String[] args) {
int[] a = new int[]{2,45,8,147,312,42,478};
MyQuickSort(a,0,a.length-1);
System.out.println(Arrays.toString(a));
}
private static void MyQuickSort(int[] a, int low, int high) {
if (low>high)return;
int pivot = a[low];
int i = low;
int j = high;
while (i!=j){
//记得要先从右边先开始
while (a[j]>=pivot && i
j--;
}
while (a[i]<=pivot && i
i++;
}
if (i
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
a[low] = a[i];
a[i] = pivot;
MyQuickSort(a, low, i-1);
MyQuickSort(a,i+1,high);
}
}
3.选择排序
package DailyPractice;
import java.util.*;
public class Test1 {
/**
* 选择排序的思路:首先第一次遍历数组找出最小的数,放在最左边,
* 第二次遍历找出第二小的数放在第二个位置,依次遍历直到数组排完。
* 其中的方法是:定一个索引,依次遍历数组,如果比他小的就将索引换为该数的下标,最后如果
* 索引和一开始的下标不一样就替换。
* @param args
*/
public static void main(String[] args) {
int[] a = new int[]{2,45,8,147,312,42,478};
MySelectionSort(a);
System.out.println(Arrays.toString(a));
}
private static void MySelectionSort(int[] a) {
for (int i = 0; i < a.length; i++) {
int minIndex = i;
for (int j = i; j < a.length; j++) {
if (a[j]