Write me some pseudocode for one pass of the Selection Sort algorithm through random values stored in an Unordered-List, or Linked-List, of values (rather than a standard "Python" List).
• How would you implement a swap using a Linked-List?
• What is the minimum number of variables required?

Respuesta :

Answer:

Minimum of 6 variables is required

Explanation:

class LinkedList:

def __init__(self, v):

self.data = v

self.next = None

def sortSelections(first):  

x = y = first

while y.next:

z = p = y.next  

while p:

if y.data > p.data:

if y.next == p:

if y == first:

y.next = p.next

p.next = y

y, p = p, y

z = p

first = y

p = p.next

else:

y.next = p.next

p.next = y

x.next = p

y, p = p, y

z = p

p = p.next

else:

if y == first:

r = y.next

y.next = p.next

p.next = r

z.next = y

y, p = p, y

z = p

p = p.next

first = y

else:

r = y.next

y.next = p.next

p.next = r

z.next = y

x.next = p

y, p = p, y

z = p  

p = p.next

else:  

z = p

p = p.next

x = y

y = y.next

return first

def display(first):

while first:

print(first.data, end = " ")

first = first.next

if __name__ == "__main__":

print("Sorted List: ")

first = LinkedList(53)

first.next = LinkedList(14)

first.next.next = LinkedList(2)

first.next.next.next = LinkedList(9)

first.next.next.next.next = LinkedList(92)

first = sortSelections(first)

display(first)