11. Sorting

Bubble sort

  1. 2 for loops

  2. every time compare and swap 2 adjacent elements

  3. find the largest element and move to the end

  4. Time: O(N^2) and space: O(1)

Selection Sort

  1. 2 for loops

  2. every loop find the smallest element and move to the beginning

  3. split the arrays into 2 parts, sorted subarray and unsorted subarray

  4. Time: O(N^2) and space: O(1)

Largest Number

  • 给一个int[] nums, 返回一个用nums里的num组成的最大的数

  • custom sort: basically implement a String comparator to decide which String should come first during concatenation

Last updated

Was this helpful?