各种排序笔记---基于比较排序部分

1. 选择排序 selection sort

  大循环 从左到右每次以一个点开始扫描array

    小循环 找到从当前起始点开始的最小值

  时间复杂度为O(N^2)

//selection sort an array array[]
public class Solution {public int[] solve(int[] array) {if (array == null || array.length == 0) {return array;}for (int i = 0; i < array.length - 1; i++) {int gobalMin = i;for (int j = i + 1; j < array.length; j++) {if (array[j] < array[gobalMin]) {gobalMin = j;}}swap(array, i, gobalMin);}return array;}private void swap(int[] array, int i, int j) {int temp = array[i];array[i] = array[j];array[j] = temp;}
}
Selection sort

 

 

2. 归并排序 Merge sort

归并排序是基于一种被称为“分治”(divide and conquer)的策略。

Merge sort array:

public int[] mergeSort(int[] array) {if (array == null || array.length == 0) {return array;}int[] temp = new int[array.length];mergeSortHelper(array, 0, array.length - 1, temp);return array;}private void mergeSortHelper(int[] array, int start, int end, int[] temp) {if (start == end) {return;}int mid = start + (end - start) / 2;mergeSortHelper(array, start, mid, temp);mergeSortHelper(array, mid + 1, end, temp);merge(array, start, mid, end, temp);}private void merge(int[] array, int start, int mid, int end, int[] temp) {int left = start;int right = mid + 1;int index = start;while (left <= mid && right <= end) {if (array[left] < array[right]) {temp[index++] = array[left++];} else {temp[index++] = array[right++];}}while (left <= mid) {temp[index++] = array[left++];}while (right <= end) {temp[index++] = array[right++];}for (index = start; index <= end; index++) {array[index] = temp[index];}}
merge sort array

复杂度分析:

 

                         1 2 3 4 5 6 7 8 

                          /                当前层拆分需要劈1刀 O(1)

                       1 2 3 4

                       /                 当前层拆分需要劈2刀  O(2)

                     12                      ...

                    /

                   1                        当前层拆分需要劈n  /2刀

                  1 + 2 + 4 + 8+ ... + n/2 -> n  = O(n)  可以这样理解,终极状态下每个数字被切分成一个单位,n个数字,需要被切n-1刀

                  所以devide and conquer的上半部分的时间复杂度是O(n) 而不是log(n)

                  空间复杂度:考虑计算机里面只要保存的额外开销,其实是粉色部分,因为在任意时刻,计算机只有一个确定的状态,call stack在同一个确定的层只能保留部分的结果。比如最底层只能保留1,或者保留2,而不会1,2同时在栈里面!

                  所以空间复杂度:1 + 2 + 4 + 8 + ... + n = O(2n) = O(n)

                    ==============================================

            devide and conquer的上半部分,merge 部分, totoal time complexity is O(nlogn):

                      1      2         3     4       5    6      7       8

                       \/                \/             \/              \/            this level time complexity is O(n)

                       12               34            56            78

                         \     /                  \        /             this level time complexity is O(n)

                         1234        5678

                            \                          /              this level time complexity is O(n)

                             12345678

 

3. 快速排序

Array quick sort:

重点在于理解左右两个挡板的物理意义!!!

a. [0,....., left]: left 的左侧(不包含left)全部为比pivot小的数

b. [left, right]: left 和 right之间为未探索的区域

c. [right, ..... n-1]: right的右侧(不包含)全部为比pivot大或者等于的数字

public class Solution {/*** @param A an integer array* @return void*/public void sortIntegers2(int[] A) {quickSort(A, 0, A.length - 1);}private void quickSort(int[] A, int start, int end) {if (start >= end) {return;}int left = start, right = end;// key point 1: pivot is the value, not the indexint pivot = A[(start + end) / 2];// key point 2: every time you compare left & right, it should be // left <= right not left < rightwhile (left <= right) {// key point 3: A[left] < pivot not A[left] <= pivotwhile (left <= right && A[left] < pivot) {left++;}// key point 3: A[right] > pivot not A[right] >= pivotwhile (left <= right && A[right] > pivot) {right--;}if (left <= right) {int temp = A[left];A[left] = A[right];A[right] = temp;left++;right--;}}quickSort(A, start, right);quickSort(A, left, end);}
}
array quick sort

 

伪代码:

function quicksort(q)var list less, pivotList, greaterif length(q) ≤ 1 {return q} else {select a pivot value pivot from qfor each x in q except the pivot elementif x < pivot then add x to lessif x ≥ pivot then add x to greateradd pivot to pivotListreturn concatenate(quicksort(less), pivotList, quicksort(greater))}

 

 

 

 

 

Linkedlist quick sort

public class Solution {public ListNode sortList(ListNode head) {if (head == null || head.next == null) {return head;}ListNode mid = findMedian(head); // O(n)//new three dummmy node with a tail point to itListNode leftDummy = new ListNode(0), leftTail = leftDummy;ListNode rightDummy = new ListNode(0), rightTail = rightDummy;ListNode middleDummy = new ListNode(0), middleTail = middleDummy;//sprate to three part while (head != null) {if (head.val < mid.val) {leftTail.next = head;leftTail = head;} else if (head.val > mid.val) {rightTail.next = head;rightTail = head;} else {middleTail.next = head;middleTail = head;}head = head.next;}//make the tail to nullleftTail.next = null;middleTail.next = null;rightTail.next = null;//recurisive do the sortListNode left = sortList(leftDummy.next);ListNode right = sortList(rightDummy.next);//connect the three parts togetherreturn concat(left, middleDummy.next, right);}private static ListNode findMedian(ListNode head) {ListNode fast = head.next;ListNode slow = head;while (fast != null && fast.next != null) {slow = slow.next;fast = fast.next.next;}return slow;}private static ListNode concat(ListNode left, ListNode mid, ListNode right) {ListNode dummy = new ListNode(0), dummyTail = dummy;dummyTail = connect(dummyTail, left);dummyTail = connect(dummyTail, mid);dummyTail = connect(dummyTail, right);return dummy.next;}private static ListNode connect(ListNode dummyTail, ListNode current) {while (current != null) {dummyTail.next = current;dummyTail = dummyTail.next;current = current.next;}return dummyTail;}
}
sortList

 

 

相关题目整理: //to do 

转载于:https://www.cnblogs.com/jiangchen/p/5935343.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/254160.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

是什么让深度学习再次崛起并超越人类?

作者潘争&#xff0c;格灵深瞳计算机视觉工程师&#xff0c;清华大学自动化系博士&#xff0c;师从智能技术与系统国家重点实验室副主任张长水。深度学习(Deep Learning)这个词最近借着AlphaGO与李世石的人机大战又火了一把。深度学习其实是机器学习(Machine Learning)的一个分…

常见的流量问题

常见的流量问题 冗余内容同类请求被间隔执行&#xff0c;请求的内容包含一些相对静态的信息&#xff0c;正确的处理是第一次请求包括静态信息就好&#xff0c;后面的同类请求只包含必要的即时变化信息即可。错误的处理方式是每次请求服务器都返回一次静态信息。 冗余请求有的时…

halcon使用点拟合圆形时候,点集顺序紊乱,不影响圆形拟合效果

read_image (Image, 截图20201226094342972.bmp) * Matching 01: BEGIN of generated code for model initialization set_system (border_shape_models, false) * Matching 01: Obtain the model image * Matching 01: The image is assumed to be made available in the * Ma…

Socket理解。

其他大部分系统&#xff0c;例如CRM/CMS/权限框架/MIS之类的&#xff0c;无论怎么复杂&#xff0c;基本上都能够本地代码本地调试&#xff0c;性能也不太重要。&#xff08;也许这个就是.net的企业级开发的战略吧&#xff09; 可是来到通讯系统&#xff0c;一切变得困难复杂。原…

多元化时代敏捷软件开发的崛起与传统软件工程的延续

多元化时代敏捷软件开发的崛起与传统软件工程的延续 1.传统软件开发模式 1.1瀑布模型 1.1.1概念 瀑布模型&#xff0c;顾名思义&#xff0c;软件开发的过程如同瀑布飞流一般&#xff0c;自上而下&#xff0c;逐级下落。瀑布模型的核心思想是将问题按照工序进行简化&#xff0c;…

Linux中的cron计划任务配置详解

cron来源于希腊单词chronos&#xff08;意为“时间”&#xff09;&#xff0c;指Linux系统下一个自动执行指定任务的程序&#xff08;计划任务&#xff09; ####1. crontab命令选项代码如下: #crontab -u <-l, -r, -e> -u指定一个用户 -l列出某个用户的任务计划 -r删除某…

new和delete

和 sizeof 类似&#xff0c;sizeof不是函数&#xff0c;它是一个操作符&#xff0c;它在编译期就完成了计算&#xff0c;在函数运行期间它已经是一个常数值了。 int a;sizeof(int) 4;sizeof(a) 4;sizeof a ——也是4 不需要括号&#xff01;此时要注意&#xff1a;sizeof in…

char a[]和char *a的比较,数组名,数组首地址,a,a,a[0]

char a[]和char *a的比较 指针和数组存在着一些本质的区别。当然&#xff0c;在某种情况下&#xff0c;比如数组作为函数的参数进行传递时&#xff0c;由于该数组自动退化为同类型的指针&#xff0c;所以在函数内部&#xff0c;作为函数参数传递进来的指针与数组确实具有一定的…

Java中继承thread类与实现Runnable接口的区别

Java中线程的创建有两种方式&#xff1a; 1&#xff0e; 通过继承Thread类&#xff0c;重写Thread的run()方法&#xff0c;将线程运行的逻辑放在其中 2&#xff0e; 通过实现Runnable接口&#xff0c;实例化Thread类 在实际应用中&#xff0c;我们经常用到多线程&#xff0c;…

【VMware vSAN 6.6】6.2.启用性能服务:vSAN硬件服务器解决方案

目录 1. 简介 1.1.适用于HCI的企业级存储2. 体系结构 2.1.带有本地存储的服务器2.2.存储控制器虚拟系统套装的缺点2.3.vSAN在vSphere Hypervisor中自带2.4.集群类型2.5.硬件部署选项3. 启用vSAN 3.1.启用vSAN3.2.轻松安装3.3.主动测试4. 可用性 4.1.对象和组件安置4.2.重新构建…

Android eclipse导入项目后出现Unable to resolve target #39;android-17#39;解决方法

eclipse导入项目后出现Unable to resolve target android-17解决方法。在最后附带还有一种编译逻辑不成功情况解决方法。 一、问题情况 二、解决的方法 1、改动项目的目标版本号与当前Android sdk相相应的版本号 2、自己主动修复一下项目 三、这个问题不是上面的。是另外情况&a…

多个圆点,鼠标选取两个,求两个点的距离,用于计算像素尺寸(halcon实现)

read_image (Image, C:/Users/22967/Desktop/晶圆找位置/0.bmp) dev_close_window () dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle) dev_display (Image)binary_threshold (Image, Region1, max_separability, dark, UsedThreshold) connection (Region1, C…

修改UBOOT和LINUX调试串口(TI达芬奇芯片--DM6467)

Posted on 2011-10-31 10:53 jamiedu 阅读(889) 评论(0) 编辑 收藏 1.1 概述 TI针对DM6467提供的UBOOT和内核默认都是串口0作为调试串口输出的&#xff0c;但现在我需要使用DM6467的UART0的modem功能&#xff0c;所以修改代码&#xff0c;改变调试串口为串口2。 需要修改的主要…

Java List与数组之间的转换

http://blog.csdn.net/kingzone_2008/article/details/8444678转载于:https://www.cnblogs.com/longshiyVip/p/5985981.html

受欢迎的五个开源可视化工具——你的选择是?

摘要&#xff1a;大数据时代&#xff0c;数据为王&#xff0c;还在对一堆数据而发愁吗&#xff1f;试试可视化工具吧&#xff0c;相信本文提到的五款工具有一款能够帮助到你。人工智能时代&#xff0c;数据和算法以及硬件资源是非常重要的&#xff0c;相关行业的大公司也越来越…

halcon车刀崩边检测

list_files (新建文件夹, files, Files) read_image (Image, Files[0]) dev_close_window () get_image_size (Image, Width, Height) dev_open_window (0, 0, Width/1.5, Height/1.5, black, WindowHandle) dev_set_draw (margin) dev_set_colored (12) for Index:0 to |Files…

FFMPEG解码264文件步骤

本文以H264视频流为例&#xff0c;讲解解码流数据的步骤。 为突出重点&#xff0c;本文只专注于讨论解码视频流数据&#xff0c;不涉及其它&#xff08;如开发环境的配置等&#xff09;。如果您需要这方面的信息&#xff0c;请和我联系。 准备变量 定义AVCodecContext。如果…

Storm概念学习系列之storm的特性

不多说&#xff0c;直接上干货&#xff01; storm的特性 Storm 是一个开源的分布式实时计算系统&#xff0c;可以简单、可靠地处理大量的数据流。 Storm支持水平扩展&#xff0c;具有高容错性&#xff0c;保证每个消息都会得到处理&#xff0c;而且处理速度很快&#xff08;在一…

Confluence 6 配置服务器基础地址示例

2019独角兽企业重金招聘Python工程师标准>>> 如果 Confluence 的安装是没有安装在非根目录路径&#xff08;这个是上下文路径&#xff09;&#xff0c;然后服务器基础 URL 地址应该包括上下文地址。例如&#xff0c;你的 Confluence 正在运行在下面的地址&#xff1…

BootstrapValidator验证

bootstrap&#xff1a;能够增加兼容性的强大框架. 因为项目需要数据验证&#xff0c;看bootstrapValidator 还不错&#xff0c;就上手一直&#xff0c;完美兼容&#xff0c;话不多说。 需要引用css&#xff1a; bootstrap.min.css bootstrapValidator.min.css js: jquery-1.10.…