1 sort

Date: 07/23 , 8/2

1) selection sort

[23451] - -> [12345]

Solution: Iterate the array, select the smallest item, and swap it with the item in current position.

Repeat until all the items are sorted.

Time: O(n^2)

Space: O(1)

2) merge sort


Solution: split to the smallest sorted unit ,then merge two sorted unit.

repeat until all the items are sorted.

Time: O(nlogn)

Space: O(n)

3) quick sort

solution: find a pivot position, make the left part smaller or equal than the pivot, the right part bigger or equal than pivot, each time make sure the pivot in the correct sorted position.

Time: O(nlogN)

Space: O(1)

4) rainbow sort

{1,0,1,-1,0} ->> {-1, 0,0,1,1}

solution 1: create a same size empty sorted array. When iterate the number array,  put -1 to the left part of the sorted array, 1 to the right part of the sorted array, between -1, and 1 , fill with 0.

Time: O(n)

Space: O(n)

solution 2: swap

Time: O(n)

Space: O(1)

5) move 0s to the end 1

Solution: use two pointers, one called start pointer, which start at the beginning position of the array, mainly used to iterate the array, the other one called the end pointer, start at the end position of the array. If the element at the start position is 0, swap the element with the element at the end pointer position. and then move end pointer one step towards the beginning, else move the start pointer one step towards the end. Repeat until two pointers meet.

Time: O(n)

Space: O(1)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,778评论 0 33
  • 本文是我正在写的gitbook上的书Algorithms一个章节,由于是记录课程的内容,所以下文都是英文,见谅。 ...
    eric_lai阅读 498评论 0 1
  • 夏日的阳光总给人一种可怖的感觉,谁都害怕遇见它。可是,你却没有退缩,反而开得那么艳丽。如果说盛开是一种美,那么...
    长辫子的麦阅读 529评论 0 0
  • 平行世界是个超级有趣的理论,这个理论可以假定有无数个世界存在,无数个世界中又有无数个你存在。你们是同一个人,但身份...
    c75ca80da992阅读 13,588评论 0 6
  • 中秋节和农历新年一样,是一个家人大团圆的日子,中秋之夜,月亮最圆最亮,月色也最娇美,家家户户把瓜果,月饼等食物,摆...
    jiangyan姜岩阅读 243评论 0 0