Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Node Modules
.node_module

# Visual Studio Code
.vscode
23 changes: 10 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# Sorting Algorithms

## Algorithms & Data structures - using JavaScript

My lab works of Algorithms classes as a student of Computer Science(Internet of Things) program in Lviv Polytechnic National University.

---

1. __Sort algorithms__ and their analysis
2. **Data structures** and their functionality
3. Solve Math **Problem**
4. **Graphs**
5. **More Graphs**: Prim's (also known as Jarník's algorithm) minimum spanning tree, Kruskal's effective minimum spanning tree, Tarjan's strongly connected components
6. Solve **Dynamic Programming** Problem
7. **String search algorithms:** Knuth–Morris–Pratt, Rabin–Karp, Boyer–Moore
### Simple
+ Bubble
+ Insertion
+ Selection

### Advanced
+ Merge
+ Heap
+ Quick
20 changes: 20 additions & 0 deletions bubble/bubblesort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let bubbleSort = (arr) => {
let len = arr.length;
let swapped;

do {
swapped = false;
for (let i = 0; i < len; i++) {
if(arr[i] > arr[i + 1]) {
let tmp = arr[i]; // store var to swap
arr[i] = arr[i + 1]; // swap
arr[i + 1] = tmp; // swap
swapped = true;
}
}
} while(swapped);

return arr;
}

console.log("\nsort result: ", bubbleSort([2, 3, 5, 4, 1]), "\n");
31 changes: 31 additions & 0 deletions heap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Lab-1

---

## Task
Implement a sorting algorithm - HeapSort(ascending/descending)

Visual presentation

![Algorithm's gif](https://upload.wikimedia.org/wikipedia/commons/1/1b/Sorting_heapsort_anim.gif)

---

### Result output
+ Algorithm's name
+ Execution time
+ Counters: swaps, comparisons
+ Sorting result
### Code must be covered with tests
+ sort the input array
+ sort in ascending order of sorted array in ascending order
+ sort in descending order of sorted array in ascending order
+ sort in ascending order of sorted array in descending order
+ sort in descending order of sorted array in descending order

---

## Installation
+ Download the project and open index.html file
+ Click right mouse button and select "Inspect"
+ Print `heapSort(inputArray)` in console and press Enter *(inputArray example: [2, 5, 7, -9, 0])*
111 changes: 111 additions & 0 deletions heap/heapsort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { performance } from 'perf_hooks';

export function heapifyMax(arr, length, i, counter = { comparisons: 0, swaps: 0, executionTime: 0 }) {
let largest = i; //i - index of the parent that we are heapifying
let left = i * 2 + 1;
let right = left + 1;


counter.comparisons++;
if (left < length) {
counter.comparisons++; //for next 'if'
if(arr[left] > arr[largest]) {
largest = left;
}
}

counter.comparisons++;
if (right < length) {
counter.comparisons++; //for next 'if'
if(arr[right] > arr[largest]) {

largest = right;
}
}

if(largest != i) {
counter.swaps++;
[arr[i], arr[largest]] = [arr[largest], arr[i]];
heapifyMax(arr, length, largest, counter);
}
}

export function heapifyMin(arr, length, i, counter = { comparisons: 0, swaps: 0, executionTime: 0 }) {
let largest = i;
let left = i * 2 + 1;
let right = left + 1;


counter.comparisons++;
if (left < length) {
counter.comparisons++; //for next 'if'
if(arr[left] < arr[largest]) {
largest = left;
}
}

counter.comparisons++;
if (right < length) {
counter.comparisons++; //for next 'if'
if(arr[right] < arr[largest]) {

largest = right;
}
}

if(largest != i) {
counter.swaps++;
[arr[i], arr[largest]] = [arr[largest], arr[i]];
heapifyMin(arr, length, largest, counter);
}
}

export function heapSort(arr, sortOrder = "asc") {
const startTime = performance.now();
let length = arr.length;
let i = Math.floor(length / 2 - 1); //last parent node
let k = length - 1; // last child

let counter = {
comparisons: 0,
swaps: 0
}

if(sortOrder === "asc" ) {
while (i >= 0) {
heapifyMax(arr, length, i, counter);
i--;
}

while (k >= 0) {
counter.swaps++;
[arr[0], arr[k]] = [arr[k], arr[0]];
heapifyMax(arr, k, 0, counter);
k--;
}
} else if (sortOrder === "desc") {
while (i >= 0) {
heapifyMin(arr, length, i, counter);
i--;
}

while (k >= 0) {
counter.swaps++;
[arr[0], arr[k]] = [arr[k], arr[0]];
heapifyMin(arr, k, 0, counter);
k--;
}
} else {
console.log("Wrong sort order");
}


const endTime = performance.now();
counter.executionTime = endTime - startTime;
console.log("HeapSort");
console.log("Sorted Array:", ...arr);
console.log("counters:", counter);
return arr;
}

heapSort([5, 3, 4, 3, 1]);
22 changes: 22 additions & 0 deletions insertion/insertionSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// sub-list before key is sorted
//
const insertionSort = (arr) => {
let len = arr.length;

for(let i = 1; i < len; i++) {
let key = arr[i]; // current key
let j = i - 1; // last element of sorted sub-list

while(j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j]; // swap
j = j - 1; // iterate through sub-list
}

arr[j + 1] = key; // next key is right of the biggest value in sub-list
}

return console.log('\nsort result: ', arr, "\n");
}

insertionSort([5, 1, 2, -3, -4, 5, -5, 10, 23, -23]);
27 changes: 27 additions & 0 deletions merge/mergeSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const merge = (left, right) => {
let arr = [];

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

return [...arr, ...left, ...right]
}

const mergeSort = (arr) => {
const half = arr.length / 2;

if(arr.length < 2) {
return arr;
}

const left = arr.splice(0, half);
const right = arr;
return merge(mergeSort(left), mergeSort(right));
}

console.log("\nsort result: ", mergeSort([5, 2, 3, 1, 7, 6]), "\n");
Loading