Skip to content
This repository was archived by the owner on Mar 22, 2025. It is now read-only.

style: format code with StandardJS #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
135 changes: 67 additions & 68 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,77 @@
function quickSort(arr) {
return tailRecursiveQuickSort(arr, 0, arr.length - 1);
}

function tailRecursiveQuickSort(arr, start, end) {
while (start < end) {
const pivotIndex = partition(arr, start, end);

if (pivotIndex - start < end - pivotIndex) {
tailRecursiveQuickSort(arr, start, pivotIndex - 1);
start = pivotIndex + 1;
} else {
tailRecursiveQuickSort(arr, pivotIndex + 1, end);
end = pivotIndex - 1;
}
function quickSort (arr) {
return tailRecursiveQuickSort(arr, 0, arr.length - 1)
}

function tailRecursiveQuickSort (arr, start, end) {
while (start < end) {
const pivotIndex = partition(arr, start, end)

if (pivotIndex - start < end - pivotIndex) {
tailRecursiveQuickSort(arr, start, pivotIndex - 1)
start = pivotIndex + 1
} else {
tailRecursiveQuickSort(arr, pivotIndex + 1, end)
end = pivotIndex - 1
}

return arr;
}

function partition(arr, start, end) {
const pivot = arr[end];
let i = start - 1;

for (let j = start; j < end; j++) {
if (arr[j] <= pivot) {
i++;
[arr[i], arr[j]] = [arr[j], arr[i]];
}

return arr
}

function partition (arr, start, end) {
const pivot = arr[end]
let i = start - 1

for (let j = start; j < end; j++) {
if (arr[j] <= pivot) {
i++;
[arr[i], arr[j]] = [arr[j], arr[i]]
}

[arr[i + 1], arr[end]] = [arr[end], arr[i + 1]];
return i + 1;
}

function mergeSort(arr) {
if (arr.length <= 1) {
return arr;
}

const middle = Math.floor(arr.length / 2);
const left = arr.slice(0, middle);
const right = arr.slice(middle);

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

[arr[i + 1], arr[end]] = [arr[end], arr[i + 1]]
return i + 1
}

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

function merge(left, right) {
let result = [];
let leftIndex = 0;
let rightIndex = 0;

while (leftIndex < left.length && rightIndex < right.length) {
if (left[leftIndex] < right[rightIndex]) {
result.push(left[leftIndex]);
leftIndex++;
} else {
result.push(right[rightIndex]);
rightIndex++;
}

const middle = Math.floor(arr.length / 2)
const left = arr.slice(0, middle)
const right = arr.slice(middle)

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

function merge (left, right) {
const result = []
let leftIndex = 0
let rightIndex = 0

while (leftIndex < left.length && rightIndex < right.length) {
if (left[leftIndex] < right[rightIndex]) {
result.push(left[leftIndex])
leftIndex++
} else {
result.push(right[rightIndex])
rightIndex++
}

return result.concat(left.slice(leftIndex)).concat(right.slice(rightIndex));
}

export default function Sort(arr) {
// Determine the best sorting method based on array characteristics
if (arr.length > 899) {
// Use mergesort for larger arrays

return result.concat(left.slice(leftIndex)).concat(right.slice(rightIndex))
}

export default function Sort (arr) {
// Determine the best sorting method based on array characteristics
if (arr.length > 899) {
// Use mergesort for larger arrays
// console.log("Using mergesort");
return mergeSort(arr);
} else {
// Use quicksort for smaller arrays
return mergeSort(arr)
} else {
// Use quicksort for smaller arrays
// console.log("Using quicksort");
return quickSort(arr);
}
return quickSort(arr)
}
}
38 changes: 19 additions & 19 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import Sort from './index.js';
import Sort from './index.js'

// Example usage:
const unsortedArray = [];
for (let i = 1900; i > 0; i--) {
unsortedArray.push(i);
}
const sortedArray = Sort(unsortedArray);
const unsortedArray = []
for (let i = 1900; i > 0; i--) {
unsortedArray.push(i)
}
const sortedArray = Sort(unsortedArray)

function isFlatArraySorted(array, sortingOrder = 'ascending') {
for (let i = 0; i < array.length - 1; i++) {
if (sortingOrder === 'ascending' && array[i] > array[i + 1]) {
return false;
} else if (sortingOrder === 'descending' && array[i] < array[i + 1]) {
return false;
}
function isFlatArraySorted (array, sortingOrder = 'ascending') {
for (let i = 0; i < array.length - 1; i++) {
if (sortingOrder === 'ascending' && array[i] > array[i + 1]) {
return false
} else if (sortingOrder === 'descending' && array[i] < array[i + 1]) {
return false
}
return true;
}
return true
}

if(isFlatArraySorted(sortedArray)) {
console.log('test passed');
}else{
console.log('test failed');
}
if (isFlatArraySorted(sortedArray)) {
console.log('test passed')
} else {
console.log('test failed')
}