# Basic Algorithims - Sorting

> Time Complexity Examples: This is O(n \ (n - 1) / 2) which is reducible to O(n^2). Nested Loop w/ inner Loop doing j + 1 look ups:

By Maxwell Kendall · 2022-05-27 · Tags: cs-fundamentals
Source: https://www.maxwellkendall.com/blog/basic-algorithims-sorting-notes

Time Complexity Examples:

```javascript
for (var i = 0; i < n; i++) {
  let minIndex = i;
  for (var j = i; j < n; j++) {
    if (unsortedList[j] < unsortedList[minIndex]) {
      minIndex = j;
    }
  }
}
```

This is O(n \* (n - 1) / 2) which is reducible to O(n^2).

Nested Loop w/ inner Loop doing j + 1 look ups:

```javascript
for (var i = n - 1; i >= 0; i--) {
    let swapped = false;
    for (var j = 0; j < i; j++) {
        if (unsortedList[j] > unsortedList[j + 1]) {
            const temp = unsortedList[j];
            // this will always be ok! 👇
            unsortedList[j] = unsortedList[j + 1];
            unsortedList[j + 1] = temp;
            swapped = true;
        }
    }
```
