# Sorting
# What is Sorting?
排序(Sorting)是將一串資料依照特定順序方式進行重新排列。我們經常需要將資料進行排序:
- 將數字從小排列到大
- 將名稱按照字母順序排列
- 將電影依照上映年份進行排序
# Bubble Sort
# Implementation
- Start looping from with a variable called
ithe end of the array towards the beginning - Start an inner loop with a variable called
jfromthe beginning untili - 1 - If
arr[j]is greater thanarr[j+1], swap those two values - Return the sorted array
function bubbleSort(arr) {
const swap = (arr, idx1, idx2) => {
[arr[idx1], arr[idx2] = arr[idx2], arr[idx1]];
}
for (let i = arr.length; i > 0; i--) {
for (let j = 0; j < i - 1; j ++) [
if (arr[j] > arr[j + 1]) swap(arr, j, j + 1);
]
}
return arr;
}
# Optimization
function bubbleSort(arr) {
const swap = (arr, idx1, idx2) => {
[arr[idx1], arr[idx2] = arr[idx2], arr[idx1]];
}
for (let i = arr.length; i > 0; i--) {
let noSwaps = true;
for (left j = 0; j < i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
noSwaps = false;
}
}
if (noSwaps) break;
}
}
# Summary
# Selection Sort
# Introduction

選擇排序(selection sort) 會遍歷未排序數組,在其中找到最小(或最大元素),然後存放到排序數組中的位置,並重複此過程,直到所有元素都排序完畢。
# Implementation
- 儲存當前位置的值並標記為最小值(最大值)
- 在未排序數組中,逐一比較並更新最小值(最大值)
- 將找到的最小值(最大值)與當前位置進行交換
- 重複上述過程直到排序完成
function selectionSort(arr) {
const swap = (arr, idx1, idx2) => ([arr[idx1], arr[idx2]] = [arr[idx2], arr[idx1]]);
for (let i = 0; i < arr.length; i++) {
let min = i;
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[min]) min = j;
}
if (i !== min) swap(arr, i, min);
}
return arr;
}
# Big O Complexity
- 最佳時間複雜度
- 平均時間複雜度
- 最糟時間複雜度
- 空間複雜度
- 不穩定排序
# Insertion Sort
# Implementation
- Start by picking the second element in the array.
- Now compare the second element with the one before it and swap if necessary
- Continue to the next element and if it is in the incorrect order, iterate through the sorted portion (i.ie. the left side) to place the element in the correct place
- Repeat until the array is sorted
function insertionSort(arr) {
for (let i = 1; i < arr.length; i++) {
let curr = arr[i];
for (let j = i - 1; j >= 0 && arr[j] > curr; j--) {
arr[j + 1] = arr[j];
}
arr[j + 1] = curr;
}
return arr;
}
insertionSort([1, 2, 9, 76, 4])