注:本博客由个人通过多篇文章整理归纳而来,希望帮助更多的人更好的理解排序相关知识,如有侵权,请私信删除:
时间复杂度与空间复杂度:
时间复杂度:
时间复杂度实际上是一个函数,代表基本操作重复执行的次数
空间复杂度:
是对一个算法在运行过程中临时占用存储空间的度量
排序算法对比
算法 时间复杂度(平均) 空间复杂度 稳定性
冒泡排序 o(n^2) O(1) 稳定
插入排序 o(n^2) O(1) 稳定
选择排序 o(n^2) O(1) 稳定
冒泡排序:
原理:比较两个相邻的元素,将值大的元素交换至右端
思路:依次比较相邻的两个数,将小数放在前面,大数放在后面。即在第一趟:首先比较第1个和第2个数,将小数放前,大数放后。然后比较第2个数和第3个数,将小数放前,大数放后,如此继续,直至比较最后两个数,将小数放前,大数放后。重复第一趟步骤,直至全部排序完成。
第一趟比较完成后,最后一个数一定是数组中最大的一个数,所以第二趟比较的时候最后一个数不参与比较;
第二趟比较完成后,倒数第二个数也一定是数组中第二大的数,所以第三趟比较的时候最后两个数不参与比较;
依次类推,每一趟比较次数-1;
……
举例说明:要排序数组:int[] arr={6,3,8,2,9,1};
第一趟排序:
第一次排序:6和3比较,6大于3,交换位置: 3 6 8 2 9 1
第二次排序:6和8比较,6小于8,不交换位置:3 6 8 2 9 1
第三次排序:8和2比较,8大于2,交换位置: 3 6 2 8 9 1
第四次排序:8和9比较,8小于9,不交换位置:3 6 2 8 9 1
第五次排序:9和1比较:9大于1,交换位置: 3 6 2 8 1 9
第一趟总共进行了5次比较, 排序结果: 3 6 2 8 1 9
第二趟排序:
第一次排序:3和6比较,3小于6,不交换位置:3 6 2 8 1 9
第二次排序:6和2比较,6大于2,交换位置: 3 2 6 8 1 9
第三次排序:6和8比较,6大于8,不交换位置:3 2 6 8 1 9
第四次排序:8和1比较,8大于1,交换位置: 3 2 6 1 8 9
第二趟总共进行了4次比较, 排序结果: 3 2 6 1 8 9
第三趟排序:
第一次排序:3和2比较,3大于2,交换位置: 2 3 6 1 8 9
第二次排序:3和6比较,3小于6,不交换位置:2 3 6 1 8 9
第三次排序:6和1比较,6大于1,交换位置: 2 3 1 6 8 9
第二趟总共进行了3次比较, 排序结果: 2 3 1 6 8 9
第四趟排序:
第一次排序:2和3比较,2小于3,不交换位置:2 3 1 6 8 9
第二次排序:3和1比较,3大于1,交换位置: 2 1 3 6 8 9
第二趟总共进行了2次比较, 排序结果: 2 1 3 6 8 9
第五趟排序:
第一次排序:2和1比较,2大于1,交换位置: 1 2 3 6 8 9
第二趟总共进行了1次比较, 排序结果: 1 2 3 6 8 9
最终结果:1 2 3 6 8 9
由此可见:N个数字要排序完成,总共进行N-1趟排序,每i趟的排序次数为(N-i)次,所以可以用双重循环语句,外层控制循环多少趟,内层控制每一趟的循环次数,即
for(int i=1;i<arr.length;i++){
for(int j=1;j<arr.length-i;j++){
//交换位置
}
用时间复杂度来说:
1.如果我们的数据正序,只需要走一趟即可完成排序。所需的比较次数C和记录移动次数M均达到最小值,即:Cmin=n-1;Mmin=0;所以,冒泡排序最好的时间复杂度为O(n)。
2.如果很不幸我们的数据是反序的,则需要进行n-1趟排序。每趟排序要进行n-i次比较(1≤i≤n-1),且每次比较都必须移动记录三次来达到交换记录位置。在这种情况下,比较和移动次数均达到最大值:冒泡排序的最坏时间复杂度为:O(n2) 。
综上所述:冒泡排序总的平均时间复杂度为:O(n2) 。
/*
* 冒泡排序
*/
public class BubbleSort {
public static void main(String[] args) {
int[] arr={6,3,8,2,9,1};
System.out.println("排序前数组为:");
for(int num:arr){
System.out.print(num+" ");
}
for(int i=0;i<arr.length-1;i++){//外层循环控制排序趟数
for(int j=0;j<arr.length-1-i;j++){//内层循环控制每一趟排序多少次
if(arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
System.out.println();
System.out.println("排序后的数组为:");
for(int num:arr){
System.out.print(num+" ");
}
}
}
public class BubbleSort {
private static int[] bubbleSort(int[] a) {
int len = a.length;
for (int i = 1; i < len - 1; i++) {
for (int j = 1; j < len - 1-i; j++) {
if (a[j + 1] < a[j]) {
swap(a, j + 1, j);
}
}
}
return a;
}
//交换方法
private static void swap(int[] a, int i, int j) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
//测试
public static void main(String[] args) {
int[] a = {1, 4, 6, 8, 99, 9, 2, 99};
int[] sort = bubbleSort(a);
for (int s : sort) {
System.out.print(s + " ");
}
}
}
冒泡排序的改进:
1.设置一标志性变量pos,用于记录每趟排序中最后一次进行交换的位置。由于pos位置之后的记录均已交换到位,故在进行下一趟排序时只要扫描到pos位置即可。
2.传统冒泡排序中每一趟排序操作只能找到一个最大值或最小值,我们考虑利用在每趟排序中进行正向和反向两遍冒泡的方法一次可以得到两个最终值(最大者和最小者) , 从而使排序趟数几乎减少了一半。
快速排序
1)选择一个基准元素,通常选择第一个元素或者最后一个元素,
2)通过一趟排序将待排序的记录分割成独立的两部分,其中一部分记录的元素值均比基准元素值小。另一部分记录的元素值比基准值大。
3)此时基准元素在其排好序后的正确位置
4)然后分别对这两部分记录用同样的方法继续进行排序,直到整个序列有序。
基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,
然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
假定数组A:46 30 82 90 56 17 95 15,取第一个数46位基数,l=0(l是字母,不是数字1)指向第一个数,h=7指向最后一个数:
从右向左找出第一个小于46的数;先比较A[0]和A[7]: 46 30 82 90 56 17 95 15 =》46和15比较=》15 30 82 90 56 17 95 46:交换位置,此时l需要+1变为l=1;h=7 (如果之前比较没有找到小于46的数,则继续取h=6位置的数和46比较,直到取到小于46的数为止) 然后从左向右找出第一个大于46的数:比较A[1]和A[7]: 15 30 82 90 56 17 95 46 =》30和46比较=》15 30 82 90 56 17 95 46:未交换位置,继续取左边下一个数字, 继续从左向右找出第一个大于46的数,此时所以l=2;h=7;比较A[2]和A[7]: 15 30 82 90 56 17 95 46 =》82和46比较=》15 30 46 90 56 17 95 82:交换位置 此时需要从右向左再找出下一个比46小的数,所以l=2,h=6,比较A[2]和A[6]: 15 30 46 90 56 17 95 82 =》46和95比较=》15 30 46 90 56 17 95 82:未交换位置 继续从左向右找比46小的数字,此时l=2,h=5,比较A[2]和A[5]: 15 30 46 90 56 17 95 46 =》46和17比较=》15 30 17 90 56 46 95 82:交换位置 再从左向右找比46大的数字,此时l=3,h=5;比较A[3]和A[5]: 15 30 17 90 56 46 95 82 =》90和46比较=》15 30 17 46 56 90 95 82:交换位置 再从右向左找比46小的数字,此时l=3,h=4; 比较A[3]和A[4]: 15 30 17 46 56 90 95 82 =》46和56比较=》15 30 17 42 56 90 95 82:为交换位置 继续从右向左找比46小的数字,此时l=3,h=3,l==h;此时A[3]左边数字(15,30,17,)全部是小于右边数字(90,95,82)的; 然后对子序列各自进行如上排序,直到子序列元素个数不大于1为止;
public static void main(String[] args) {
int[] a = {46, 30, 82, 90, 56, 17, 95, 15};
int start = 0;
int end = a.length - 1;
sort(a, start, end);
for (int anA : a) {
System.out.println(anA);
}
}
public static void sort(int arr[], int low, int high) {
int l = low;
int h = high;
int baseNum = arr[low];
while (l < h) {
//1.从右向左查找小于指定基数的数,找到之后跳出循环执行下面if循环,交换数据
while (l < h && arr[h] >= baseNum) {
h--;
}
//交换数据
if (l < h) {
int temp = arr[h];
arr[h] = arr[l];
arr[l] = temp;
l++;
}
//2.从左向右查找大于指定基数的数,找到后跳出循环执行下面if循环,交换数据
while (l < h && arr[l] <= baseNum)
l++;
//交换数据
if (l < h) {
int temp = arr[h];
arr[h] = arr[l];
arr[l] = temp;
h--;
}
}
if (l > low) {
sort(arr, low, l - 1);
}
if (h < high) {
sort(arr, l + 1, high);
}
}
//输出结果:
15
17
30
。
。
。
public void sort(int[] a,int low,int high){
int start = low;
int end = high;
int key = a[low];
while(end>start){
//从后往前比较
while(end>start&&a[end]>=key) //如果没有比关键值小的,比较下一个,直到有比关键值小的交换位置,然后又从前往后比较
end--;
if(a[end]<=key){
int temp = a[end];
a[end] = a[start];
a[start] = temp;
}
//从前往后比较
while(end>start&&a[start]<=key)//如果没有比关键值大的,比较下一个,直到有比关键值大的交换位置
start++;
if(a[start]>=key){
int temp = a[start];
a[start] = a[end];
a[end] = temp;
}
//此时第一次循环比较结束,关键值的位置已经确定了。左边的值都比关键值小,右边的值都比关键值大,但是两边的顺序还有可能是不一样的,进行下面的递归调用
}
//递归
if(start>low) sort(a,low,start-1);//左边序列。第一个索引位置到关键值索引-1
if(end<high) sort(a,end+1,high);//右边序列。从关键值索引+1到最后一个
}
public class QuickSort {
private static int[] quickSort(int[] a, int low, int high) {
//中心点
int mid = 0;
if (low < high) {
mid = partition(a, low, high);
quickSort(a, low, mid - 1);
quickSort(a, mid + 1, high);
}
return a;
}
private static int partition(int[] a, int low, int high) {
int b = a[low];
while (low < high) {
while (low < high && a[high] >= b) {
high--;
}
a[low] = a[high];
while (low < high && a[low] <= b) {
low++;
}
a[high] = a[low];
}
a[low] = b;
return low;
}
//测试
public static void main(String[] args) {
int[] a = {1, 14, 6, 8, 99, 9, 2, 99};
int[] sort = quickSort(a, 0, 7);
for (int s : sort) {
System.out.print(s + " ");
}
}
选择排序:
a) 原理:每一趟从待排序的记录中选出最小的元素,顺序放在已排好序的序列最后,直到全部记录排序完毕。也就是:每一趟在n-i+1(i=1,2,…n-1)个记录中选取关键字最小的记录作为有序序列中第i个记录。基于此思想的算法主要有简单选择排序、树型选择排序和堆排序。(这里只介绍常用的简单选择排序)
b) 简单选择排序的基本思想:给定数组:int[] arr={里面n个数据};第1趟排序,在待排序数据arr[1]arr[n]中选出最小的数据,将它与arrr[1]交换;第2趟,在待排序数据arr[2]arr[n]中选出最小的数据,将它与r[2]交换;以此类推,第i趟在待排序数据arr[i]~arr[n]中选出最小的数据,将它与r[i]交换,直到全部排序完成。
c) 举例:数组 int[] arr={5,2,8,4,9,1};
第一趟排序: 原始数据:5 2 8 4 9 1
最小数据1,把1放在首位,也就是1和5互换位置,
排序结果:1 2 8 4 9 5
第二趟排序:
第1以外的数据{2 8 4 9 5}进行比较,2最小,
排序结果:1 2 8 4 9 5
第三趟排序:
除1、2以外的数据{8 4 9 5}进行比较,4最小,8和4交换
排序结果:1 2 4 8 9 5
第四趟排序:
除第1、2、4以外的其他数据{8 9 5}进行比较,5最小,8和5交换
排序结果:1 2 4 5 9 8
第五趟排序:
除第1、2、4、5以外的其他数据{9 8}进行比较,8最小,8和9交换
排序结果:1 2 4 5 8 9
注:每一趟排序获得最小数的方法:for循环进行比较,定义一个第三个变量temp,首先前两个数比较,把较小的数放在temp中,然后用temp再去跟剩下的数据比较,如果出现比temp小的数据,就用它代替temp中原有的数据。具体参照后面的代码示例,相信你在学排序之前已经学过for循环语句了,这样的话,这里理解起来就特别容易了。
//选择排序
public class SelectionSort {
public static void main(String[] args) {
int[] arr={1,3,2,45,65,33,12};
System.out.println("交换之前:");
for(int num:arr){
System.out.print(num+" ");
}
//选择排序的优化
for(int i = 0; i < arr.length - 1; i++) {// 做第i趟排序
int k = i;
for(int j = k + 1; j < arr.length; j++){// 选最小的记录
if(arr[j] < arr[k]){
k = j; //记下目前找到的最小值所在的位置
}
}
//在内层循环结束,也就是找到本轮循环的最小的数以后,再进行交换
if(i != k){ //交换a[i]和a[k]
int temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
}
System.out.println();
System.out.println("交换后:");
for(int num:arr){
System.out.print(num+" ");
}
}
}
选择排序的时间复杂度:简单选择排序的比较次数与序列的初始排序无关。 假设待排序的序列有 N 个元素,则比较次数永远都是N (N - 1) / 2。而移动次数与序列的初始排序有关。当序列正序时,移动次数最少,为 0。当序列反序时,移动次数最多,为3N (N - 1) / 2。
所以,综上,简单排序的时间复杂度为 O(N2)。
创建一棵树
public class Node {
private int data;
private Node leftNode;
private Node rightNode;
public Node(int data, Node leftNode, Node rightNode){
this.data = data;
this.leftNode = leftNode;
this.rightNode = rightNode;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getLeftNode() {
return leftNode;
}
public void setLeftNode(Node leftNode) {
this.leftNode = leftNode;
}
public Node getRightNode() {
return rightNode;
}
public void setRightNode(Node rightNode) {
this.rightNode = rightNode;
}
}
遍历二叉树:
public class BinaryTree {
/**
* @author yaobo
* 二叉树的先序中序后序排序
*/
public Node init() {//注意必须逆序建立,先建立子节点,再逆序往上建立,因为非叶子结点会使用到下面的节点,而初始化是按顺序初始化的,不逆序建立会报错
Node J = new Node(8, null, null);
Node H = new Node(4, null, null);
Node G = new Node(2, null, null);
Node F = new Node(7, null, J);
Node E = new Node(5, H, null);
Node D = new Node(1, null, G);
Node C = new Node(9, F, null);
Node B = new Node(3, D, E);
Node A = new Node(6, B, C);
return A; //返回根节点
}
public void printNode(Node node){
System.out.print(node.getData());
}
public void theFirstTraversal(Node root) { //先序遍历
printNode(root);
if (root.getLeftNode() != null) { //使用递归进行遍历左孩子
theFirstTraversal(root.getLeftNode());
}
if (root.getRightNode() != null) { //递归遍历右孩子
theFirstTraversal(root.getRightNode());
}
}
public void theInOrderTraversal(Node root) { //中序遍历
if (root.getLeftNode() != null) {
theInOrderTraversal(root.getLeftNode());
}
printNode(root);
if (root.getRightNode() != null) {
theInOrderTraversal(root.getRightNode());
}
}
public void thePostOrderTraversal(Node root) { //后序遍历
if (root.getLeftNode() != null) {
thePostOrderTraversal(root.getLeftNode());
}
if(root.getRightNode() != null) {
thePostOrderTraversal(root.getRightNode());
}
printNode(root);
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
Node root = tree.init();
System.out.println("先序遍历");
tree.theFirstTraversal(root);
System.out.println("");
System.out.println("中序遍历");
tree.theInOrderTraversal(root);
System.out.println("");
System.out.println("后序遍历");
tree.thePostOrderTraversal(root);
System.out.println("");
}
}
对链表的操作:
**
* Created by Administrator on 2017-10-27.
*/
public class linkedListFuns {
public static void main(String[] arg) {
Node head = new Node(1);
for(int i=2;i<10;i++){
insertFromTail(head,new Node(i));
}
printList(head);
deleteFromIndex(head,3);
printList(head);
}
static class Node {
int data;
Node next;
public Node(int d) {
data = d;
next = null;
}
}
//从头节点插入,比较简单不用遍历链表
public static void insetFromHead(Node head,Node newNode){
newNode.next=head;
head = newNode;
}
//在尾部插入,要遍历链表
public static void insertFromTail(Node head1, Node newNode){
if(head1 == null){ //如果是个空链表,直接把新节点赋值给head,然后结束,要先判断null的情况 其实这是一段错误代码,大家可以查看我另外一篇文章,Java参数引用传递之例外:null
head1 =newNode;
return;
}
Node temp = head1; //用temp代替head去遍历找到最后一个节点,一定不要用head自己去遍历,不然就找不到链表头了
while (temp.next!=null){
temp=temp.next;
}
temp.next=newNode;
}
//计算链表的长度
public static int length(Node head){
int len =0;
Node temp = head;
while(temp!=null){
len++;
temp=temp.next;
}
return len;
}
//从特定位置删除一个节点
public static boolean deleteFromIndex(Node head,int index){
if(index<1||index>length(head)){ //先判断是否越界
return false;
}
if(index ==1){//如果是删除第一个元素,因为直接涉及到了head所以只能单独处理
head = head.next;
return true;
}
Node curNode = head;
for(int curIndex =1;curIndex<index-1;curIndex++){ //删除顺序为index的node只能将curNode停在index-1的位置
curNode = curNode.next;
}
curNode.next=curNode.next.next;
return true;
}
//按照顺序输出一个列表
public static void printList(Node head){
Node temp = head;
while(temp != null){
System.out.print(temp.data+" ");
temp = temp.next;
}
System.out.println();
}
//对链表进行冒泡排序
public static void orderList(Node head){
}
}
二分查找法:
// 二分查找普通循环实现
public static int binSearch(int srcArray[], int key) {
int mid = srcArray.length / 2;
if (key == srcArray[mid]) {
return mid;
}
int start = 0;
int end = srcArray.length - 1;
while (start <= end) {
mid = (end - start) / 2 + start;
if (key < srcArray[mid]) {
end = mid - 1;
} else if (key > srcArray[mid]) {
start = mid + 1;
} else {
return mid;
}
}
return -1;
}
}
/**
* Created by david on 2018/8/16
* 查找前的数据必须是已经排好序的, 然后得到数组的开始位置start和结束位置end,
* 取中间位置mid的数据a[mid]跟待查找数据key进行比较, 若 a[mid] > key, 则取end = mid - 1;
* 若 a[mid] < key, 则取start = mid + 1; 若 a[mid] = key 则直接返回当前mid为查找到的位置.
* 依次遍历直到找到数据或者最终没有该条数据
*/
public class BinarySearch {
public static int binarySearch(int[] a, int key) {
int start = 0;
int end = a.length - 1;
int mid = -1;
while (start <= end) {
mid = (start + end) / 2;
if (a[mid] == key) {
return mid;
} else if (a[mid] > key) {
end = mid - 1;
} else if (a[mid] < key) {
start = mid + 1;
}
}
return -1;
}
// 测试
public static void main(String[] args) {
int[] a = {1, 4, 6, 8, 99};
int i = binarySearch(a, 99);
System.out.println(i);
}
斐波那契额数列
斐波那契数列(Fibonacci sequence)的定义:斐波那契数列指的是这样一个数列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368........,这个数列从第3项开始,每一项都等于前两项之和。
public class PrintFib {
public static void main(String[] args) {
//定义第一个加数a,初始值为1;定义第二个加数b,初始值为1;定义两个加数之和为c,初始值为0
int a = 1;
int b = 1;
int c = 0;
//首先在控制台打印出数列中第一个数和第二个数的值
System.out.print(a + "\t" + b + "\t");
//建立一个for循环,用于循环输出数列中第三位至第十位的数字
for (int i = 3; i <= 10; i++) {
//第三个数即为c,a+b等于c的值
c = a + b;
//将第一个加数a赋值为数列中的第二个数b的值
a = b;
//将第二个加数b赋值为数列中的第三个数c的值
b = c;
//在第二次循环打印时,将打印数列中的第四个数为:b + c = b + (a + b)
System.out.print(c + "\t");
}
}
}
B树
B树是一种多叉树,也叫多路搜索树,适合用于文件索引上,减少磁盘IO次数,子节点存储最大数成为B树的阶,图中为2-3树。
m阶B树特点:
非叶节点最多有m棵子树。
根节点最少有两棵子树,非根非叶节点最少有m/2棵子树。
非叶节点保存的关键字个数等于该节点子树个数-1。
非叶节点保存的关键字大小有序。
节点中每个关键字左子树的关键字都小于该该关键字,右子树的关键字都大于该该关键字。
所有叶节点都在同一层。
查找:
对节点关键字进行二分查找。
如果找不到,进入对应的子树进行二分查找,如此循环。
B+树
B树的变种,拥有B树的特点
独有特点:
节点中的关键字与子树数目相同。
关键字对应的子树节点都大于等于该关键字,子树包含该关键字自身。
所有关键字都出现在叶节点之中。
所有叶节点都有指向下一个叶节点的指针。
搜索:只在叶节点搜索。
叶子节点保存关键字和对应的数据,非叶节点只保存关键字和指向叶节点的指针,同等关键字数量的B树和B+树,B+树更小。
更适合做索引系统,原因:
由于叶节点有指针项链,B+树更适合做范围检索。
由于非叶节点只保存关键字和指向叶节点的指针,B+树可以容纳更多的关键字,树层数变小,磁盘查询次数更低。
B+树的查询效率比较稳定,查询所有关键字的路径相同。(MySQL索引就提供了B+树的实现方式)