How to partition linked list around minimum value - javascript

I am trying to perform a selection sort on a linked list using recursion and I am having some trouble partitioning my linked list around the node with the smallest value on each pass through my recursive sort function. I am trying to get the node with the smallest value, partition the linked list around the smallest value, append the smallest to the front, join the two partitioned lists, and then perform the sort again on the joined partitioned list until the entire linked list is sorted. For example:
q w e r t // partition around e
e -> q w r t // join the partitions
eq -> w r t // append q to e
eq -> w r t // partition around r
and so forth.
My sort method:
Node.prototype.sort = function(){
if(!next){
return this;
} else {
var a = null;
var b = null;
var smallest = this.smallest();
splitIt(smallest, a, b);
appendSmallest(smallest);
a.join(b);
a.sort();
}
}
I get the smallest node like so:
Node.prototype.smallest = function(){
if(!next) return this;
var sm = next.smallest();
if(sm.data < this.data){
return sm;
}
return this;
}
Here are my append and join methods:
Node.prototype.appendSmallest = function(smallest){
if(!next) next = smallest;
}
Node.prototype.join = function(otherNode){
if(!next) next = otherNode;
else next.join(otherNode);
}
I am having some trouble implementing the splitIt method recursively. What would the pseudocode be for such operation?

I'm assuming you are using pure JavaScript, as there is no indication otherwise.
In your code you use several times the word Node as kind of a variable type in a way that is not valid JS. You declare variables with the word var (and in ECMAScript6 let for block scoped variables). Look at this question. So for example in smallest you write:
var sm = next.smallest();
In sort you have two additional problems: first, you pass null variables as parameters in hope that the function will assign objects that will replace them (see the explanation here regarding the nature of reference valued variables (not primitive valued) in JS). Second, assuming you forgot but meant to have this line in appendSmallest
else { next.appendSmallest(smallest);}
then I think you have an infinite loop, as smallest is appended to this linked list, which is (if splitIt works properly) is the same as a.
My suggestion is doing the split and join together as a "spitSmallest" function:
Node.prototype.spitSmallest = function(smallest){
//we know that this node is not smallest
if (this.next == smallest){
this.next = this.next.next;
smallest.next = null; //again not really needed
} else {
this.next.spitSmallest(smallest);
}
}
Node.prototype.sort = function(){
if(!this.next){
return this;
} else {
var smallest = this.smallest();
var restHead;
if (smallest==this){
restHead = this.next;
this.next = null; //not needed but makes it more readable
} else {
restHead = this;
this.spitSmallest(smallest);
}
smallest.next = restHead.sort();
return smallest;
}
}

Related

Sorting a link list

I am trying to solve the LeetCode problem 148. Sort List
Given the head of a linked list, return the list after sorting it in ascending order.
I am trying to do it in a recursive way before trying something smarter, as I am learning to handle data structures.
This is my code:
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* #param {ListNode} head
* #return {ListNode}
*/
var sortList = function(head) {
let previousNode = head
if(!head){
return head
}
let node = head.next
if(!node){
return head
}
let start = head
let previousNode1 = head
function sortList1(node, previousNode){
if(node.next == null){
return start
}
let temp = node.next;
if(traverseFromHead(node)){
start = node
}
previousNode1 = node
return sortList1(temp, node)
}
return sortList1(node, previousNode)
function traverseFromHead(node){
let myPosition = start
let inserted = false
if(start.val > node.val){
previousNode1.next = node.next
node.next = start
console.log("found in head excahange", node)
return node;
}
let myprevious2 = start
while(myPosition.next != null){
if(myPosition.val>=node.val){
console.log("before check start was", start, "with position at", myPosition.val, "for point", node.val, "my previous is", myprevious2.val)
let temp = node.next
myprevious2.next = node
node.next = myPosition
// previousNode1.next = temp
console.log("after update start is", start, "with position at", myPosition.val, "for point", node.val)
return null
}
myprevious2 = myPosition;
myPosition = myPosition.next
}
return false
}
};
I am not able to get it working correctly; it must be I am doing something wrong by logic or by concept
For instance for the linked list 4→2→3→0 the expected output would be 0→2→3→4, but my code produces 2→0.
Where is the problem in my code?
You have tried to implement insertion sort.
There are these issues that prevent it from working correctly:
The base case of the recursive function is not correct. With if(node.next == null) you are stopping too early. It could well be that this tail node should be moved elsewhere in the list, yet this node's value is not compared with anything. The stop condition really should be node == null.
previousNode1 = node is not always correctly identifying the previous node. If the call to traverseFromHead moved node to elsewhere in the list, then previousNode1 should not change, because what used to be the node before node, will now have become the node before the next node you want to process. For the same reason the second argument you pass in the recursive call is most often wrong: sortList1(temp, node).
It is a bit overwhelming to have that many variants of previousNodeXX variables. I would suggest to at least eliminate this previousNode1 and continue to work with previousNode, passing it also as argument to traverseFromHead. So call it as traverseFromHead(node, previousNode) and make sure you pass the correct second argument to sortList1. There are two cases to distinguish:
When node wasn't moved, then sortList1(temp, node) is correct, but when node was moved, it should be sortList1(temp, previousNode). You can make the distinction with a conditional operator:
sortList1(temp, previousNode.next != node ? previousNode : node)
traverseFromHead only removes the node from its current position in the if case, but forgets to do the same in the more general case. In the general case, the node is inserted, but previousNode.next is not adapted, meaning you now have two nodes whose next property point to node. There are several ways to do it right. I would suggest to perform the node-removal action in all cases before doing anyting else. You could place the code for node extraction before the if statement so that it always happens:
previousNode.next = node.next // <-- should always happen
if(start.val > node.val){
//...
I can understand why you put previousNode1.next = temp in comments inside the loop. Most often this needs to happen, but not when node didn't move! To solve this dilemma, perform a quick exit when node is already at its correct position (in comparison with previousNode). So at the top of the function do:
if (node.val >= previousNode.val) return null;
Now you can be sure that node will move.
traverseFromHead has a strange while condition. With the above corrections in place, this while condition can just be the opposite of the if condition, so that you can deal with the insertion after the loop:
while (myPosition.val < node.val)
myprevious2 = myPosition;
myPosition = myPosition.next
}
Here is your code with those corrections:
var sortList = function(head) {
let previousNode = head
if(!head){
return head
}
let node = head.next
if(!node){
return head
}
let start = head
function sortList1(node, previousNode){
if(node == null){ // Corrected base case
return start
}
let temp = node.next;
if(traverseFromHead(node, previousNode)){ // Pass the second argument
start = node
}
// Depending on whether node was moved, the node that precedes temp is different
return sortList1(temp, previousNode.next != node ? previousNode : node)
}
return sortList1(node, previousNode)
function traverseFromHead(node, previousNode){ // Second argument
if (node.val >= previousNode.val) return null; // Quick exit for trivial case
previousNode.next = node.next // Always first extract the node
if(start.val >= node.val){ // Equal is also good, so >=
node.next = start
return node;
}
let myPosition = start.next // Can start iteration at second node
let myprevious2 = start
while (myPosition.val < node.val) { // Look for the insertion spot
myprevious2 = myPosition;
myPosition = myPosition.next
}
// Now perform the re-insertion
myprevious2.next = node
node.next = myPosition
return null
}
};
Other remarks
Insertion sort is not the most efficient among sorting algorithms, and for linked lists it is quite easy to implement better performing sorting algorithms.
See for instance Merge sort on linked list
I have here adapted that solution for the LeetCode challenge (spoiler):
var sortList = function(head) {
if (!head || !head.next) return head; // Nothing to sort
// Find last node of first half
let tail = head;
for (let fast = tail.next; fast?.next; fast = fast.next.next) {
tail = tail.next;
}
// Split list into two halves
let head2 = tail.next;
tail.next = null;
// Recursively sort the two shorter lists
head = sortList(head);
head2 = sortList(head2);
// Merge the two sorted lists
if (head.val > head2.val) [head2, head] = [head, head2];
tail = head;
while (tail.next && head2) {
if (tail.next.val > head2.val) [head2, tail.next] = [tail.next, head2];
tail = tail.next;
}
tail.next ??= head2;
return head;
};
MergeSort naturally fits for linked lists.
In merge sort you consider that merging 2 size one lists is trivial (just put the higher head value after the lower one).
Extending that idea, if you have two already sorted lists, then it's easy to merge them as well. Just create a new list where you add the highest of both lists till both lists are empty.
So you can do a merge sort by creating first 2 lists of size 1. (ie. the first 2 elements of your list) Then merging them.
Then create a second list of size 2 (by merging 2 of size 1).
And continue until you have merged the entire original list into a sorted list.
Recursion
To implement this recursively first write a merge function that given two sorted lists merges them by preserving the sort order.
Then do the following to implement sort:
If your list is empty, then return the list as your result
Now merge the first element with sort(rest of the list)

How to copy the values of an array (not pointing). Slice isn't working for me

I have a function that tries to systematically add arrays to another multidimensional array. At each step of the way the arrays being added are calculated correctly, however, these calculations change the previously entered values. I've tried using slice but I'm clearly doing it wrong :(.
Please see code below - it is the return posMatrix that is being affected.
function allPossibilities(hand) {
var startingHandLength = hand.length;
var potHand = Array.prototype.slice.call(hand);
var scores = new Array();
var posMatrix = new Array();
var nextCard = 1;
var progressStage = true;
var finished = false;
var shallowArr = new Array();
do {
scores = calculateScores(potHand);
var maxScore = Math.max.apply(null, scores)
shallowArr = potHand.slice();
if (maxScore>16.5)
{posMatrix.push([shallowArr,maxScore])
console.log(posMatrix);
debugger;
if (potHand.length !== startingHandLength)
{
do{
if(potHand[potHand.length-1][1] < 10)
{
potHand[potHand.length-1][1]++;
progressStage = true;
}
else {potHand.pop();
potHand[potHand.length-1][1]++;}
}
while(progressStage === false)
}
}
else
{
potHand.push(["Imaginary",1,"Imaginary"]);
}
progressStage=false;
if(potHand.length === startingHandLength)
{finished = true;}
}
while(finished === false);
return posMatrix;
}
If the starting hand > 16.5, the function works as none of the other code gets to run. But otherwise it does not. The final return should be an array where each element is looks like this: [[array],number]. The number seems to come out fine, but since it is not an object it is not affected. I would expect the [array]s to be different from one another, currently they are all the same.
Slice returns a shallow copy of array, since you have multidimensional array so you need to deep clone of array
JSON.parse(JSON.stringify(array))
Or you can use loadash cloneDeep
You made a shallow copy of hand (which, BTW, you should've included). With statements like this
potHand[potHand.length-1][1]++;
you're accessing and modifying elements of hand, too.
Here, potHand[potHand.length-1] is an object, and it's en element of hand (not a copy - the same element).

When should I use a JavaScript array in lieu of a JavaScript object literal?

This question may seem trivial but it's something that's been bothering me for a while so I thought I'd ask. Apologies in advance if this question seems silly/naive to you.
So in JavaScript, object literals {} are hash tables under the hood. Meaning that they have near constant time lookup and insertion. So the question I keep wondering is (outside of pure convenience) why would I ever use a JavaScript array in lieu of an object literal?
Take this example of creating a Queue in JavaScript;
1) First, we'll use an Object literal as our storage component;
var Queue = function() {
this.storage = {};
this.order = [];
this.length = 0;
this.add = function(item) {
this.storage[item] = 1;
this.order.push(item);
this.length++;
};
this.remove = function() {
var removed = this.order.shift();
delete this.storage[removed];
this.length--;
return removed;
};
this.contains = function(value) {
return this.storage[value] ? true : false;
};
this.size = function() {
return this.length;
};
};
2) Second, using an array as our Queue's storage component
var Queue2 = function() {
this.storage = [];
this.add = function(item) {
this.storage.push(item);
return this;
};
this.remove = function() {
return this.storage.shift();
};
this.contains = function(target) {
var clone = this.storage.slice(0);
clone.sort();
var recurse = function(low, high) {
if (high === low) return false;
var mid = Math.floor((high - low) / 2) + low;
if (clone[mid] === target) return true;
else if (clone[mid] > target)
return recurse(low, mid);
else if (clone[mid] < target)
return recurse(mid + 1, high);
};
return recurse(0, clone.length);
}
};
lets create two separate Queues;
var objQueue = new Queue();
var arrayQueue = new Queue2();
Now lets add 1 million random passwords to both our Queues.
for (var i = 0; i < 1000000; i++) {
objQueue.add(Math.random().toString(36).slice(2));
}
for (var i = 0; i < 1000000; i++) {
arrayQueue.add(Math.random().toString(36).slice(2));
}
Normally we would never have to do this all at one time. But yes, it's going to take a little longer to fill up our object Queue. We've had to create a separate array in our Queue constructor to keep track of the order. Yes, this is very annoying. Unfortunately, when adding keys as numbers to a JavaScript object, JavaScript auto-sorts the numerical keys. So if you add 4, then 6, then 2, then 1. The Object will look like this:
{ '1': 1, '2':1, '4':1, '6':1 }
and for a Queue, we want it to look like this:
{ '4': 1, '6':1, '2':1, '1':1 }
NOTE: This is not the case when adding strings. With strings, the order of addition is preserved so creating a separate array to preserve order is unnecessary. I'm not sure if this is intentional or just a mistake? But in this particular case, since we're creating a Queue and order matters, it's annoying. Which could be one reason people might prefer to just use an array for storage in this situation.
Removing from the queue will have the same time complexity in both cases because we're always removing the first value. No big deal there.
But what happens if we want to search our Queue? This probably goes against the nature of what a Queue is supposed to be used for(does it??? I dunno, maybe our Queue is a priority queue, we're tired of waiting, and we want to search where our number is in the queue) but let's just say we want to search this giant Queue we've created.
Lets add a value value to the end of both our Queues that is not random:
objQueue.add('bvflq9kk61xajor');
arrayQueue.add('bvflq9kk61xajor');
Now lets search for this value in our array Queue and measure the time:
var start1 = new Date().getTime();
var result1 = arrayQueue.contains('bvflq9kk61xajor');
var end1 = new Date().getTime();
var time1 = end1 - start1;
Now lets search for this value in our object Queue and measure the time:
var start2 = new Date().getTime();
var result2 = objQueue.contains('bvflq9kk61xajor');
var end2 = new Date().getTime();
var time2 = end2 - start2;
RESULTS
console.log('Execution time for Array Queue: ' + time1);
console.log('RESULT:', result1);
//Execution time for Array Queue: **3873**
//RESULT: true
console.log('Execution time for Object Queue: ' + time2);
console.log('RESULT', result2);
//Execution time for Array Queue: **0**
//RESULT: true
The main reason the array Queue takes so much longer to execute is because I'm having to sort it before performing a Binary Search on it. Binary search is fast but requires the array to be pre-sorted. I could pre-sort the array each time I add a new value to the array Queue but then this would defeat the purpose of what a Queue is: FIFO(first in first out) and we'd start running into the same order issue we have with objects.
So I guess the real question I'm wondering is why do we even bother with arrays when we can do most things we need to do with a JS Object? Especially when JS objects are hash tables and thus we get near constant time lookup with them.
I guess it wouldn't matter so much if you never planned on searching through your data but when is that ever the case?? When you fill up an object or an array with data, you're inevitably going to want to search your data? Yes? No? Am I totally wrong here? And when you go to search this data, you'll be stoked to have stored it in what's essentially a hash table. Right?
Am I totally wrong here? Missing something? Would love some feedback on when to use arrays and when to use object literals. I'm guessing one case would be when your data set is small.

Counter array in Javascript

I am trying to make two arrays. the unique array can get the elements (no repeats) from the text array, and the counter one can count the frequency of each elements. but something is wrong with the counter one.
var unique_array=new Array();
var counter_array=new Array();
var unique=true;
for (i=0;i<text_array.length;i++){
if (unique_array.length==0){
unique_array.push(text_array[0]);
counter_array.push(1);
}
else if(unique_array.length>0&&unique_array.length<=text_array.length){
for (j=0; j<unique_array.length;j++){
if (text_array[i]==unique_array[j]){
counter_array[j]=counter_array[j]+1;// something wrong with the
alert(counter_array[j]);
var unique=false;
}
}
if (unique==true){
unique_array.push(text_array[i]);
counter_array.push[1];
}
unique=true;
}
You could also simplify the code down using a hashmap and some ES5 higher-order functions:
var text_array = ["a1","a1","a2","a3","a2","a4","a1","a5"];
var counts = {};
text_array.forEach(function(el) {
counts[el] = counts.hasOwnProperty(el) ? counts[el]+1 : 1;
});
var unique_array = Object.keys(counts);
var counter_array=unique_array.map(function(key) { return counts[key]; })
You can do this much more simply using an object. Let the values be the keys of an object, then just increment the count of each property as you go. At the end, you can get an array of the unique keys and their values:
var text_array = ['foo','bar','foo','fum','fum','foo'];
var i = text_array.length;
var obj = {};
while (i--) {
if (obj.hasOwnProperty(text_array[i])) {
obj[text_array[i]]++;
} else {
obj[text_array[i]] = 1;
}
}
console.log('Unique values: ' + Object.keys(obj)); // Unique values: foo,fum,bar
console.log('Value counts: ' + Object.keys(obj).map(function(v){return obj[v]})); // Value counts: 3,2,1
Note that the sorting of counts in the output is purely coincidental.
As Jasvir posted, you can make it pretty concise:
var obj = {};
text_array.forEach(function(v) {
obj.hasOwnProperty(v)? ++obj[v] : obj[v] = 1;
});
But the first example is a bit easier to digest.
I think the approach is what's making it difficult. A hash table / associative array would be much easier to work with.
With a hash table (an object {} in JS), you can store each word in a key and increment the value of the key when you encounter the word again. Then, at the end, just go through the hash table and gather up all the keys which have small values. Those are your unique words.
function get_unique_words(text_array) {
var hash_table, i, unique_words, keys;
hash_table = {};
for(i = 0; i < text_array.length; i++) {
if(hash_table[text_array[i]] === undefined) {
hash_table[text_array[i]] = 1;
} else {
hash_table[text_array[i]]++;
}
}
// go through the hash table and get all the unique words
unique_words = [];
keys = Object.keys(hash_table);
for(i = 0; i < keys.length; i++) {
if(hash_table[keys[i]] === 1) {
unique_words.push(keys[i]);
}
}
return unique_words.sort();
}
console.log(get_unique_words(
['blah', 'blah', 'blah', 'goose', 'duck',
'mountain', 'rock', 'paper', 'rock', 'scissors']
));
Some issues and suggestions :
Don't use var twice for the same variable.
Browsers deal with it ok, but for clarity you should only be declaring your variables once.
Always localize your loop counters - forgetting a var before your i and j will cause them to become global variables.
This is relevant when you have a page with lots of code - all global variables will show up in the debugger's watch list at all times, making it harder to debug your code.)
Use the array literal notation [] instead of the function form Array.
The function form is longer and it's easier to forget the new. It's also easier to read (IMO).
Use more whitespace (it won't bite), such as before and after an equals sign:
var x = 1;
// vs.
var x=1;
It makes the code easier to read and most people don't overdo it.
Indent your code when it's inside a block (e.g. function, if, else, while, for, etc.).
This makes it easier to read the control flow of the code and will help prevent bugs.
Use three equals signs (===) unless you are using loose equality on purpose.
This will help someone looking at your code later (probably yourself) understand better what the test is supposed to be testing.

Give structure to 'random' function js?

I have an array and a function that picks randomly elements from this array and displays them in a div.
My array:
var testarray = [A, B, C, D, E, F];
Part of the js function:
var new_word = testarray[Math.floor((Math.random()*testarray.length)+1)];
$("#stimuli").text(new_word);
My question is, is there a way I can have them picked randomly in a certain ratio/order?
For example, that if I have my function executed 12 times, that each of the six letters is displayed exactly twice, and that there can never be the same letter displayed twice in a row?
You might want to try a quasi-random sequence. These sequences have the properties you're after. http://en.wikipedia.org/wiki/Low-discrepancy_sequence
Edit:
To your question in the comment: Of course there are hundreds ways to solve a problem. Think about using artificial intelligence, a mathematical algorithm or the answers given by others here. It depends on what you really want to achieve. I just gave a robust solution that is easy to understand and implement..
Here's another (different approach), same result but with the prevention that values displays twice in a row.
Jsfiddle: http://jsfiddle.net/kychan/jJE7F/
Code:
function StructuredRandom(arr, nDisplay)
{
// storage array.
this.mVar = [];
this.previous;
// add it in the storage.
for (var i in arr)
for (var j=0; j<nDisplay; j++)
this.mVar.push(arr[i]);
// shuffle it, making it 'random'.
for(var a, b, c = this.mVar.length; c; a = Math.floor(Math.random() * c), b = this.mVar[--c], this.mVar[c] = this.mVar[a], this.mVar[a] = b);
// call this when you want the next item.
this.next = function()
{
// default value if empty.
if (this.mVar.length==0) return 0;
// if this is the last element...
if (this.mVar.length==1)
{
// we must give it..
return this.mVar.pop();
// or give a default value,
// because we can't 'control' re-occuring values.
return -1;
}
// fetch next element.
var element = this.mVar.pop();
// check if this was already given before.
if (element==this.previous)
{
// put it on top if so.
this.mVar.unshift(element);
// call the function again for next number.
return this.next();
}
// set 'previous' for next call.
this.previous = element;
// give an element if not.
return element;
};
}
NOTE: In this example we can't fully control that the same values are displayed twice.. This is because we can control the first numbers, but when there is only one number left to display, we must either give it or display a default value for it, thus there is a chance that the same value is shown.
Good luck!
Like this?
var arr = [1,2,3,4,5,6,7], // array with random values.
maxDispl = 2, // max display.
arr2 = init(arr) // storage.
;
// create object of given array.
function init(arr)
{
var pop = [];
for (var i in arr)
{
pop.push({value:arr[i], displayed:0});
}
return pop;
}
// show random number using global var arr2.
function showRandom()
{
// return if all numbers has been given.
if (arr2.length<1) return;
var randIndex= Math.floor(Math.random()*arr2.length);
if (arr2[randIndex].displayed<maxDispl)
{
document.getElementById('show').innerHTML+=arr2[randIndex].value + ', ';
arr2[randIndex].displayed++;
}
else
{
// remove from temp array.
arr2.splice(randIndex, 1);
// search for a new random.
showRandom();
}
}
// iterate the function *maxDispl plus random.
var length = (arr.length*maxDispl) + 2;
for (var i=0; i<length; i++)
{
showRandom();
}
jsfiddle: http://jsfiddle.net/kychan/JfV77/3/

Categories