Last active
April 5, 2023 12:45
-
-
Save 3AHAT0P/9c9ca00bd08385b58eac65e673cf332a to your computer and use it in GitHub Desktop.
Implementation custom sorting algorithm (sorting by copying to new array)
This file contains hidden or 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
const binarySearch = ( | |
array: number[], | |
element: number, | |
range: [left: number, right: number] = [0, array.length - 1], | |
): number => { | |
let left = range[0]; | |
let right = range[1]; | |
let halfIndex = -1; | |
while (left < right) { | |
halfIndex = left + Math.trunc((right - left + 1) / 2); | |
if (array[halfIndex] === element) return halfIndex + 1; | |
if (element < array[halfIndex]) right = halfIndex - 1; | |
else left = halfIndex + 1; | |
if (left === right) return left + 1; | |
if ((right - left) === 1) { | |
if (element < array[left]) return left; | |
if (element < array[left + 1]) return left + 1; | |
return left + 2; | |
} | |
if ((right - left) === 2) { | |
if (element < array[left]) return left; | |
if (element < array[left + 1]) return left + 1; | |
if (element < array[left + 2]) return left + 2; | |
return left + 3; | |
} | |
} | |
return halfIndex; | |
}; | |
const sortByCopy = (inputArray: number[]) => { | |
const outputArray = []; | |
if (inputArray[0] > inputArray[1]) outputArray.push(inputArray[1], inputArray[0]); | |
else outputArray.push(inputArray[0], inputArray[1]); | |
for (let i = 2; i < inputArray.length; i += 1) { | |
outputArray.splice( | |
binarySearch(outputArray, inputArray[i]), | |
0, | |
inputArray[i], | |
); | |
} | |
return outputArray; | |
}; | |
const testBinarySearch = () => { | |
const a1 = [6,7,8,9,10, 11,12,13,14,15, 16,17,18,19,20, 21,22]; | |
for (let i = 0; i <= 5; i += 1) { | |
const result = binarySearch(a1, i); | |
const expectedResult = 0; | |
if (result !== expectedResult) throw new Error(`Search work wrong! (expected ${expectedResult} got ${result})`); | |
} | |
for (let i = 6; i <= 22; i += 1) { | |
const result = binarySearch(a1, i); | |
const expectedResult = i - 6 + 1; | |
if (result !== expectedResult) throw new Error(`Search work wrong! (expected ${expectedResult} got ${result})`); | |
} | |
for (let i = 23; i <= 30; i += 1) { | |
const result = binarySearch(a1, i); | |
const expectedResult = a1.length; | |
if (result !== expectedResult) throw new Error(`Search work wrong! (expected ${expectedResult} got ${result})`); | |
} | |
console.log('BinarySearchFunction test: All cases passed!'); | |
}; | |
const compareArrays = (a1: any[], a2: any[]): boolean => { | |
if (a1.length !== a2.length) return false; | |
for (let i = 0; i < a1.length; i += 1) if (a1[i] !== a2[i]) return false; | |
return true; | |
} | |
const testSorting = (input: any[], expected: any[]): void | never => { | |
const real = sortByCopy(input); | |
if (!compareArrays(real, expected)) throw new Error(`Sort function working wrong. Expected ${expected} got ${real}`); | |
}; | |
const startSortingFunctionTestCases = () => { | |
testSorting([5, 4, 3, 2 ,1], [1, 2, 3, 4, 5]); | |
testSorting([1, 2, 3, 4 ,5], [1, 2, 3, 4, 5]); | |
testSorting([1, 4, 3, 2 ,5], [1, 2, 3, 4, 5]); | |
console.log('SortingFunction test: All cases passed!'); | |
} | |
testBinarySearch(); | |
startSortingFunctionTestCases(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment