bubble sort in array stack and linked list in javascript

Bubble Sort in Array

Bubble sort is a simple sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. Here's how to implement bubble sort in an array using JavaScript:

index.tsx
function bubbleSort(arr) {
  let n = arr.length;
  for(let i = 0; i < n-1; i++) {
    for(let j = 0; j < n-i-1; j++) {
      if (arr[j] > arr[j+1]) {
        // swap arr[j] and arr[j+1]
        let temp = arr[j];
        arr[j] = arr[j+1];
        arr[j+1] = temp;
      }
    }
  }
  return arr;
}

// Usage
let arr = [64, 34, 25, 12, 22, 11, 90];
console.log(bubbleSort(arr)); // Output: [11, 12, 22, 25, 34, 64, 90]
419 chars
19 lines

Bubble Sort in Stack

A stack is a collection of elements, where elements are stored and retrieved based on last-in-first-out (LIFO) principle. Here's how to implement bubble sort in a stack using JavaScript:

index.tsx
class Stack {
  constructor() {
    this.items = [];
  }
  push(element) {
    this.items.push(element);
  }
  pop() {
    return this.items.pop();
  }
  isEmpty() {
    return this.items.length === 0;
  }
  printStack() {
    let str = "";
    for(let i = 0; i < this.items.length; i++) {
      str += this.items[i] + " ";
    }
    return str.trim();
  }
  sort() {
    let n = this.items.length;
    for(let i = 0; i < n-1; i++) {
      for(let j = 0; j < n-i-1; j++) {
        if (this.items[j] > this.items[j+1]) {
          // swap this.items[j] and this.items[j+1]
          let temp = this.items[j];
          this.items[j] = this.items[j+1];
          this.items[j+1] = temp;
        }
      }
    }
    return this;
  }
}

// Usage
let stack = new Stack();
stack.push(64);
stack.push(34);
stack.push(25);
stack.push(12);
stack.push(22);
stack.push(11);
stack.push(90);
console.log(stack.sort().printStack()); // Output: 11 12 22 25 34 64 90
951 chars
47 lines

Bubble Sort in Linked List

A linked list is a linear data structure where each element is a separate object. Linked list elements are not stored at contiguous memory locations. Here's how to implement bubble sort in a linked list using JavaScript:

index.tsx
class Node {
  constructor(element) {
    this.element = element;
    this.next = null;
  }
}

class LinkedList {
  constructor() {
    this.head = null;
    this.size = 0;
  }
  add(element) {
    let node = new Node(element);

    if (this.head === null) {
      this.head = node;
    } else {
      let current = this.head;
      while (current.next) {
        current = current.next;
      }
      current.next = node;
    }
    this.size++;
  }
  printList() {
    let current = this.head;
    let str = "";
    while (current) {
      str += current.element + " ";
      current = current.next;
    }
    return str.trim();
  }
  sort() {
    let n = this.size;
    for(let i = 0; i < n-1; i++) {
      let swapped = false;
      let current = this.head;
      let prev = null;
      for(let j = 0; j < n-i-1; j++) {
        if (current.element > current.next.element) {
          // swap current and current.next
          let temp = current.next;
          current.next = temp.next;
          temp.next = current;
          if (prev === null) {
            this.head = temp;
          } else {
            prev.next = temp;
          }
          prev = temp;
          swapped = true;
        } else {
          prev = current;
          current = current.next;
        }
      }
      if(swapped === false)
        break;
    }
    return this;
  }
}

// Usage
let linkedList = new LinkedList();
linkedList.add(64);
linkedList.add(34);
linkedList.add(25);
linkedList.add(12);
linkedList.add(22);
linkedList.add(11);
linkedList.add(90);
console.log(linkedList.sort().printList()); // Output: 11 12 22 25 34 64 90
1621 chars
77 lines

gistlibby LogSnag