0% found this document useful (0 votes)
11 views2 pages

Separate LinkedLists Sort - Py

The document contains Python code for implementing and sorting doubly and singly linked lists. The doubly linked list uses Bubble Sort for sorting, while the singly linked list employs Selection Sort. Example tests demonstrate the sorting of both types of lists, resulting in ordered outputs.

Uploaded by

Brahian candamil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Separate LinkedLists Sort - Py

The document contains Python code for implementing and sorting doubly and singly linked lists. The doubly linked list uses Bubble Sort for sorting, while the singly linked list employs Selection Sort. Example tests demonstrate the sorting of both types of lists, resulting in ordered outputs.

Uploaded by

Brahian candamil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

17/4/25, 14:37 Separate_LinkedLists_Sort.

py - Colab

# --- Lista Doble con Bubble Sort ---


class NodeDoubly:
"""Nodo para lista doblemente enlazada."""
def __init__(self, data):
self.data = data
self.prev = None
self.next = None

class DoublyLinkedList:
"""Implementación de lista doblemente enlazada."""
def __init__(self):
self.head = None

def append(self, data):


"""Añadir un nodo al final de la lista."""
new_node = NodeDoubly(data)
if not self.head:
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node
new_node.prev = current

def bubble_sort(self):
"""Ordenamiento Bubble Sort."""
if not self.head:
return
swapped = True
while swapped:
swapped = False
current = self.head
while current.next:
if current.data > current.next.data:
current.data, current.next.data = current.next.data, current.data
swapped = True
current = current.next

def display(self):
"""Mostrar la lista."""
elements = []
current = self.head
while current:
elements.append(current.data)
current = current.next
return elements

# --- Lista Simple con Selection Sort ---


class NodeSingly:
"""Nodo para lista simplemente enlazada."""
def __init__(self, data):
self.data = data
self.next = None

class SinglyLinkedList:
"""Implementación de lista simplemente enlazada."""
def __init__(self):
self.head = None

def append(self, data):


"""Añadir un nodo al final de la lista."""
new_node = NodeSingly(data)
if not self.head:
self.head = new_node
else:
current = self.head
while current.next:
current = current.next
current.next = new_node

def selection_sort(self):
"""Ordenamiento Selection Sort."""
current = self.head
while current:
smallest = current
https://colab.research.google.com/drive/1JkEw NK7hfpFz-vCx207-cb2b5tStxrF1?authuser=0#scrollTo=S5dvk3mbQGi6&printMode=true 1/2
17/4/25, 14:37 Separate_LinkedLists_Sort.py - Colab
next_node = current.next
while next_node:
if next_node.data < smallest.data:
smallest = next_node
next_node = next_node.next
current.data, smallest.data = smallest.data, current.data
current = current.next

def display(self):
"""Mostrar la lista."""
elements = []
current = self.head
while current:
elements.append(current.data)
current = current.next
return elements

# --- Prueba de listas separadas ---

# Prueba con la lista doblemente enlazada (Bubble Sort)


dll = DoublyLinkedList()
for value in [6, 3, 8, 5, 2]:
dll.append(value)
dll.bubble_sort()
print("Lista doblemente enlazada ordenada:", dll.display())

# Prueba con la lista simplemente enlazada (Selection Sort)


sll = SinglyLinkedList()
for value in [7, 4, 9, 1, 3]:
sll.append(value)
sll.selection_sort()
print("Lista simplemente enlazada ordenada:", sll.display())

Lista doblemente enlazada ordenada: [2, 3, 5, 6, 8]


Lista simplemente enlazada ordenada: [1, 3, 4, 7, 9]

https://colab.research.google.com/drive/1JkEw NK7hfpFz-vCx207-cb2b5tStxrF1?authuser=0#scrollTo=S5dvk3mbQGi6&printMode=true 2/2

You might also like