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
  • please help with python Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection...

    please help with python Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection Sort segments the list into sorted and unsorted elements. The algorithm continues to remove the smallest element of the unsorted list and adds it to the sorted segment. Implement the algorithm that accepts an unsorted list and returns a sorted one - in ascending order. 5 3 8 4 6 Initial List Minimum Swaps Position: Sorted List Length 1 Minimum Swaps Position: Sorted List...

  • Question 5 (10 Points) Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection-Sort...

    Question 5 (10 Points) Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection-Sort segments the list into sorted and unsorted elements. The algorithm continues to remove the smallest element of the unsorted list and adds it to the sorted segment. Implement the algorithm that accepts an unsorted list and returns a sorted one - in ascending order. 5 3 8 Initial List 4 6 18 4 6 Minimum Swaps Position: Sorted List Length 1 Minimum Swaps Position:...

  • how to implement a linked list object using only singly linked list toolkit. Then implement a...

    how to implement a linked list object using only singly linked list toolkit. Then implement a FREQUENCY function to count the ovcurrence of each element in the list. task#1: Add = operator to node: implement the assignment operator for the node such that setting a node = overwrites the value in the node with the value. task#2:Linked List class implement the methods of linked list. insert search and locate remove node* operator [] task#3: Implement the Frequency

  • Exercise-1:15 points Develop a node class and a singly list class. The node class should have two...

    Exercise-1:15 points Develop a node class and a singly list class. The node class should have two state variables namely data and nextNode. The singly list class should contain the following methods . Middlelnsert-insert a node somewhere in the middle of the list . Startinsert-insert a node at start of the Linked list Endinsert-insert a node at the end of the Linked list . Delete-delete a node Traverse-prints all the node's data Reverse -reverses the linked list Note: Choose appropriate...

  • Programming Assignment 1 Data structure Java Implement Shellsort for a linked list, based on a variant...

    Programming Assignment 1 Data structure Java Implement Shellsort for a linked list, based on a variant of bubble sort. The rather severe constraints imposed by the singly-linked list organization presents special problems for implementing Shellsort. Your task is to overcome these constraints and develop a simple, elegant implementation that does not sacrifice efficiency or waste space. Step 1. First, implement a routine to build a list: read integers from standard input, and build a list node for each item consisting...

  • You are given a pointer to a singly linked list. The singly linked list has cycles...

    You are given a pointer to a singly linked list. The singly linked list has cycles due to a programming error. Write a C program to detect whether the linked list has cycles without storing all the pointers to the nodes. The function detect_cycles should return 1 when a cycle is found and 0 when there are no cycles. Your code should handle all corner cases carefully and should not cause segmentation fault. Figure shows various examples of cycles for...

  • C# is the language Create a generic class called “LinkedList” and implement the singly linked list....

    C# is the language Create a generic class called “LinkedList” and implement the singly linked list. Implement the following functions: - In C# Function Name Function Parameters Description Empty - Return true if empty else return false. Size - Return the current size of the list. Insert Value Insert the Value at the end of the list. Insert Index, Value Insert the Value at the specified Index. If the Index is out of range then insert the Value at the...

  • (20 points) ) Write a Python program which merges two sorted singly linked lists and return...

    (20 points) ) Write a Python program which merges two sorted singly linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Example: Input: 1->2- >4, 1->3->4 Output: 1->1->2->3 ->4->4

  • Objective: Implement a sorting algorithm. Description: Implement a radix sort in a Java class named RadixSort.java....

    Objective: Implement a sorting algorithm. Description: Implement a radix sort in a Java class named RadixSort.java. Your program should receive its input from a file named "input.txt", which contains one integer per line. It should produce a sorted output file named "output.txt". Include a main method which demonstrates that your algorithm works.

  • In python - Implement a doubly linked circular linked list of Node objects called CircularDoublyLinkedList. The...

    In python - Implement a doubly linked circular linked list of Node objects called CircularDoublyLinkedList. The data of each Node in the list is an integer. You will collect measurements for: An already sorted linked list An already sorted linked list in descending order A linked list containing random data

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