Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let quickSort = (arr = [], left = [], right = []) => {
- if (arr.length <= 1) return arr;
- let pivot = arr.pop();
- for (let i = 0; i < arr.length; i++)
- (compare(arr[i], pivot) ? left : right).push(arr[i]);
- return [...quickSort(left), pivot, ...quickSort(right)];
- }
- const prompt = require('prompt-sync')({ sigint: true });
- const songs = [
- 'Wonderwall',
- 'Californication',
- 'Free Fallin\'',
- 'Beat It',
- 'Africa'
- ];
- console.log(quickSort(songs));
- function compare(left, right) {
- while (true) {
- let input = prompt(`Is "${left}" better than "${right}"? [y/n]`);
- input = input.toLowerCase();
- if (isValidInput(input)) return input == 'y';
- console.log('Invalid input!');
- }
- }
- function isValidInput(input) {
- return input == 'y' || input == 'n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement