Skip to content

Commit

Permalink
implement insertion sort
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKondor committed Mar 25, 2024
1 parent 0cbc5df commit 8632433
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
<a href="https://martinkondor.github.io/SortingAlgorithms/"><p align="center"><img src="public/img/SortingAlgorithms.gif" width="550" title="Click to try it out!"></p></a>
<a href="https://martinkondor.github.io/SortingAlgorithms/"><p align="center"><img src="public/img/SortingAlgorithms.gif" width="575" title="Click to try it out!"></p></a>

# 📊 SortingAlgorithms

[![Project Status](https://img.shields.io/badge/status-active-brightgreen.svg)](https://github.com/MartinKondor/SortingAlgorithms/)
[![version](https://img.shields.io/badge/version-21.05-brightgreen.svg)](https://github.com/MartinKondor/SortingAlgorithms/)
[![GitHub Issues](https://img.shields.io/github/issues/MartinKondor/SortingAlgorithms.svg)](https://github.com/MartinKondor/SortingAlgorithms/issues)
![Contributions welcome](https://img.shields.io/badge/contributions-welcome-blue.svg)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)

Sorting algorithms visualized.

## Implemented algorithms
* Bubble sort
* Insertion sort

### TODO
* Insertion sort
* Selection sort
* Merge sort
* Quicksort
Expand Down
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<i class="fas fa-step-forward mr-1"></i>
Forward
</button>
<button id="randomize" class="btn btn-outline-warning">
<button id="randomize" class="btn btn-outline-secondary">
<i class="fas fa-redo mr-1"></i>
Randomize
</button>
Expand All @@ -60,8 +60,8 @@
<select id="algorithm" name="algorithm" class="">
<option value="bubble">Bubble Sort</option>
<option value="insertion">Insertion Sort</option>
<option value="merge">Merge Sort</option>
<!--
<option value="merge">Merge Sort</option>
<option value="quick">Quick Sort</option>
-->
</select>
Expand Down
38 changes: 36 additions & 2 deletions public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,39 @@
}

function insertionSort(arr) {
console.log("insertionSort");
for (let i = 1; i < arr.length; i++) {
let currentValue = arr[i];
let j;
for (j = i - 1; j >= 0 && arr[j] > currentValue; j--) {
arr[j + 1] = arr[j];
}
arr[j + 1] = currentValue;
}
return arr;
}

function mergeSort(arr) {
if (arr.length <= 1) {
return arr;
}
let mid = Math.floor(arr.length / 2);
let left = mergeSort(arr.slice(0, mid));
let right = mergeSort(arr.slice(mid));
return merge(left, right);
}

function merge(left, right) {
let sortedArr = [];

while (left.length && right.length) {
if (left[0] < right[0]) {
sortedArr.push(left.shift());
} else {
sortedArr.push(right.shift());
}
}

return [...sortedArr, ...left, ...right];
}

// Stores the selected sorting agorithm
Expand Down Expand Up @@ -358,7 +390,9 @@
if ($('#algorithm').val() === 'insertion') {
sortingAlgorithm = insertionSort;
}
console.log("ITT VAGYOK");
if ($('#algorithm').val() === 'merge') {
sortingAlgorithm = mergeSort;
}
});

animation.record(function(arr) {
Expand Down

0 comments on commit 8632433

Please sign in to comment.