Skip to content

Instantly share code, notes, and snippets.

@fleepgeek
Last active February 22, 2019 13:45
Show Gist options
  • Save fleepgeek/acebe4a709cd200918e2a9914abeb5c9 to your computer and use it in GitHub Desktop.
Save fleepgeek/acebe4a709cd200918e2a9914abeb5c9 to your computer and use it in GitHub Desktop.
// function bubbleSort(passedArray) {
// // clone the array to prevent the passed array from
// // being modified
// const arr = [...passedArray];
// const len = passedArray.length;
// let swapped = false;
// let count = 0;
// for (let i = 0; i < len; i++) {
// count++;
// // Swap elements if the current is greater than the next
// for (let j = 0; j < (len - i - 1); j++) {
// if(arr[j] > arr[j + 1]) {
// [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
// swapped = true;
// }
// }
// if(!swapped) {
// console.log("Not swapped")
// return arr;
// }
// }
// console.log("Interated " + count + " times");
// return arr;
// }
// function bubbleSort(passedArrray) {
// let swapped = false;
// let arr = [...passedArrray];
// let count = 0
// do {
// swapped = false;
// count++
// arr.forEach((item, index) => {
// if(item > arr[index + 1]) {
// [arr[index], arr[index + 1]] = [arr[index + 1], arr[index]];
// swapped = true;
// }
// });
// } while (swapped);
// console.log("Interated " + count + " times");
// return arr;
// }
function quickSort(passedArray) {
const arr = [...passedArray];
if(arr.length < 2) {
return arr;
}
const pivotIndex = arr.length - 1;
const pivot = arr[pivotIndex];
const left = [];
const right = [];
for (let i = 0; i < pivotIndex; i++) {
const item = arr[i];
item < pivot ? left.push(item) : right.push(item);
}
return [...quickSort(left), pivot, ...quickSort(right)];
}
let a = [8,6,7,3,5];
// let a = [1,2,3,4,5];
// console.log(bubbleSort(a));
// console.log(a);
console.log(quickSort(a));
// function createStack() {
// const stack = [];
// return {
// push(item) {
// return stack.push(item);
// },
// pop() {
// return stack.pop();
// },
// peek() {
// return stack[stack.length - 1];
// },
// length() {
// return stack.length;
// }
// }
// }
// class Stack {
// constructor() {
// this.items = [];
// }
// push(item) {
// return this.items.push(item);
// }
// pop() {
// return this.items.pop();
// }
// peek() {
// return this.items[this.items.length - 1];
// }
// length() {
// return this.items.length;
// }
// }
// // const cards = createStack();
// const cards = new Stack();
// cards.push(1);
// cards.push(2);
// cards.push(3);
// cards.push(4);
// cards.pop();
// cards.pop();
// console.log(cards.peek());
// function createQueue() {
// const queue = [];
// return {
// enqueue(item) {
// queue.unshift(item);
// },
// dequeue() {
// return queue.pop()
// },
// peek() {
// return queue[queue.length - 1];
// },
// length() {
// return queue.length;
// }
// }
// }
// const queue = createQueue();
// queue.enqueue(1);
// queue.enqueue(2);
// queue.enqueue(3);
// queue.enqueue(4);
// queue.dequeue();
// queue.dequeue();
// console.log(queue.peek());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment