Question

Write a Python function to implement the quick sort algorithm over a singly linked list. The...

Write a Python function to implement the quick sort algorithm over a singly linked list. The input of your function should be a reference pointing to the first node of a linked list, and the output of your function should also be a reference to the first node of a linked list, in which the data have been sorted into the ascending order. (You may use the LinkedQueue class we introduced in the lecture directly in your program.)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please please thumbs up!!!

Hope it will help uh ouT!!!!!!

Code ::

(IN PYTHON LANGUAGE)

-----------------------------------------

class LinkedList:

class _Node:

def __init__(self, newData, newNext):

self._data = newData

self._next = newNext

  

def __init__(self):

self.__head = None

self.__size = 0

def __len__(self):

return self.__size

def is_empty(self):

return self.__size == 0

def remove(self, _data):

if(self.is_empty()):

print('List is empty.')

return

current = self.__head

prev = None

while current != None and current._data != _data:

prev = current

current = current._next

if current == None:

return

if prev == None:

self.__head = current._next

else:

prev._next = current._next

self.__size -= 1

def add(self, e):

new_node = self._Node(e, None)

if self.__head == None:

self.__head = new_node

else:

current = self.__head

while current._next != None:

current = current._next

current._next = new_node

self.__size += 1

def outputList(self):

if(self.is_empty()):

print('List is empty.')

return

current = self.__head

while current != None:

print(current._data, end = ' ')

current = current._next

print()

# quick_sort() function implementation

def quick_sort(self):

if self.__head is None:

return None

last = self.__head

while last != None and last._next != None:

last = last._next

first, last = self.quick_sort_helper(self.__head, last)

last._next = None

self.__head = first

def quick_sort_helper(self, first, last):

if first is not last:

first_prev, last_prev, first_ref, last_ref, first_next, last_next = self.partition(first, last)

if first_prev is None:

first = first_ref

else:

first_prev, last_prev = self.quick_sort_helper(first_prev, last_prev)

first = first_prev

last_prev._next = first_ref

if first_next is None:

last = last_ref

else:

first_next, last_next = self.quick_sort_helper(first_next, last_next)

last_ref._next = first_next

last = last_next

return first, last

def partition(self, first, last):

temp_node = last

first_ref, last_ref = temp_node, temp_node

first_prev, last_prev, first_next, last_next = None, None, None, None

dummy_node = self._Node(None, None)

dummy_node._next = first

current = dummy_node

while current._next is not last:

current = current._next

if current._data > temp_node._data:

if first_next is not None:

last_next._next = current

last_next = current

else:

first_next = current

last_next = current

elif current._data < temp_node._data:

if first_prev is not None:

last_prev._next = current

last_prev= current

else:

first_prev = current

last_prev = current

else:

last_ref._next = current

last_ref = current

return first_prev, last_prev, first_ref, last_ref, first_next, last_next

# main() function implementation

if __name__ == '__main__':

# create an _data for LinkedList class

myList = LinkedList()

  

# call add() function

myList.add(78)

myList.add(69)

myList.add(25)

myList.add(45)

myList.add(35)

myList.add(75)

# call outputList() function

print("List before sorting: ", end = '')

myList.outputList()

# call quick_sort() function

myList.quick_sort()

# call outputList() function

print("List after sorting: ", end = '')

myList.outputList()

---------------------------------------

Output ::

0 % -/Desktop/CODE/demo.py (CODE) - Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Project Preferencese X -/Desktop/CODE/demo.py (CODE) - Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Project Preferencese --/Desktop/CODE/demo.py (CODE) - Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Project Preferences H# call quick sort() function myList.quick_sort() 132 133 134 135 136 137 138 # call outputList() function print(List after snikhil@nikhil-Vostro-15-3568: -/Desktop/CODE File Edit View Search Terminal Help nikhil@nikhil-Vostro-15-3568:-/Desktop/CODE$

Add a comment
Know the answer?
Add Answer to:
Write a Python function to implement the quick sort algorithm over a singly linked list. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT