加粗样式# 常用的算法java
实现
1.归并排序
import java.util.Arrays;
public class MergeSort {
public static void main(String[] args) {
int[] a = {10,9,8,7,6,5,4,3,2,1};
sort(a,0,a.length-1);
//排序结果 =>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
System.out.println(Arrays.toString(a));
}
public static void sort(int[] a ,int left ,int right){
int mid = (left+right)/2;
//通过递归,进行分治拆分
if (left<right){
sort(a,left,mid);
sort(a,mid+1,right);
//进行合并,需要借助另外一个临时数组temp
merge(a,left,mid,right);
}
}
private static void merge(int[] a, int left, int mid, int right) {
int l = left;
int r = mid+1;
//用来记录temp的下标位置
int ind = 0;
int[] temp = new int[right - left + 1];
while (l<=mid && r <= right){
//谁小谁先放进temp
if(a[l]< a[r]){
temp[ind++] = a[l++];
}else temp[ind++] = a[r++];
}
//将其余的元素一次放进temp
while (l<=mid) {
temp[ind++] = a[l++];
}
while (r<=right){
temp[ind++] = a[r++];
}
//元素整齐放完之后将temp里面的元素一次放进原数组a
for (int i = 0; i < temp.length; i++) {
a[left+i] = temp[i];
}
}
}
2.快排
import java.util.Arrays;
public class QuickSort {
/*********************************************|
*快排主要是思想是将pivot左边的值调节到都比pivot小**|
*右边都比pivot的值要大**************************/
public static void main(String[] args) {
int[] a = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
sort(a, 0, a.length - 1);
// => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
System.out.println(Arrays.toString(a));
}
public static void sort(int[] a, int left, int right) {
//进行简单的校验
if (a.length == 1) return;
int l = left;
int r = right;
int mid = (left + right) / 2;
int pivot = a[mid];
//不能是l <= r否则会出问题
while (l < r) {
//从pivot左边找到第一个比pivot大的元素,最坏l == mid
while (a[l] < pivot) {
l++;
}
while (a[r] > pivot) {
r--;
}
//继续校验,避免异常
if (l >= r) break;
//交换位置
a[l] = a[l] ^ a[r];
a[r] = a[l] ^ a[r];
a[l] = a[l] ^ a[r];
if (a[l] == pivot) r--;
if (a[r] == pivot) l++;
}
//校验,不然会造成 StackoverFlow
if (l == r) {
l++;
r--;
}
if (l < right) sort(a, l, right);
if (r > left) sort(a, left, r);
}
}
3.希尔排序
import java.util.Arrays;
public class ShellSort {
public static void main(String[] args) {
int[] a = {9, 8, 7, 6, 5, 4, 3, 2, 1};
sort(a);
// => [1, 2, 3, 4, 5, 6, 7, 8, 9]
System.out.println(Arrays.toString(a));
}
public static void sort(int[] a) {
if (a.length == 1) return;
//确定初始步长
//每个步长都要事先一次插入排序
for (int gap = (a.length - 1) / 2; gap >= 1; gap /= 2) {
for (int i = gap; i <= a.length - 1; i++) {
int temp = a[i];
int pos = i;
while (pos - gap >= 0 && temp < a[pos - gap]) {
//移动位置
a[pos] = a[pos - gap];
pos -= gap;
}
a[pos] = temp;
}
}
}
}
4.插入排序
import java.util.Arrays;
public class InsertSort {
public static void main(String[] args) {
int[] a = {5,4,3,2,1};
sort(a);
System.out.println(Arrays.toString(a));
}
public static void sort(int[] a) {
if (a.length == 1) return;
//确定从哪个下标开始插入开始插入
for (int i = 1; i < a.length; i++) {
int pos = i;
int temp = a[i];
while (pos - 1 >= 0 && temp < a[pos - 1]) {
a[pos] = a[pos - 1];
pos--;
}
a[pos] = temp;
}
}
}
5.选择排序
import java.util.Arrays;
public class SelectSort {
public static void main(String[] args) {
int[] a = {5, 4, 3, 2, 1};
sort(a);
// => [1, 2, 3, 4, 5]
System.out.println(Arrays.toString(a));
}
public static void sort(int[] a) {
if (a.length == 1) return;
//确定插入子集的起始位置,选出一个最小值放在该位置
for (int i = 0; i < a.length - 1; i++) {
int minVal = Integer.MAX_VALUE;
int minPos = i;
for (int j = i; j < a.length; j++) {
if (a[j] < minVal) {
minVal = a[j];
minPos = j;
}
}
a[minPos] = a[i];
a[i] = minVal;
}
}
}
6.冒泡排序
import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
int[] a = {5, 4, 3, 2, 1};
sort(a);
// => [1, 2, 3, 4, 5]
System.out.println(Arrays.toString(a));
}
public static void sort(int[] a) {
if (a.length == 1) return;
for (int i = 0; i < a.length - 1; i++) {
// 一个小小的优化,避免无效的循环
boolean flag = false;
for (int j = 0; j < a.length - 1 - i; j++) {
if (a[j] > a[j + 1]) {
a[j] = a[j] ^ a[j + 1];
a[j + 1] = a[j] ^ a[j + 1];
a[j] = a[j] ^ a[j + 1];
flag = true;
}
}
//如果flag == flase 说明整个序列已经是有序的了
if (!flag) break;
}
}
}