Skip to content

Commit

Permalink
Restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
lesyk authored Sep 15, 2019
1 parent 9370ab4 commit 70472c8
Showing 1 changed file with 91 additions and 87 deletions.
178 changes: 91 additions & 87 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Toolbox of software data structures, design patterns, algorithms and typical pro

## Sorting

## 🛁 Bubble Sort
### Simple sorts

#### 🛁 Bubble Sort

![Bubble Sort](https://upload.wikimedia.org/wikipedia/commons/c/c8/Bubble-sort-example-300px.gif "Bubble Sort")

Expand Down Expand Up @@ -78,7 +80,7 @@ console.log(bubbleSort(numbers));

</details>

## 🗳️ Insertion Sort
#### 🗳️ Insertion Sort

![Insertion Sort](https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif "Insertion Sort")

Expand Down Expand Up @@ -157,7 +159,93 @@ console.log(insertionSort(unsortedArray));

</details>

## ⚥ Merge Sort
#### 🔘 Selection Sort

![Selection Sort](https://upload.wikimedia.org/wikipedia/commons/9/94/Selection-Sort-Animation.gif "Selection Sort")

<details>

[Wikipedia says](https://en.wikipedia.org/wiki/Selection_sort):
> Selection sort is an in-place comparison sort. It has O(n2) complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations.
> The algorithm finds the minimum value, swaps it with the value in the first position, and repeats these steps for the remainder of the list. It does no more than n swaps, and thus is useful where swapping is very expensive.
| Algorithm | Time Complexity | | | Space Complexity |
| -------------- |----------------:| -------:|----------:|-----------------:|
| | Best | Average | Worst | Worst |
| Selection sort | Ω(n^2) | Θ(n^2) | Θ(n^2) | Θ(1 ) |

### Swift

**Example:**
```swift
func selectionSort(numbers: [Int]) -> [Int] {
var sortedNumbers = numbers

for i in 0..<sortedNumbers.count-1 {
var minIndex = i
for j in i..<sortedNumbers.count {
if sortedNumbers[j] < sortedNumbers[minIndex] {
minIndex = j
}
}

let temp = sortedNumbers[minIndex]
sortedNumbers[minIndex] = sortedNumbers[i]
sortedNumbers[i] = temp
}

return sortedNumbers
}

let numbers = [5, 15, 14, 1, 26, 0, 99]

print(selectionSort(numbers: numbers))
```

#### Output:
```
[0, 1, 5, 14, 15, 26, 99]
```

### TypeScript
**Example:**
[jsfiddle link](https://jsfiddle.net/oe2cm1fn/)

```typescript
function selectionSort(numbers: number[]): number[] {
let sortedNumbers = numbers;

for (let i = 0; i < sortedNumbers.length - 1; i++) {
let minValueIndex = i;

for (let j = i + 1; j < sortedNumbers.length; j++) {
if (sortedNumbers[j] < sortedNumbers[minValueIndex]) {
minValueIndex = j;
}
}
const temp = sortedNumbers[minValueIndex];
sortedNumbers[minValueIndex] = sortedNumbers[i];
sortedNumbers[i] = temp;
}

return sortedNumbers;
}

const unsortedArray = [5, 15, 14, 1, 26, 0, 99];
console.log(selectionSort(unsortedArray));
```

#### Output:
```
[ 0, 1, 5, 14, 15, 26, 99 ]
```

</details>

### Efficient sorts

#### ⚥ Merge Sort

![Merge Sort](https://upload.wikimedia.org/wikipedia/commons/c/cc/Merge-sort-example-300px.gif "Merge Sort")

Expand Down Expand Up @@ -307,90 +395,6 @@ console.log(mergeSort(unsortedArrayOfNumbers));

</details>

## 🔘 Selection Sort

![Selection Sort](https://upload.wikimedia.org/wikipedia/commons/9/94/Selection-Sort-Animation.gif "Selection Sort")

<details>

[Wikipedia says](https://en.wikipedia.org/wiki/Selection_sort):
> Selection sort is an in-place comparison sort. It has O(n2) complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations.
> The algorithm finds the minimum value, swaps it with the value in the first position, and repeats these steps for the remainder of the list. It does no more than n swaps, and thus is useful where swapping is very expensive.
| Algorithm | Time Complexity | | | Space Complexity |
| -------------- |----------------:| -------:|----------:|-----------------:|
| | Best | Average | Worst | Worst |
| Selection sort | Ω(n^2) | Θ(n^2) | Θ(n^2) | Θ(1 ) |

### Swift

**Example:**
```swift
func selectionSort(numbers: [Int]) -> [Int] {
var sortedNumbers = numbers

for i in 0..<sortedNumbers.count-1 {
var minIndex = i
for j in i..<sortedNumbers.count {
if sortedNumbers[j] < sortedNumbers[minIndex] {
minIndex = j
}
}

let temp = sortedNumbers[minIndex]
sortedNumbers[minIndex] = sortedNumbers[i]
sortedNumbers[i] = temp
}

return sortedNumbers
}

let numbers = [5, 15, 14, 1, 26, 0, 99]

print(selectionSort(numbers: numbers))
```

#### Output:
```
[0, 1, 5, 14, 15, 26, 99]
```

### TypeScript
**Example:**
[jsfiddle link](https://jsfiddle.net/oe2cm1fn/)

```typescript
function selectionSort(numbers: number[]): number[] {
let sortedNumbers = numbers;

for (let i = 0; i < sortedNumbers.length - 1; i++) {
let minValueIndex = i;

for (let j = i + 1; j < sortedNumbers.length; j++) {
if (sortedNumbers[j] < sortedNumbers[minValueIndex]) {
minValueIndex = j;
}
}
const temp = sortedNumbers[minValueIndex];
sortedNumbers[minValueIndex] = sortedNumbers[i];
sortedNumbers[i] = temp;
}

return sortedNumbers;
}

const unsortedArray = [5, 15, 14, 1, 26, 0, 99];
console.log(selectionSort(unsortedArray));
```

#### Output:
```
[ 0, 1, 5, 14, 15, 26, 99 ]
```

</details>


# Data Structures
## 🔗 Linked List
Expand Down

0 comments on commit 70472c8

Please sign in to comment.