基本排序实现 笔记记录
- 一、插入排序
-
- 二、交换排序
-
- 三、选择排序
-
- 四、 归并排序
- 五、基数排序
一、插入排序
1.直接插入排序
import java.util.Arrays;
public class InsertSort {
//不带"哨兵"
static void InsertSort(int array[], int n) {//待排序列共n个元素
int temp, j;
for (int i = 1; i < n; i++) {
if (array[i] < array[i - 1]) { //arr[i]关键字小于前驱
temp = array[i];
for (j = i - 1; j >= 0 && temp < array[j]; j--) {
array[j + 1] = array[j]; //所有大于关键字的都向后移动
}
array[j + 1] = temp;
}
}
}
//带哨兵 不用每轮循环都判断j>=0
//使用哨兵第一个数组位置其实是没有使用的 因此 要排序n个元素 应给数组n+1的长度
static void InsertSort2(int array[], int n) {//待排序列共n个元素
int j;
for (int i = 2; i < n; i++) {
if (array[i] < array[i - 1]) {
array[0] = array[i]; //复制为哨兵
System.out.println("哨兵:"+array[0]);
for (j = i - 1; array[0] < array[j]; --j) {
array[j + 1] = array[j]; //所有大于关键字的都向后移动
}
array[j + 1] = array[0]; //复制到插入位置
}
}
}
public static void main(String[] args) {
int a[] = {0, 1, 5, 3, 4, 6, 9, 2,7};
InsertSort(a, a.length);
InsertSort2(a, a.length);
System.out.println(Arrays.toString(a));
}
}
2.折半插入排序
import java.util.Arrays;
public class InsertSort2 {
static void InsertSort(int arr[], int n) {
int j, temp, low, mid, high;
for (int i = 1; i < n; i++) {
temp = arr[i];
low = 0;
high = i - 1;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] > temp) {
high = mid - 1;
} else {
low = mid + 1;
}
}
//此处可与注释部分替换
for (j = i - 1; j >= high + 1; --j) { //j>=high+1 防止越界 与直接排序中的j>=0 一样的道理
arr[j + 1] = arr[j];
}
arr[high + 1] = temp;
}
}
public static void main(String[] args) {
int a[] = {1, 0, 5, 3, 4, 6, 9, 2, 7};
InsertSort(a, a.length);//[0, 1, 2, 3, 4, 5, 6, 7, 9]
System.out.println(Arrays.toString(a));
}
}
3.希尔排序
import java.util.Arrays;
public class ShellSort {
//希尔排序其实是比较距离为增量的 插入排序 本质还是插入排序
static void ShellSort(int arr[], int n) {
int j, temp;
for (int gap = n / 2; gap >0; gap /= 2) { //gap为增量
for (int i = gap; i < n; i++) {
if (arr[i] < arr[i - gap]) {
temp = arr[i];
for (j = i - gap; j >= 0 && temp < arr[j]; j -= gap) {
arr[j + gap] = arr[j];
}
arr[j + gap] = temp;
}
}
}
}
public static void main(String[] args) {
int a[] = {1, 0, 5, 3, 4, 6, 9, 2, 7};
ShellSort(a, a.length);//[0, 1, 2, 3, 4, 5, 6, 7, 9]
System.out.println(Arrays.toString(a));
}
}
二、交换排序
1.冒泡排序
import java.util.Arrays;
public class BubbleSort {
static void BubbleSort1(int []a,int n){
for (int i=0;i
for (int j=0;j
if (a[j]>a[j+1]){ //将最大的换到最后
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
public static void main(String[] args) {
int a[] = {8, 1, 5, 3, 4, 6, 9, 2,7};
BubbleSort1(a, a.length);
System.out.println(Arrays.toString(a));
}
}
2.快速排序
import java.util.Arrays;
public class QuicklySort {
static int a[];
public static void quickSort(int left, int right) {
if (left > right) {
return;
}
int temp = a[left]; // 基准
int i = left;
int j = right;
while (i != j) {
// 顺序很重要,要先从右往左找
while (a[j] >= temp && i < j){
j--;
}
// 再从左往右找
while (a[i] <= temp && i < j){
i++;
}
// 交换两个数在数组中的位置
if (i < j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
// 最终将基准归位
a[left] = a[i];
a[i] = temp;
quickSort(left, i - 1);// 处理左边
quickSort(i + 1, right);// 处理右边
return;
}
public static void main(String[] args) {
a = new int[]{6, 1, 2, 7, 9, 3, 4, 5, 10, 8};
System.out.println("未排序前:"+Arrays.toString(a));
//[6, 1, 2, 7, 9, 3, 4, 5, 10, 8]
quickSort(0, a.length-1);
System.out.println("排序后:"+Arrays.toString(a));
//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
}
}
三、选择排序
1.简单选择排序
import java.util.Arrays;
public class SelectSort {
static void SelectSort(int a[], int n) {//待排序列共n个元素
for (int i=0;i
int min=i;
for (int j=i+1;j
if (a[j]
min=j;
}
}
int temp =a[i];
a[i]=a[min];
a[min]=temp;
}
}
public static void main(String[] args) {
int a[] = {0, 1, 5, 3, 4, 6, 9, 2,7};
SelectSort(a, a.length);
System.out.println(Arrays.toString(a));
}
}
2.堆排序
在这里插入代码片
四、 归并排序
五、基数排序