Skip to content

Instantly share code, notes, and snippets.

View vaidehijoshi's full-sized avatar

Vaidehi Joshi vaidehijoshi

View GitHub Profile
var items = [19, 22, 63, 105, 2, 46];
quickSort(items, 0, items.length - 1);
> ** pivot is: 63
> ** left is: 19
> ** right is: 46
> l is now pointing to: 22
> l is now pointing to: 63
> ** now swapping l and r pointers: 63 46
> ** now swapping l and r pointers: 105 2
function quickSort(items, leftIndex, rightIndex) {
// Declare an index that will be our pivot reference.
var pivotIndex;
// If the array has only one item, it's already sorted!
// If it has no items, we don't want to try to sort it!
if (items.length > 1) {
// As long as the array has two items, we can parition it.
pivotIndex = partition(items, leftIndex, rightIndex);
// The partition() method takes a list of items, and a left
// reference, as well as a right reference. Both left and right
// are indexes to indicate where the pointers should start.
function partition(items, left, right) {
// Find the pivot by adding the two indexes together
// and dividing by two (the middle element, effectively).
var pivot = items[Math.floor((right + left) / 2)];
var l = left;
var r = right;
function swap(items, leftPointerIndex, rightPointerIndex){
// Create a temporary reference for the left item.
var tempReference = items[leftPointerIndex];
// Move left item to the index that contains right item.
// Move right item to the temporary reference.
items[leftPointerIndex] = items[rightPointerIndex];
items[rightPointerIndex] = tempReference;
}
var array = [5, 1, 7, 3, 2, 8, 6, 4];
mergeSort(array);
> array is: (2) [5, 1]
> left array is: [5]
> ** end of merge function ** array is: (2) [1, 5]
> array is: (2) [7, 3]
> left array is: [7]
> ** end of merge function ** array is: (2) [3, 7]
function mergeSort(array) {
// Determine the size of the input array.
var arraySize = array.length;
// If the array being passed in has only one element
// within it, it is considered to be a sorted array.
if (arraySize === 1) {
return;
}
function preorderSearch(node) {
// Check that a node exists.
if (node === null) {
return;
}
// Print the data of the node.
console.log(node.data);
// Pass in a reference to the left child node to preorderSearch.
function levelOrderSearch(rootNode) {
// Check that a root node exists.
if (rootNode === null) {
return;
}
// Create our queue and push our root node into it.
var queue = [];
queue.push(rootNode);
// Create an empty queue.
var queue = [];
// Add values to the end of the queue.
queue.push(1); // queue is now [1]
queue.push(2); // queue is now [1, 2]
// Remove the value at the top of the queue.
var topOfQueueValue = queue.shift();
console.log(topOfQueueValue) // returns 1
// The queue now has just one element in it.
console.log(queue) // returns [2]
function bookHashing(bookTitle, hashTableSize) {
// Remove any spaces from book title.
var strippedBookTitle = bookTitle.replace(/\s/g, '')
// Divide the length of the title by the hash table size.
// Return the remainder.
return strippedBookTitle.length % hashTableSize;
}
bookHashing("The Grapes of Wrath", 12);
// 4