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.)
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 ::
Write a Python function to implement the quick sort algorithm over a singly linked list. The...
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 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 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 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...
(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. 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 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
5.8 Sort Linked List Use mergersort to sort a linked list Do not use the following libraries: algorithm, cmath. Do not load the values into a vector and sort them. The sort must be done with a linked list Example Two lists 6->3->1->2->4->5 will return a list with 1->2->3->4->5->6. Input There will be no input read in anymore. Going forward use the hard-coded input on the template and ensure the program works. There will be one compare output test based...
use python Write a function that will sort a given list using merge sort. You must use the merge sort algorithm (but may be recursive or iterative). The function will take a list as an input and return a sorted version of the list (you may assume it will be a list of integers). The method signature must be merge_sort(lst).
Build a singly linked list with 10 noes. The data are "This is my first project in Data structure and algorithm". First node is "This" second "is" and so on. Build insert function, insert "the " before "Data" and delete function, delete "my" before "first". Search "in" and output it location and then append "C++" and output the linked list again. In writing this program use menu which includes ”1. Create; 2. Insert; 3. Delete; 4. Append; 5. Search; 6....