The term Bubble Sort was first used by Iverson, K in 1962. \
Problem Statement
Given an unsorted array of n elements, write a function to sort the array
Approach
- select the first element of the array
- compare it with its next element
- if it is larger than the next element then swap them
- else do nothing
- keep doing this for every index of the array
- repeat the above process n times.
Time Complexity
O(n^2) Worst case performance
O(n) Best-case performance
O(n^2) Average performanceSpace Complexity
O(1) Worst case
异或运算: 不引入第三个变量交换位置
u = 3
v = 2
二进制异或过程如下
u = u ^ v = 11 ^ 10 = 01
v = u ^ v = 01 ^ 10 = 11 = 3
u = u ^ v = 01 ^ 11 = 10 = 2