Skip to content

Commit

Permalink
Add bubble, insertion and selection sort algos
Browse files Browse the repository at this point in the history
Signed-off-by: Darko Draskovic <[email protected]>
  • Loading branch information
darkodraskovic committed May 13, 2022
1 parent 6feeacc commit b1bb009
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
"@babel/preset-env": "^7.12.11",
"jasmine": "^4.1.0",
"nodemon": "^2.0.15"
},
"dependencies": {
"@types/jasmine": "^4.0.3"
}
}
}
17 changes: 17 additions & 0 deletions spec/sort.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
bubbleSort, selectionSort, insertionSort
} from '../src/algorithms/sort.js'

describe("Function", function () {
const createNumbers = () => [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0];
const sorted = [0, 1, 2, 4, 5, 6, 44, 63, 86, 99, 283];

let sortFuncs = [bubbleSort, selectionSort, insertionSort];
sortFuncs.forEach(func => {
it(func.name + " which sorts numbers", function () {
let numbers = createNumbers();
func(numbers);
expect(numbers).toEqual(sorted);
});
});
});
83 changes: 83 additions & 0 deletions src/algorithms/sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const numbers = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0];

function swapArrayElements(array, i1, i2) {
let tmp = array[i1];
array[i1] = array[i2];
array[i2] = tmp;
}

export function bubbleSort(array) {
for (let i = 0; i < array.length - 1; i++) {
for (let j = 1; j < array.length - i; j++) {
if (array[j - 1] > array[j]) {
swapArrayElements(array, j - 1, j);
}
}
}
}

export function selectionSort(array) {
for (let i = 0; i < array.length - 1; i++) {
let smallestIdx = i;
for (let j = i + 1; j < array.length; j++) {
if (array[smallestIdx] > array[j]) {
smallestIdx = j;
}
}
swapArrayElements(array, i, smallestIdx);
}
}

function findGreater(array, end) {
for (let i = 0; i < end; i++) {
if (array[i] > array[end]) {
return i;
}
}
return end;
}
function shiftRight(array, start, end) {
for (let i = end; i > start; i--) {
array[i] = array[i - 1];
}
}

export function insertionSort(array) {
for (let i = 1; i < array.length; i++) {
if (array[i] < array[i - 1]) {
let tmp = array[i];
let greater = findGreater(array, i);
shiftRight(array, greater, i);
array[greater] = tmp;
}
}
}

function mergeSort(array) {
if (array.length === 1) {
return array
}

// Split Array in into right and left
const half = Math.ceil(list.length / 2);
let left = array.slice(0, half)
let right = array.slice(-half)

return merge(
mergeSort(left),
mergeSort(right)
)
}

function merge(left, right) {
// TODO:
}


// bubbleSort(numbers);
// selectionSort(numbers);
// insertionSort(numbers);
// console.log(numbers);

// const answer = mergeSort(numbers);
// console.log(answer);

0 comments on commit b1bb009

Please sign in to comment.