Question: Trying to get bubble sort to work but it keeps saying that it is not a function. What I am trying to do is get

Trying to get bubble sort to work but it keeps saying that it is not a function. What I am trying to do is get it to sort inputted numbers. Any help would be appreciated nd please show output in jsfiddle.

HTML


JAVASCRIPT

function Node(_content) { this.node = _content; this.next = null; }

function lList(list) { this.head = list || null }

lList.prototype.add = function(_content) {

if (this.head === null) this.head = new Node(_content);

else {

let list = this.head; while (list.next) { list = list.next; }

list.next = new Node(_content) } }

lList.prototype.print = function() {

if (this.head === null) return null;

let list = this.head;

while (list) { console.log(list.node) if (list.next) console.log(', ') list = list.next } } lList.prototype.mergeSort = function(list) {

if (list.next === null) return list;

let count = 0; let countList = list let leftPart = list; let leftPointer = list; let rightPart = null; let rightPointer = null;

while (countList.next !== null) { count++; countList = countList.next; }

let mid = Math.floor(count / 2) let count2 = 0;

while (count2 < mid) { count2++; leftPointer = leftPointer.next; }

rightPart = new lList(leftPointer.next); leftPointer.next = null;

return this._mergeSort(this.mergeSort(leftPart), this.mergeSort(rightPart.head)) }

lList.prototype._mergeSort = function(left, right) {

let result = new lList()

let resultPointer = result.head; let pointerLeft = left; let pointerRight = right;

while (pointerLeft && pointerRight) { let tempNode = null;

if (pointerLeft.node > pointerRight.node) { tempNode = pointerRight.node pointerRight = pointerRight.next; } else { tempNode = pointerLeft.node pointerLeft = pointerLeft.next; }

if (result.head == null) { result.head = new Node(tempNode) resultPointer = result.head } else { resultPointer.next = new Node(tempNode) resultPointer = resultPointer.next } }

resultPointer.next = pointerLeft; while (resultPointer.next) resultPointer = resultPointer.next

resultPointer.next = pointerRight

return result.head; }

let l = new lList();

function addVariable() {

var c = document.getElementById("uInput").value;

l.add(c);

console.log(c); }

function GetLength()

{

let list = this.head;

var count = 0; //count the number of nodes in linked list

while (list.next)

{

list = list.next;

count = count + 1;

}

count = count + 1;

return count;

}

lList.prototype._bubbleSort = function() {

var length = l.GetLength();

for (var firstLoopPosition = 0; firstLoopPosition < lList.length; firstLoopPostion++) {

for (var secondLoopPosition = 0; secondLoopPosition < (length - firstLoopPosition - 1); secondLoopPosition++); {

if (lList.secondLoopPosition > lList.secondLoopPosition + 1) {

var temp = lList.secondLoopPosition;

lList.secondLoopPosition = lList.secondLoopPosition + 1;

lList.secondLoopPosition = tmp; }

}

}}

function mergeProgram() {

l.head = lList.prototype.mergeSort(l.head)

console.log(' After sorting : ');

l.print(); }

function bubbleSort() {

l.head = lList.prototype.bubbleSort(l.head)

console.log('After sorting : ');

l.print(); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!