Time complexity:

Binary search O(log2 n):

i=0.   n elements:         -------------------

i=1.   n/2 elements:                   ----------

i=2.   n/4 elements:                          -----

...

i=i.    n/2^i elements:                        -

进行n/2^i=1, i=log2 n 次,来达到只剩一个element.

1,Selection Sort

Steps:

from 0~n-1, select the minimum element[comparing n-1 times], swap it with the A[0].

from 1~n-1, select the minimum element[comparing n-2 times], swap it with the A[1].

...

So total comparison times: n-1+n-2+...+2+1 = n(n-1)/2=O(n^2).

2, Insertion Sort

Steps:

i, compare with j=0~i-1=0, if bigger than A[j], nothing changes; if smaller than A[j], A[j+1] = A[j], move A[i] to position j.

So total comparison times:  1+2+...+n-1=n(n-1)/2=O(n^2).

3, Merge 2 sorted arrays

像两叠放着的纸牌,最上面放着最小的牌,指针从上开始往下,注意的是两叠的高度不同,一叠到底以后只需考虑另一叠。

4, Bubble Sort

也是两层loop, 比较相邻两个element,如果前面大于后面,就swap,这样每一轮把最大的那个扔到末端。

public static void sort(int[] a) {

  for(int i = 0; i< a.length; i++) {

    for(int j = 0; j<a.length -1 - i; j++) {
      if(a[j] > a[j+1]) {
        int temp = a[j];
        a[j]= a[j+1];
        a[j+1] = temp;
    }}}
}

5, Quick Sort

Divide and Conquer, Recuisive, Complexity: O(nlogn), worst-case: O(n^2)[if you pick the pivot which is the biggest/smallest value in the array].

private static void quickSort2(int[] array, int low, int high) {
  int i = low, j= high;
  int mid = (low + high) /2;
  while(i<=j) {
    while(array[i] < array[mid]) i++;
    while(array[j] > array[mid]) j--;
    if(i<= j) {
      int temp = array[i];
      array[i] = array[j];
      array[j] = temp;
      i++;  //Don't forget to increase/decrease index here
      j--;
    }
  }
  if(low < j) quickSort2(array, low, j);
  if(i < high) quickSort2(array, i, high);
}

6, Merge Sort

Worst-case and average are both: O(nlogn). 每层都比较n次,有logn层。

Use helplerArray to copy from, One method for divide and conquer, One method for merging lower part and higher part.

private void mergeSort(int low, int high) {
  if(low<high) {
    int middle = (low + high)/2;
    mergeSort(low, middle);
    mergeSort(middle +1, high);
    merge(low, middle, high);
  }
}
private void merge(int low, int middle, int high) {
  for(int i = low; i<= high; i++)
    helper[i] = this.numbers[i];
  int i = low;            //pointer i for low part of helper array
  int j = middle + 1;  //pointer j for higher part of helper array
  int k = low;           //pointer k for target array
  while(i<= middle && j<= high) {
    if(helper[i] < helper[j]) numbers[k++] = helper[i++];
    else numbers[k++] = helper[j++];
  }
  while(j<=high) numbers[k++] = helper[j++];
  while(i<=middle) numbers[k++] = helper[i++];
}

Steps: 1, private var int[] numbers, int[] helper; 2, private method mergeSort(int low, int high), 在这里做recursion,先mergeSort(low, mid)再mergeSort(mid+1, high); 3, private method merge(int low, int mid, int high),在这里先把low~high赋给helper array,然后开始比较.

7, Radix Sort O(n)

Radix Sort puts the elements in order by comparing the digits of the numbers. We should start sorting by comparing and ordering the one's digits:Now, we gather the sublists (in order from the 0 sublist to the 9 sublist) into the main list again:the sublists are created again, this time based on the ten's digit:Finally, the sublists are created according to the hundred's digit.

In the example above, the numbers were all of equal length, but many times, this is not the case. If the numbers are not of the same length, then a test is needed to check for additional digits that need sorting. This can be one of the slowest parts of Radix Sort, and it is one of the hardest to make efficient.

时间复杂度O(n*k),k是位数。k不一定小于logn。

Java program:

func(int[] arr, int maxDigits)

int exp = 1;

for(int i = 0; i<maxDigits; i++) {

  ArrayList[] bucketList = new ArrayList[10]; //use ArrayList<ArrayList<Integer>>.

  for(int k = 0; k<10; k++)  bucketList[k] = new ArrayList();

  for(int j = 0; j< arr.length; j++) { int index = arr[j]/exp%10; bucketList[index].add(arr[j]);

  exp = exp *10;

  int number = 0;

  for(int k = 0; k<10; k++) {//循环bucketList将所有element回放到原arr中}

}

8, Count Sort

原数组A={3, 5, 6, 3, 3, 5};

统计组B={0, 0, 0, 3, 0, 2, 1}; => {0, 0, 0, 0, 3, 3, 3, 5};  //数组B的长度是k, B[A[i]]++; for(i:k){oldCount = B[i]; B[i]=total; total+=oldCount;}

排序组C={3, 3, 3, 5, 5, 6}       //C[B[A[i]]=A[i]; B[A[i]]++;

Count Sort对于数组的元素值范围比较小,频率比较大的情况有效。时间和空间复杂度都是O(n+k)。和Radix sort一样,都不是比较排序。计数排序是用来排序0到100之间的数字的最好的算法。

9, Arrays.sort()

用的是Dual-pivot quick sort, average and worst time complexity are O(nlogn), worst我不确定,document里说的是对于多数情况下比quick sort要好。

Collections.sort(List<T> list); 的不同之处在于它是sort List, 据说内部是将list转成array用同样的排序算法,complexity is also O(nlogn).

Linked List

先构建一个Node:

class Node{

  int num;

  Node next;

  public Node(int n) { num = n; next = null;}

}

基本的构建一个singly linked list:

public static void main(String[] args) {
  Node top = null, node = null, last = null;
  System.out.printf("Enter some integers ending with 0\n");
  Scanner in = new Scanner(System.in);
  int input = in.nextInt();
  while(input != 0) {
    top = addInPlace(top, input);  
    input = in.nextInt();
  }
  print(top);
}

构建sorted linked list的方法:

public static Node addInPlace(Node top, int input) {
  Node prev = null;
  Node curr = top;
  Node node = new Node(input);
  while(curr != null && input > curr.num) {
    prev = curr;
    curr = curr.next;
  }
  node.next = curr;
  if(prev == null) return node;
  prev.next = node;
  return top;
}

Insert node into a circular sorted linked list的方法:

public static void insertIntoCircularList(Node top, int key) {
  Node curr = top;
  Node prev = null;
  Node node = new Node(key);
  do{
    prev = curr;
    curr = curr.next;
    if(key > prev.num && key <curr.num) break;
    if(key < prev.num && key <curr.num) break;
  }while(curr!= top);
  prev.next = node;
  node.next = curr;
}

反转singly linked list的方法:

private static Node reverseList(Node top) {
  Node p1 = null;
  Node p2 = top;
  Node p3 = null;
  while(p2 != null) {
    p3 = p2.next;
    p2.next = p1;
    p1 = p2;
    p2 = p3;
  }
  return p1;
}

asd

[Algorithm Basics] Sorting, LinkedList的更多相关文章

  1. [Algorithm Basics] Search

    1, Binary Search On sorted array! public static int binarySearch(int e, int[] array, int low, int hi ...

  2. Lazarus教程 中文版后续给出

    市面上有介绍Delphi的书籍(近来Delphi的书也是越来越少了),但没有一本系统的介绍Lazarus的书,这本书是网上的仅有的一本Lazarus教程,目前全部是英文,不过我已经着手开始翻译,争取尽 ...

  3. Data-Structure-Notes

    Data Structure Notes Chapter-1 Sorting Algorithm Selection Sorting: /* * Selection Sort */ template& ...

  4. Introduction to Machine Learning

    Chapter 1 Introduction 1.1 What Is Machine Learning? To solve a problem on a computer, we need an al ...

  5. 谷歌技术面试要点(Google面试)(14年5月20日交大专场)

    技术面试的主题 1.简要自我介绍: 姓名.学校.专业 做过的项目与实习 个人主要成就 2.技术评估: 构建与开发算法 编程 计算机基础知识 数据结构 现实世界问题解决能力 设计问题(主要针对博士生) ...

  6. 谷歌的java文本差异对比工具

    package com.huawei.common.transfertool.utils; /** * @author Paul * @version 0.1 * @date 2018/12/11 * ...

  7. 快速排序—三路快排 vs 双基准

    快速排序被公认为是本世纪最重要的算法之一,这已经不是什么新闻了.对很多语言来说是实际系统排序,包括在Java中的Arrays.sort. 那么快速排序有什么新进展呢? 好吧,就像我刚才提到的那样(Ja ...

  8. R语言 arules包 apriori()函数中文帮助文档(中英文对照)

    apriori(arules) apriori()所属R语言包:arules                                         Mining Associations w ...

  9. 结对编程作业(java)

    结对对象:许峰铭 一.Github项目地址:https://github.com/Leungdc/Leungdc/tree/master/%E5%9B%9B%E5%88%99%E8%BF%90%E7% ...

随机推荐

  1. Animation小问题整理

    1.在动画播放中改变层级内容的名字,不会造成动画内容映射的改变. 2.Animator动画剪辑层级没问题,但是不播放 检查是否Mask损坏,FBX文件的Animations选项下面有个Mask.Uni ...

  2. async和await浅析

    要理解async和await的用法,首先要了解Task相关知识,这里不做说明,因为这不是本文的重点. 如果你已经对Task很了解,那么如何使用async和await,在此主要总结了以下三点: 只有在a ...

  3. How to generate a random number in R

    Generate a random number between 5.0 and 7.5x1 <- runif(1, 5.0, 7.5) # 参数1表示产生一个随机数x2 <- runif ...

  4. JSP 基础语法

    1.JSP简介 含义:是运行在服务器端的java页面,是动态网页技术,开发时采用html嵌套java代码的方式实现的 JSP的执行流程是什么? 翻译阶段:web服务器收到jsp请求时,把jsp文件翻译 ...

  5. 怎样让webservice在浏览器远程浏览时像在本地浏览一样有参数输入框

    从远程客户端访问服务器上的WebService能够显示,但点击调用相关的方法时显示“只能用于来自本地计算机的请求”,这时提醒我们还需要在服务器进行相关的配置才能让其他机器正常访问该WebService ...

  6. 一步一步学习underscore的封装和扩展方式

    前言 underscore虽然有点过时,这些年要慢慢被Lodash给淘汰或合并. 但通过看它的源码,还是能学到一个库的封装和扩展方式. 第一步,不污染全局环境. ES5中的JS作用域是函数作用域. 函 ...

  7. docker图形界面工具

    好久不打开docker运行环境,执行命令出现下面的情况 bgddeMBP:~ bgd$ sudo docker info Cannot connect to the Docker daemon. Is ...

  8. 《BI项目笔记》创建计算成员

    也可以利用脚本来生成计算成员

  9. Scrum Meeting 3-20151203

    任务安排 姓名 今日任务 明日任务 困难 董元财 请假(明天是编译截至最后一天) 学习上拉加入新的listview 无 胡亚坤 请假(明天是编译截至最后一天) 设计优化聊天页面 无 刘猛 请假(明天是 ...

  10. 7 -- Spring的基本用法 -- 7...

    7.7 创建Bean的3种方式 ① 调用构造器创建Bean. ② 调用静态工厂方法创建Bean. ③ 调用实例工厂方法创建Bean. 7.7.1 使用构造器创建Bean实例. 使用构造器来创建Bean ...