目录
一.普通排序法
1.冒泡排序(交换排序)
2.直接插入排序
3.选择排序
二.较为高级的排序法
1.希尔排序(属于插入排序)
2.归并排序
3.快速排序(属于交换排序)
4.堆排序(属于选择排序)
时间复杂度:O(n^2) ,具有稳定性。
动态效果:排序(冒泡排序,选择排序,插入排序,归并排序,快速排序,计数排序,基数排序) - VisuAlgo
代码如下:
public class BubbleSort {
// 具有稳定性 时间复杂度O(n^2)
public void Sort(int[] array){
for (int i = 0; i < array.length-1; i++) { //元素n个 ,就循环n-1个
boolean sorted = true;
for (int j = 0; j < array.length-1; j++) {
if(array[j]>array[j+1]){
swap(array,j,j+1);
sorted = false;
}
}
if(sorted){
return;
}
}
}
private void swap(int[] array, int j, int i) {
int e = array[i];
array[i] =array[j];
array[j] = e;
}
整个区间被分为
1. 有序区间
2. 无序区间
每次选择无序区间的第一个元素,在有序区间内选择合适的位置插入.(具备稳定性 ,时间复杂度:O(n^2)-> 平均)
代码如下:
public class InsertSort {
public void Sort(int[] array){
for (int i = 0; i < array.length-1; i++) {
int e = array[i+1];
int j ;
for(j =i;j>=0 && array[j]> e;j--){
array[j+1] =array[j];
}
array[j] = e;
}
}
}
代码如下:
public void selectSort(long[] array) {
for (int i = 0; i < array.length - 1; i++) {
int maxIdx = 0;
for (int j = 1; j < array.length - i; j++) {
if (array[j] > array[maxIdx]) {
maxIdx = j;
}
}
if (maxIdx != array.length - i - 1) {
swap(array, maxIdx, array.length - i - 1);
}
}
}
private void swap(int[] array, int j, int i) {
int e = array[i];
array[i] =array[j];
array[j] = e;
}
时间复杂度:O(n*1.3) ,不具备稳定性。
原理:
public void shellSort(long[] array) {
for (int g = array.length / 2; g > 1; g = g / 2) {
insertSortWithGap(array, g);
}
insertSortWithGap(array, 1);
}
private void insertSortWithGap(long[] array, int g) {
for (int i = 0; i < array.length - g; i++) {
long key = array[i + g];
int j;
for (j = i; j >= 0 && array[j] > key; j = j - g) {
array[j + g] = array[j];
}
array[j + g] = key;
}
}
原理 :合并两个有序数组
时间复杂度:O(n* log(n)) , 具备稳定性。
代码如下:
private void RangeSort(long[] array, int from, int to) {
int size = to - from;
if (size <= 1) {
return;
}
int mid = from + (size / 2);
sortRange(array, from, mid);
sortRange(array, mid, to);
merge(array, from, mid, to);
}
private void merge(long[] array, int from, int mid, int to) {
int size = to - from;
int left = from;
int right = mid;
int dest = 0;
long[] other = new long[size];
while (left < mid && right < to) {
if (array[left] <= array[right]) {
other[dest++] = array[left++];
} else {
other[dest++] = array[right++];
}
}
while (left < mid) {
other[dest++] = array[left++];
}
while (right < to) {
other[dest++] = array[right++];
}
for (int i = 0; i < size; i++) {
array[from + i] = other[i];
}
}
代码如下:
public void SortQuilk(long[] array,int from,int to){
if(to-from<=0){
return;
}
int pi = PartitionMethodC(array,from,to);
SortQuilk(array,from,pi-1);
SortQuilk(array,pi+1,to);
}
private int PartitionMethodC(long[] array,int from,int to){
int s= from;
long pivot = array[to];
for (int i = from; i < to; i++) {
if(array[i]
时间复杂度:O(n*log(n)) ,不具备稳定性。
原理:
代码如下:
// 堆排序就是 建堆后 ,将第一个最大的元素和最后一个元素交换,后向下调整后,重复循环
public void Sort(long[] array){
createHeap(array);
for (int i = 0; i < array.length-1; i++) {
swap(array,0,array.length-1-i);
shiftDown(array,array.length-1-i,0);
}
}
// 建堆
public void createHeap(long[] array){
for(int i = (array.length-2)/2;i>=0;i--){
shiftDown(array,array.length,i);
}
}
private void shiftDown(long[] array, int size, int index) {
// TODO: 2022/3/17 向下调整,构建大堆 index > maxIdex
while(index*2+1array[maxIdex]){
maxIdex =right;
}
if(array[index]
动态效果:数据结构和算法动态可视化 (Chinese) - VisuAlgoVisuAlgo was conceptualised in 2011 by Dr Steven Halim as a tool to help his students better understand data structures and algorithms, by allowing them to learn the basics on their own and at their own pace. Together with his students from the National University of Singapore, a series of visualizations were developed and consolidated, from simple sorting algorithms to complex graph data structures. Though specifically designed for the use of NUS students taking various data structure and algorithm classes (CS1010/equivalent, CS2040/equivalent, CS3230, CS3233, and CS4234), as advocators of online learning, we hope that curious minds around the world will find these visualizations useful as well.https://visualgo.net/zh