-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add bubble, insertion and selection sort algos
Signed-off-by: Darko Draskovic <[email protected]>
- Loading branch information
1 parent
6feeacc
commit b1bb009
Showing
3 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |