def merge(q1,q2): nonlocal refPoint res = LinkedQueue() while q1.isEmpty() == False and q2.isEmpty() == False: if q1.front().distance(refPoint) < q2.front().distance(refPoint): res.enqueue(q1.dequeue()) else: res.enqueue(q2.dequeue()) while q1.isEmpty() == False: res.enqueue(q1.dequeue()) while q2.isEmpty() == False: res.enqueue(q2.dequeue()) return res
def pick_pivot(queue): """ :param queue: Type LinkedQueue queue :return: Pivot element Type: Node.val """ new_queue = LinkedQueue() new_queue.enqueue(queue.head.val) new_queue.enqueue(queue.tail.val) new_queue.enqueue(queue.get_middle()) insertion_sort(new_queue) return new_queue.head.val
def mergeSort(q): if len(q) == 1: return q size = len(q) half = size//2 left_q = LinkedQueue() right_q = LinkedQueue() for i in range(0,half): left_q.enqueue(q.dequeue()) for i in range(half,size): right_q.enqueue(q.dequeue()) left_q = mergeSort(left_q) right_q = mergeSort(right_q) return merge(left_q,right_q)
#!/usr/bin/python3 from QuickSort import quick_sort from Queue import LinkedQueue def AddValues(queue, fp): ''' :param queue: queue to add values to :param fp: file pointer to read from :return: None ''' for num in fp: queue.enqueue( float(num)) if __name__ == '__main__': fp = open(input("Please Enter File Name: "), "r") queue = LinkedQueue() AddValues(queue, fp) s = queue.get_middle() fp.close() quick_sort(queue) print(queue)
def quick_sort(S): ''' sorts list S ''' n = len(S) if n > 10: p = pick_pivot(S) L = LinkedQueue() E = LinkedQueue() G = LinkedQueue() while not S.is_empty(): initial = S.head.val if initial< p: L.enqueue(S.dequeue()) elif p < initial: G.enqueue(S.dequeue()) else: E.enqueue(S.dequeue()) quick_sort(L) quick_sort(G) while not L.is_empty(): S.enqueue(L.dequeue()) while not E.is_empty(): S.enqueue(E.dequeue()) while not G.is_empty(): S.enqueue(G.dequeue()) else: insertion_sort(S)
for i in range(half,size): right_q.enqueue(q.dequeue()) left_q = mergeSort(left_q) right_q = mergeSort(right_q) return merge(left_q,right_q) result_q = mergeSort(q) while result_q.isEmpty() == False and toPrint: print(str(result_q.dequeue())) print("To Check Algorith Correctness: ") n = 0 q = LinkedQueue() print('Enter the number of points: ') n = int(input()) print('Enter the points[x,y]: ') for i in range(n): x,y = tuple(map(float,input().split())) q.enqueue(Point(x,y)) print('Enter the referance point[x,y]: ') x,y = tuple(map(float,input().split())) refPoint = Point(x,y) solve(q,refPoint)
#!/usr/bin/python3 from QuickSort import quick_sort from Queue import LinkedQueue def AddValues(queue, fp): ''' :param queue: queue to add values to :param fp: file pointer to read from :return: None ''' for num in fp: queue.enqueue( float(num)) if __name__ == '__main__': fp = open(input("Please Enter File Name: "), "r") queue = LinkedQueue() AddValues(queue, fp) fp.close() quick_sort(queue) print(queue)
def quick_sort(queue): """ :param queue: :return: """ size = len(queue) if size <=10: insertion_sort(queue) else: p = pick_pivot(queue) lesser_list = LinkedQueue() greater_list = LinkedQueue() equal_list = LinkedQueue() while not queue.is_empty(): if queue.head.val < p: lesser_list.enqueue(queue.dequeue()) elif queue.head.val > p: greater_list.enqueue(queue.dequeue()) else: equal_list.enqueue(queue.dequeue()) quick_sort(lesser_list) quick_sort(greater_list) while not lesser_list.is_empty(): queue.enqueue(lesser_list.dequeue()) while not equal_list.is_empty(): queue.enqueue(equal_list.dequeue()) while not greater_list.is_empty(): queue.enqueue(greater_list.dequeue())
def quick_sort(queue): """ precondition: queue to be sorted postcondition: sorts the nodes in a queue in ascending order using the quick sort algorithm """ #code mostly from D2L slide example if len(queue) <= 10: insertion_sort(queue) #use insertion sort for queues smaller than 11 else: pivot = Node( pick_pivot(queue), None) #pick pivot point and make pivot node for comparison L = LinkedQueue( ) #create queues for less than pivot, equal to, and greater than E = LinkedQueue() G = LinkedQueue() while not queue.is_empty(): #sort elements into respective queues if queue.left() < pivot: L.enqueue(queue.dequeue()) elif pivot < queue.left(): G.enqueue(queue.dequeue()) else: E.enqueue(queue.dequeue()) quick_sort( L ) #recursively run quicksort to sort less than and greater than queues quick_sort(G) while not L.is_empty( ): #combine sorted queues for complete sorted queue queue.enqueue(L.dequeue()) while not E.is_empty(): queue.enqueue(E.dequeue()) while not G.is_empty(): queue.enqueue(G.dequeue())
def quick_sort(queue): ''' :param queue: desired queue to be sorted :return: None ''' if len(queue) <= 10: insertion_sort(queue) return pivot = pick_pivot(queue, queue.head.val, queue.tail.val) # pick pivot lesser = LinkedQueue() # initialize lesser, greater, and equal queues greater = LinkedQueue() equal = LinkedQueue() while not queue.is_empty(): # divide Queue up based on pivot if queue.head.val > pivot: greater.enqueue(queue.dequeue()) elif queue.head.val < pivot: lesser.enqueue(queue.dequeue()) else: equal.enqueue(queue.dequeue()) quick_sort(greater) # partition greater queue quick_sort(lesser) # partition lesser queue while not lesser.is_empty(): # concatenate the partitioned queues queue.enqueue(lesser.dequeue()) while not equal.is_empty(): queue.enqueue(equal.dequeue()) while not greater.is_empty(): queue.enqueue(greater.dequeue())