Как реализовать bubble sort в связанном списке?



@Dyadko_Orest

пытаюсь реализовать Bubble sort для связанного списка в JavaScript. Никак не получается сделать. Искал подобные вопросы, но находил в реализацию только на C++ или Java. Буду благодарен за помощь. Внизу реализация списка.
LinkedListNode
export class LinkedListNode {
  public value;
  public prev;
  public next;

  constructor(value) {
    this.value = value;
    this.next = null;
    this.prev = null;
  }
}

LinkedList

import { LinkedListNode } from './LinkedListNode';

export class LinkedList {
  private head;
  private tail;

  addHeadNode = (value) => {
    const newLinkendListNode = new LinkedListNode(value);

    if (!this.head) {
      this.head = newLinkendListNode;
      this.tail = newLinkendListNode;
    } else {
      this.head.prev = newLinkendListNode;
      newLinkendListNode.next = this.head;
      this.head = newLinkendListNode;
    }
  };

  addTailNode = (value) => {
    const newLinkendListNode = new LinkedListNode(value);

    if (!this.tail) {
      this.head = newLinkendListNode;
      this.tail = newLinkendListNode;
    } else {
      this.tail.next = newLinkendListNode;
      newLinkendListNode.prev = this.tail;
      this.tail = newLinkendListNode;
    }
  };

  getByIndex = index => {
    let currentNode = this.head;
    let count = 0;

    while (currentNode) {
      if (count === index) {
        console.log(currentNode);
        return currentNode;
      }
      count++;
      currentNode = currentNode.next;
    }
    return -1;
  };
}


Решения вопроса 0


Ответы на вопрос 1



@IonDen

В вашем классе для создания связанного списка недостаточно методов для этого.
Вам понадобится добавить еще методов, например swap, чтобы менять ноды местами.
Алгоритм то простецкий: https://www.youtube.com/watch?v=9I2oOAr2okY

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *