Compare commits

...

No commits in common. "master" and "rewrite" have entirely different histories.

34 changed files with 4767 additions and 628 deletions

13
.eslintignore Normal file
View File

@ -0,0 +1,13 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example
# Ignore files for PNPM, NPM and YARN
pnpm-lock.yaml
package-lock.json
yarn.lock

15
.eslintrc.cjs Normal file
View File

@ -0,0 +1,15 @@
/** @type { import("eslint").Linter.Config } */
module.exports = {
root: true,
extends: ['eslint:recommended', 'plugin:svelte/recommended', 'prettier'],
parserOptions: {
sourceType: 'module',
ecmaVersion: 2020,
extraFileExtensions: ['.svelte']
},
env: {
browser: true,
es2017: true,
node: true
}
};

12
.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
*.example.js

1
.npmrc Normal file
View File

@ -0,0 +1 @@
engine-strict=true

4
.prettierignore Normal file
View File

@ -0,0 +1,4 @@
# Ignore files for PNPM, NPM and YARN
pnpm-lock.yaml
package-lock.json
yarn.lock

8
.prettierrc Normal file
View File

@ -0,0 +1,8 @@
{
"useTabs": true,
"singleQuote": true,
"trailingComma": "none",
"printWidth": 100,
"plugins": ["prettier-plugin-svelte"],
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
}

View File

@ -1,81 +0,0 @@
final int amount = 250;
final float timeout = 2.5;
ArrayList<Integer> bars;
float barWidth;
int current = 0;
int distance = -1;
boolean complete = false;
int sortedFrameCountCapture = -1;
float tenPercentHeight;
float ninetyPercentHeight;
void setup() {
fullScreen(P3D);
// size(1280, 720, P3D);
distance = amount-1;
tenPercentHeight = height * 0.1;
ninetyPercentHeight = height * 0.9;
bars = new ArrayList<Integer>();
for (int i = 0; i < amount; i++) {
bars.add(floor(random(tenPercentHeight, ninetyPercentHeight)));
}
barWidth = (float)width / amount;
frameRate(250);
}
void draw() {
background(0);
stroke(0);
for (int i = 0; i < bars.size(); i++) {
if (i == current && !complete) {
fill(255,0,0);
} else if (i == current + distance && !complete) {
fill(0,255,0);
} else {
fill(255);
}
rect(i * barWidth, height-bars.get(i), barWidth, bars.get(i));
}
fill(255);
stroke(255);
textAlign(LEFT, TOP);
textSize(width*0.022);
text("Bar Amount: "+amount+"; FPS: "+round(frameRate)+";", 0, 0);
if (!complete && distance > 0) {
final int tempValue = bars.get(current);
if (tempValue > bars.get(current+distance)) {
bars.set(current, bars.get(current+distance));
bars.set(current+distance, tempValue);
}
current++;
if (current+distance == amount) {
current = 0;
distance--;
if (distance == 0) {
complete = true;
sortedFrameCountCapture = frameCount;
}
}
} else if (sortedFrameCountCapture != -1 && frameCount-sortedFrameCountCapture >= amount*timeout) {
sortedFrameCountCapture = -1;
current = 0;
complete = false;
distance = amount-1;
bars = new ArrayList<Integer>();
for (int i = 0; i < amount; i++) {
bars.add(floor(random(tenPercentHeight, ninetyPercentHeight)));
}
barWidth = (float)width / amount;
// frameRate(amount);
}
}

View File

@ -1,86 +0,0 @@
final int amount = 250;
final float timeout = 2.5;
ArrayList<Integer> bars;
float barWidth;
int current = 1;
int compare = 1;
boolean complete = false, hasSwitched = false;
int sortedFrameCountCapture = -1;
float tenPercentHeight;
float ninetyPercentHeight;
void setup() {
// fullScreen();
size(1280, 720, P3D);
tenPercentHeight = height * 0.1;
ninetyPercentHeight = height * 0.9;
bars = new ArrayList<Integer>();
for (int i = 0; i < amount; i++) {
bars.add(floor(random(tenPercentHeight, ninetyPercentHeight)));
}
barWidth = (float)width / amount;
frameRate(1000);
}
void draw() {
background(0);
stroke(0);
for (int i = 0; i < bars.size(); i++) {
if (i == current && !complete) {
fill(255,0,0);
} else if (i == compare && !complete) {
fill(0,255,0);
} else {
fill(255);
}
rect(i * barWidth, height-bars.get(i), barWidth, bars.get(i));
}
fill(255);
stroke(255);
textAlign(LEFT, TOP);
textSize(width*0.022);
text("Bar Amount: "+amount+"; FPS: "+round(frameRate)+";", 0, 0);
if (!complete) {
boolean noSwap = false;
if (bars.get(compare) < bars.get(compare - 1)) {
hasSwitched = true;
final int temporaryValue = bars.get(compare);
bars.set(compare, bars.get(compare - 1));
bars.set(compare - 1, temporaryValue);
} else {
noSwap = true;
}
if (compare <= 1 || noSwap) {
current++;
compare = current;
if (current == bars.size()) {
complete = true;
sortedFrameCountCapture = frameCount;
}
} else {
compare--;
}
} else if (sortedFrameCountCapture != -1 && frameCount-sortedFrameCountCapture >= amount*timeout) {
current = 1;
compare = 1;
complete = false;
hasSwitched = false;
bars = new ArrayList<Integer>();
for (int i = 0; i < amount; i++) {
bars.add(floor(random(tenPercentHeight, ninetyPercentHeight)));
}
barWidth = (float)width / amount;
// frameRate(amount);
}
}

View File

@ -1,160 +0,0 @@
final float timeout = 2;
ArrayList<Integer> bars = new ArrayList<Integer>();
ArrayList<Job> queue = new ArrayList<Job>();
int mQL = 1, aQL = 0, amount = 100;
float barWidth = 10f;
int sortedFrameCountCapture = -1;
void init() {
amount = floor(random(100, 750));
barWidth = (float)width / amount;
bars = new ArrayList<Integer>();
for (int i = 0; i < amount; i++) {
// bars.add(round(map(i,0,amount-1,0,width/2)));
// bars.add(round(random(width/2)));
bars.add(round(random(height * 0.1, height * 0.9)));
}
// int counter = 0;
// while (counter < 2*amount) {
// int i = floor(random(bars.size()));
// int j = floor(random(bars.size()));
// int t = bars.get(i);
// bars.set(i, bars.get(j));
// bars.set(j, t);
// counter++;
// }
}
void setup() {
fullScreen(P2D);
// size(1280, 720);
init();
queue.add(new Job(0, amount-1));
frameRate(amount);
}
void draw() {
background(0);
noStroke();
stroke(0);
for (int i = 0; i < bars.size(); i++) {
// stroke(255);
fill(255);
for (int j = 0; j < queue.size(); j++) {
Job job = queue.get(j);
if (i == job.rightIndex || i == job.leftIndex) {
// stroke(255, 0, 0);
fill(255, 0, 0);
} else if (i == job.i) {
// stroke(0, 255, 0);
fill(0, 255, 0);
} else if (i == job.j) {
// stroke(0, 0, 255);
fill(0, 0, 255);
}
}
// line(i, height, i, height-bars.get(i));
rect(i * barWidth, height, barWidth, -bars.get(i));
// ellipse(i * barWidth + barWidth / 2, height - bars.get(i), barWidth, barWidth);
// final float a = map(i, 0, bars.size()-1, 0, 12*TWO_PI);
// final float r = bars.get(i);
// final float x = width/2 + cos(a) * r;
// final float y = height/2 + sin(a) * r;
// ellipse(x, y, barWidth, barWidth);
}
/*if (queue.size() > 0) {
queue.get(0).sortStep();
}*/
for (int i = 0; i < queue.size(); i++) {
queue.get(i).sortStep();
}
if (queue.size() == 0) {
if (sortedFrameCountCapture == -1) {
sortedFrameCountCapture = frameCount;
} else if (frameCount-sortedFrameCountCapture>=round(frameRate*timeout)) {
sortedFrameCountCapture = -1;
init();
queue.add(new Job(0, amount-1));
mQL = 1;
aQL = 0;
frameRate(amount);
}
}
mQL = max(queue.size(), mQL);
fill(255,0,0);
stroke(255,0,0);
textAlign(LEFT, TOP);
textSize(width*0.022);
text("Queue Length: "+queue.size()+"; Max. Length: "+mQL+"; Absolute Length: "+aQL+"; Bar Amount: "+bars.size()+"; FPS: "+round(frameRate)+";", 0, 0);
}
class Job {
int leftIndex, rightIndex;
int i, j, pivotValue;
boolean iIndex, jIndex;
Job(int start, int end) {
i = start;
j = end - 1;
rightIndex = end;
leftIndex = start;
pivotValue = bars.get(rightIndex);
}
void sortStep() {
checkIndecies();
if (i < j || iIndex || jIndex) {
if (iIndex) {
i++;
} else if (jIndex) {
j--;
} else if (i < j) {
final int tV1 = bars.get(i);
bars.set(i, bars.get(j));
bars.set(j, tV1);
}
} else {
// Reverse here for reverse sorting-order
if (bars.get(i) > pivotValue) {
final int tV2 = bars.get(i);
bars.set(i, bars.get(rightIndex));
bars.set(rightIndex, tV2);
}
if (isInBounds(i-1) && (leftIndex < i-1)) {
queue.add(new Job(leftIndex, i-1));
}
if (isInBounds(i+1) && (i+1 < rightIndex)) {
queue.add(new Job(i+1, rightIndex));
}
aQL++;
queue.remove(this);
}
}
void checkIndecies() {
// Reverse here for reverse sorting-order
iIndex = (i < rightIndex) && (bars.get(i) < pivotValue);
jIndex = (j > leftIndex) && (bars.get(j) >= pivotValue);
}
boolean isEqual(Job other) {
return ((other.leftIndex == leftIndex) && (other.rightIndex == rightIndex));
}
}
boolean isInBounds(int index) {
return ((index >= 0) && (index < bars.size()));
}

38
README.md Normal file
View File

@ -0,0 +1,38 @@
# create-svelte
Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/main/packages/create-svelte).
## Creating a project
If you're seeing this, you've probably already done this step. Congrats!
```bash
# create a new project in the current directory
npm create svelte@latest
# create a new project in my-app
npm create svelte@latest my-app
```
## Developing
Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
```bash
npm run dev
# or start the server and open the app in a new browser tab
npm run dev -- --open
```
## Building
To create a production version of your app:
```bash
npm run build
```
You can preview the production build with `npm run preview`.
> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment.

View File

@ -1,93 +0,0 @@
// final int FPS = 1000;
final float timeout = 2.5;
ArrayList<Integer> bars;
float barWidth;
int amount;
int current = 0, compare = 1;
boolean complete = false;
int recordIndex = 0;
float tenPercentHeight;
float ninetyPercentHeight;
int sortedFrameCountCapture = -1;
void setup() {
fullScreen(P3D);
// size(1280, 720, P3D);
tenPercentHeight = height * 0.1;
ninetyPercentHeight = height * 0.9;
amount = floor(random(100, 500));
bars = new ArrayList<Integer>();
for (int i = 0; i < amount; i++) {
bars.add(floor(random(tenPercentHeight, ninetyPercentHeight)));
}
barWidth = (float)width / amount;
// frameRate(amount);
frameRate(1000);
}
void draw() {
background(0);
stroke(0);
for (int i = 0; i < bars.size(); i++) {
if (i == current && !complete) {
fill(255,0,0);
} else if (i == compare && !complete) {
fill(0,255,0);
} else if (i == recordIndex && !complete) {
fill(0,0,255);
} else {
fill(255);
}
rect(i * barWidth, height-bars.get(i), barWidth, bars.get(i));
}
fill(255);
stroke(255);
textAlign(LEFT, TOP);
textSize(width*0.022);
text("Bar Amount: "+amount+"; FPS: "+round(frameRate)+";", 0, 0);
if (!complete) {
if (bars.get(compare) < bars.get(recordIndex)) {
recordIndex = compare;
}
compare++;
if (compare == bars.size()) {
final int tempValue = bars.get(current);
if (bars.get(recordIndex) < tempValue) {
bars.set(current, bars.get(recordIndex));
bars.set(recordIndex, tempValue);
}
current++;
compare = current+1;
recordIndex = current;
if (current == bars.size()-1) {
complete = true;
sortedFrameCountCapture = frameCount;
}
}
} else if (sortedFrameCountCapture != -1 && frameCount-sortedFrameCountCapture >= amount*timeout) {
sortedFrameCountCapture = -1;
current = 0;
compare = 1;
complete = false;
amount = floor(random(100, 500));
bars = new ArrayList<Integer>();
for (int i = 0; i < amount; i++) {
bars.add(floor(random(tenPercentHeight, ninetyPercentHeight)));
}
barWidth = (float)width / amount;
// frameRate(amount);
frameRate(1000);
}
}

View File

@ -1,117 +0,0 @@
final float timeout = 2.5;
final int aMin = 100;
final int aMax = 500;
final int repeats = 4;
ArrayList<Integer> bars;
float barWidth;
int amount;
int sortOnLeft = 0, sortOnRight = 0;
int current = 1;
boolean complete = false;
boolean direction = true;
boolean hasSwitched = false;
float tenPercentHeight;
float ninetyPercentHeight;
int sortedFrameCountCapture = -1;
void setup() {
fullScreen(P3D);
// size(1280, 720, P3D);
tenPercentHeight = height * 0.1;
ninetyPercentHeight = height * 0.9;
amount = floor(random(aMin, aMax));
bars = new ArrayList<Integer>();
for (int i = 0; i < amount; i++) {
bars.add(floor(random(tenPercentHeight, ninetyPercentHeight)));
}
barWidth = (float)width / amount;
// frameRate(amount);
frameRate(1000);
}
void draw() {
background(0);
stroke(0);
for (int i = 0; i < bars.size(); i++) {
if (i == sortOnLeft && !complete) {
fill(255, 0, 0);
} else if (i == amount-1-sortOnRight && !complete) {
fill(255, 0, 0);
} else if (i == current && !complete) {
fill(0, 255, 0);
} else {
fill(255);
}
rect(i * barWidth, height-bars.get(i), barWidth, bars.get(i));
}
fill(255);
stroke(255);
textAlign(LEFT, TOP);
textSize(width*0.022);
text("Bar Amount: "+amount+"; FPS: "+round(frameRate)+";", 0, 0);
for (int j = 0; j < repeats; j++) {
if (!complete) {
final int tempValue = bars.get(current);
if (tempValue < bars.get(current-1)) {
bars.set(current, bars.get(current-1));
bars.set(current-1, tempValue);
hasSwitched = true;
}
if (current == amount-1-sortOnRight && direction) {
direction = !direction;
sortOnRight++;
if (hasSwitched == false) {
complete = true;
}
hasSwitched = false;
} else if (current == sortOnLeft+1 && !direction) {
direction = !direction;
sortOnLeft++;
if (hasSwitched == false) {
complete = true;
}
hasSwitched = false;
} else {
if (direction) {
current++;
} else {
current--;
}
}
if (sortOnLeft+1 == amount-1-sortOnRight) {
complete = true;
}
if (complete) {
sortedFrameCountCapture = frameCount;
}
} else if (sortedFrameCountCapture != -1 && frameCount-sortedFrameCountCapture >= amount*timeout) {
sortedFrameCountCapture = -1;
current = 1;
complete = false;
sortOnLeft = 0;
sortOnRight = 0;
direction = true;
hasSwitched = false;
amount = floor(random(aMin, aMax));
bars = new ArrayList<Integer>();
for (int i = 0; i < amount; i++) {
bars.add(floor(random(tenPercentHeight, ninetyPercentHeight)));
}
barWidth = (float)width / amount;
// frameRate(amount);
}
}
}

View File

@ -1,91 +0,0 @@
final int amount = 20;
final float twoThirds = (2/3f);
ArrayList<Integer> bars = new ArrayList<Integer>();
boolean finished = false;
Job stoogeSort;
float barWidth;
void setup() {
fullScreen();
frameRate(144);
textAlign(LEFT, TOP);
textSize(32);
barWidth = (float)width / amount;
for (int i = 0; i < amount; i++)
bars.add(round(random(height * 0.1, height * 0.9)));
stoogeSort = new Job(0, bars.size()-1);
}
void draw() {
background(0);
for (int i = 0; i < amount; i++) {
final int barHeight = bars.get(i);
fill(255);
if (stoogeSort.contains(i) && !finished)
fill(0,255,0);
rect(i * barWidth, height - barHeight, barWidth, barHeight);
}
fill(255);
text("Bar Amount: "+amount+"; Queue Length: "+stoogeSort.getLength()+"; FPS: "+round(frameRate), 0, 0);
if (!finished)
finished = stoogeSort.work();
}
class Job {
int section1end, section2start, start, end;
ArrayList<Job> children = new ArrayList<Job>();
Job(int s, int e) {
start = s;
end = e;
if (!(s+1 == e)) {
final int k = floor((end-start+1)/3f);
children.add(new Job(start, end-k));
children.add(new Job(start+k, end));
children.add(new Job(start, end-k));
}
}
boolean work() {
final boolean check = children.size() > 0;
if (check) {
final boolean fin = children.get(0).work();
if (fin)
children.remove(0);
if (children.size() == 0)
return true;
} else {
final int st = bars.get(start);
final int et = bars.get(end);
if (st > et) {
bars.set(start, et);
bars.set(end, st);
}
}
return !check;
}
boolean contains(int i) {
if (children.size() > 0)
return children.get(0).contains(i);
return start == i || end == i;
}
int getLength() {
if (children.size() > 0) {
int sum = 0;
for (Job c : children)
sum += c.getLength();
return sum;
}
return 1;
}
}

19
jsconfig.json Normal file
View File

@ -0,0 +1,19 @@
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"moduleResolution": "bundler"
}
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias
// except $lib which is handled by https://kit.svelte.dev/docs/configuration#files
//
// If you want to overwrite includes/excludes, make sure to copy over the relevant includes/excludes
// from the referenced tsconfig.json - TypeScript does not merge them in
}

3991
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

33
package.json Normal file
View File

@ -0,0 +1,33 @@
{
"name": "sorting",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "vite dev",
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json --watch",
"lint": "prettier --check . && eslint .",
"format": "prettier --write ."
},
"devDependencies": {
"@sveltejs/adapter-auto": "^3.0.0",
"@sveltejs/kit": "^2.0.0",
"@sveltejs/vite-plugin-svelte": "^3.0.0",
"@types/eslint": "^8.56.0",
"autoprefixer": "^10.4.19",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-svelte": "^2.35.1",
"postcss": "^8.4.38",
"prettier": "^3.1.1",
"prettier-plugin-svelte": "^3.1.2",
"svelte": "^4.2.7",
"svelte-check": "^3.6.0",
"tailwindcss": "^3.4.3",
"typescript": "^5.0.0",
"vite": "^5.0.3"
},
"type": "module"
}

6
postcss.config.js Normal file
View File

@ -0,0 +1,6 @@
export default {
plugins: {
tailwindcss: {},
autoprefixer: {}
}
};

3
src/app.css Normal file
View File

@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

13
src/app.d.ts vendored Normal file
View File

@ -0,0 +1,13 @@
// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
declare global {
namespace App {
// interface Error {}
// interface Locals {}
// interface PageData {}
// interface PageState {}
// interface Platform {}
}
}
export {};

12
src/app.html Normal file
View File

@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
</body>
</html>

16
src/lib/createData.js Normal file
View File

@ -0,0 +1,16 @@
/**
* Create an array of locally unique numbers in random order
* @param {number} length
* @returns {number[]}
*/
export function createData(length) {
// Initialize array with indicies
const arr = Array.from({ length }, (_, i) => i);
// FisherYates shuffle to randomize
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
// Swap elements in-place
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}

8
src/lib/scaleData.js Normal file
View File

@ -0,0 +1,8 @@
/**
* Scale an array of numbers down to usable percentages made for this project
* @param {number[]} data
* @returns {number[]}
*/
export function scaleData(data) {
return data.map((d) => (d * 0.9) / (data.length - 1) + 0.05);
}

View File

@ -0,0 +1,5 @@
<script>
import '../app.css';
</script>
<slot />

33
src/routes/+page.svelte Normal file
View File

@ -0,0 +1,33 @@
<h1 class="text-4xl font-bold m-5 text-center">Choose an Algorithm</h1>
<div class="grid grid-rows-4 m-1 text-center">
<h2 class="grid grid-cols-1 font-bold text-xl">
<a
class="border-2 border-black border-b-0 underline text-blue-600 hover:bg-gray-200 cursor-pointer"
href="/bubble">Bubble sort</a
>
</h2>
<h2 class="grid grid-cols-1 font-bold text-xl">
<a
class="border-2 border-black border-b-0 underline text-blue-600 hover:bg-gray-200 cursor-pointer"
href="/quick">Quicksort</a
>
</h2>
<h2 class="grid grid-cols-1 font-bold text-xl">
<a
class="border-2 border-black border-b-0 underline text-blue-600 hover:bg-gray-200 cursor-pointer"
href="/heap">Heapsort</a
>
</h2>
<h2 class="grid grid-cols-1 font-bold text-xl">
<a
class="border-2 border-black border-b-0 underline text-blue-600 hover:bg-gray-200 cursor-pointer"
href="/merge">Merge sort</a
>
</h2>
<h2 class="grid grid-cols-1 font-bold text-xl">
<a
class="border-2 border-black underline text-blue-600 hover:bg-gray-200 cursor-pointer"
href="/bogo">Bogosort</a
>
</h2>
</div>

View File

@ -0,0 +1,81 @@
<script lang="js">
import { createData } from '$lib/createData';
import { scaleData } from '$lib/scaleData';
import { error } from '@sveltejs/kit';
import { onMount } from 'svelte';
// Constants / Parameters
const FPS = 20;
const AMOUNT = 100;
// System variables
const data = scaleData(createData(AMOUNT));
/** @type {HTMLCanvasElement} */
let canvas;
/** @type {CanvasRenderingContext2D} */
let context;
/** @type {number} */
let width;
/** @type {number} */
let interv;
onMount(() => {
// Initialize the canvas dimensions
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Try getting 2D context
const ctx = canvas.getContext('2d');
if (!ctx) error(417, 'Canvas not supported');
// Initialize global variables
context = ctx;
width = canvas.width / AMOUNT;
// Start continuous animation
interv = setInterval(animate, 1000 / FPS);
});
let index = 0;
/** Render a single frame of the sorting algorithm at a time on the canvas. */
function animate() {
// Random index after current in Fisher-Yates shuffle style
const compr = Math.floor(Math.random() * (AMOUNT - index)) + index;
// Clear canvas with black background
context.fillStyle = 'black';
context.fillRect(0, 0, canvas.width, canvas.height);
// Display datapoints as lines
context.fillStyle = 'white';
data.forEach((d, i) => {
if (i === index) context.fillStyle = 'red';
else if (i === compr) context.fillStyle = 'lime';
else context.fillStyle = 'white';
context.fillRect(i * width, canvas.height, width - 1, -d * canvas.height);
});
// Swap datapoints
[data[index], data[compr]] = [data[compr], data[index]];
// After last iteration, check order
if (index++ >= data.length - 1) {
let prev = 0,
sorted = true;
for (const d of data) {
if (d < prev) {
sorted = false;
break;
}
prev = d;
}
if (sorted) {
clearInterval(interv);
index = data.length;
} else index = 0;
}
}
</script>
<canvas bind:this={canvas} class="fixed top-0 left-0" />

View File

@ -0,0 +1,67 @@
<script lang="js">
import { createData } from '$lib/createData';
import { scaleData } from '$lib/scaleData';
import { error } from '@sveltejs/kit';
import { onMount } from 'svelte';
// Constants / Parameters
const FPS = 20;
const AMOUNT = 100;
// System variables
const data = scaleData(createData(AMOUNT));
/** @type {HTMLCanvasElement} */
let canvas;
/** @type {CanvasRenderingContext2D} */
let context;
/** @type {number} */
let width;
/** @type {number} */
let interv;
onMount(() => {
// Initialize the canvas dimensions
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Try getting 2D context
const ctx = canvas.getContext('2d');
if (!ctx) error(417, 'Canvas not supported');
// Initialize global variables
context = ctx;
width = canvas.width / AMOUNT;
// Start continuous animation
interv = setInterval(animate, 1000 / FPS);
});
let index = 0,
complete = 0;
/** Render a single frame of the sorting algorithm at a time on the canvas. */
function animate() {
// Clear canvas with black background
context.fillStyle = 'black';
context.fillRect(0, 0, canvas.width, canvas.height);
// Display datapoints as lines
data.forEach((d, i) => {
if (i === index) context.fillStyle = 'red';
else if (i === index + 1) context.fillStyle = 'lime';
else context.fillStyle = 'white';
context.fillRect(i * width, canvas.height, width - 1, -d * canvas.height);
});
// Compare datapoint with neighbor
if (data[index] > data[index + 1])
[data[index], data[index + 1]] = [data[index + 1], data[index]];
// Increment index
if (++index > data.length - complete - 2) {
if (++complete >= AMOUNT) clearInterval(interv);
index = 0;
}
}
</script>
<canvas bind:this={canvas} class="fixed top-0 left-0" />

View File

@ -0,0 +1,52 @@
<script lang="js">
import { createData } from '$lib/createData';
import { scaleData } from '$lib/scaleData';
import { error } from '@sveltejs/kit';
import { onMount } from 'svelte';
// Constants / Parameters
const FPS = 20;
const AMOUNT = 100;
// System variables
const data = scaleData(createData(AMOUNT));
/** @type {HTMLCanvasElement} */
let canvas;
/** @type {CanvasRenderingContext2D} */
let context;
/** @type {number} */
let width;
/** @type {number} */
let interv;
onMount(() => {
// Initialize the canvas dimensions
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Try getting 2D context
const ctx = canvas.getContext('2d');
if (!ctx) error(417, 'Canvas not supported');
// Initialize global variables
context = ctx;
width = canvas.width / AMOUNT;
// Start continuous animation
interv = setInterval(animate, 1000 / FPS);
});
/** Render a single frame of the sorting algorithm at a time on the canvas. */
function animate() {
// Clear canvas with black background
context.fillStyle = 'black';
context.fillRect(0, 0, canvas.width, canvas.height);
// Display datapoints as lines
context.fillStyle = 'white';
data.forEach((d, i) =>
context.fillRect(i * width, canvas.height, width - 1, -d * canvas.height)
);
}
</script>
<canvas bind:this={canvas} class="fixed top-0 left-0" />

View File

@ -0,0 +1,98 @@
<script lang="js">
import { createData } from '$lib/createData';
import { scaleData } from '$lib/scaleData';
import { error } from '@sveltejs/kit';
import { onMount } from 'svelte';
// Constants / Parameters
const FPS = 20;
const AMOUNT = 100;
// System variables
const data = scaleData(createData(AMOUNT));
/** @type {HTMLCanvasElement} */
let canvas;
/** @type {CanvasRenderingContext2D} */
let context;
/** @type {number} */
let width;
/** @type {number} */
let interv;
onMount(() => {
// Initialize the canvas dimensions
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Try getting 2D context
const ctx = canvas.getContext('2d');
if (!ctx) error(417, 'Canvas not supported');
// Initialize global variables
context = ctx;
width = canvas.width / AMOUNT;
// Start continuous animation
interv = setInterval(animate, 1000 / FPS);
queue.push(new ArrRef(0, AMOUNT));
});
class ArrRef {
/**
* @param {number} start
* @param {number} end
*/
constructor(start, end) {
this.start = start;
this.end = end;
this.length = end - start;
}
/**
* @param {number} index
* @returns {number}
*/
at(index) {
return data[this.start + index];
}
}
/**
* @param {ArrRef} a
* @param {ArrRef} b
*/
function merge(a, b) {
let index = 0,
compr = 0;
while (index < a.length && compr < b.length) {
if (a.at(index) < b.at(compr)) {
data[index + compr] = a.at(index);
index++;
} else {
data[index + compr] = b.at(compr);
compr++;
}
}
while (index < a.length) (data[index + compr] = a.at(index)), index++;
while (compr < b.length) (data[index + compr] = b.at(compr)), compr++;
}
/** @type {ArrRef[]} */
const queue = [];
/** Render a single frame of the sorting algorithm at a time on the canvas. */
function animate() {
// Clear canvas with black background
context.fillStyle = 'black';
context.fillRect(0, 0, canvas.width, canvas.height);
// Display datapoints as lines
context.fillStyle = 'white';
data.forEach((d, i) =>
context.fillRect(i * width, canvas.height, width - 1, -d * canvas.height)
);
}
</script>
<canvas bind:this={canvas} class="fixed top-0 left-0" />

View File

@ -0,0 +1,158 @@
<script lang="js">
import { createData } from '$lib/createData';
import { scaleData } from '$lib/scaleData';
import { error } from '@sveltejs/kit';
import { onMount } from 'svelte';
// Constants / Parameters
const FPS = 20;
const AMOUNT = 100;
// System variables
const data = scaleData(createData(AMOUNT));
/** @type {HTMLCanvasElement} */
let canvas;
/** @type {CanvasRenderingContext2D} */
let context;
/** @type {number} */
let width;
/** @type {number} */
let interv;
onMount(() => {
// Initialize the canvas dimensions
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Try getting 2D context
const ctx = canvas.getContext('2d');
if (!ctx) error(417, 'Canvas not supported');
// Initialize global variables
context = ctx;
width = canvas.width / AMOUNT;
// Start continuous animation
interv = setInterval(animate, 1000 / FPS);
// Assign first job over the entire array
queue.push(new Job(data, 0, data.length));
});
/** @type {Job[]} */
let queue = [];
/** Response type for the quicksort algorithm */
class Response {
/**
* @param {boolean} done
* @param {number|null} index
* @param {number|null} start
* @param {number|null} end
*/
constructor(done, index, start, end) {
this.done = done;
this.index = index;
this.start = start;
this.end = end;
}
/**
* Generates follow up jobs from response.
* @param {number[]} array
* @returns {Job[]}
*/
generate(array) {
if (this.done) return [];
return [
new Job(array, this.start ?? 0, this.index ?? 0),
new Job(array, this.index ?? 0 + 1, this.end ?? 0)
];
}
}
/** Represent a call to the quicksort algorithm. */
class Job {
/**
* @param {number[]} array
* @param {number} start
* @param {number} end
*/
constructor(array, start, end) {
this.array = array;
this.start = start;
this.end = end;
this.pivot = array[end - 1];
this.index = start;
this.compr = start;
}
/**
* @returns {Response|void}
*/
step() {
// Base case, end condition
if (this.start >= this.end - 1 || this.start < 0) return new Response(true, null, null, null);
// Comparator reached end of range, swap pivot
if (this.compr >= this.end - 1) {
[this.array[this.index], this.array[this.end - 1]] = [
this.array[this.end - 1],
this.array[this.index]
];
return new Response(false, this.index, this.start, this.end);
}
// Compare with pivot to split range in two
if (this.array[this.compr] <= this.pivot) {
[this.array[this.index], this.array[this.compr]] = [
this.array[this.compr],
this.array[this.index]
];
this.index++;
}
this.compr++;
}
}
/** Render a single frame of the sorting algorithm at a time on the canvas. */
function animate() {
// Clear canvas with black background
context.fillStyle = 'black';
context.fillRect(0, 0, canvas.width, canvas.height);
// Display datapoints as lines
context.fillStyle = 'white';
data.forEach((d, i) => {
let color = 'white';
// Try chosing color based on all job indecies
queue.forEach((q) => {
const { index, compr } = q;
if (i === index) color = 'red';
else if (i === compr) color = 'lime';
});
// Display datapoint with chosen color
context.fillStyle = color;
context.fillRect(i * width, canvas.height, width - 1, -d * canvas.height);
});
/** @type {Job[]} */
const newQueue = [];
// Step job and handle result
queue.forEach((j, i) => {
const result = j.step();
if (result !== undefined) {
queue.splice(i, 1);
if (!result.done) newQueue.push(...result.generate(data));
}
});
// Add new jobs to queue
queue.push(...newQueue);
}
</script>
<canvas bind:this={canvas} class="fixed top-0 left-0" />

View File

@ -0,0 +1,52 @@
<script lang="js">
import { createData } from '$lib/createData';
import { scaleData } from '$lib/scaleData';
import { error } from '@sveltejs/kit';
import { onMount } from 'svelte';
// Constants / Parameters
const FPS = 20;
const AMOUNT = 100;
// System variables
const data = scaleData(createData(AMOUNT));
/** @type {HTMLCanvasElement} */
let canvas;
/** @type {CanvasRenderingContext2D} */
let context;
/** @type {number} */
let width;
/** @type {number} */
let interv;
onMount(() => {
// Initialize the canvas dimensions
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Try getting 2D context
const ctx = canvas.getContext('2d');
if (!ctx) error(417, 'Canvas not supported');
// Initialize global variables
context = ctx;
width = canvas.width / AMOUNT;
// Start continuous animation
interv = setInterval(animate, 1000 / FPS);
});
/** Render a single frame of the sorting algorithm at a time on the canvas. */
function animate() {
// Clear canvas with black background
context.fillStyle = 'black';
context.fillRect(0, 0, canvas.width, canvas.height);
// Display datapoints as lines
context.fillStyle = 'white';
data.forEach((d, i) =>
context.fillRect(i * width, canvas.height, width - 1, -d * canvas.height)
);
}
</script>
<canvas bind:this={canvas} class="fixed top-0 left-0" />

BIN
static/favicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

15
svelte.config.js Normal file
View File

@ -0,0 +1,15 @@
import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
// If your environment is not supported, or you settled on a specific environment, switch out the adapter.
// See https://kit.svelte.dev/docs/adapters for more information about adapters.
adapter: adapter()
},
preprocess: vitePreprocess()
};
export default config;

8
tailwind.config.js Normal file
View File

@ -0,0 +1,8 @@
/** @type {import('tailwindcss').Config} */
export default {
content: ['./src/**/*.{html,js,svelte,ts}'],
theme: {
extend: {}
},
plugins: []
};

6
vite.config.js Normal file
View File

@ -0,0 +1,6 @@
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [sveltekit()]
});