Search

Merge Sort

Characteristics

1.
Has two index to track of
2.
If index1 > index2 switch order
Worst Case: O(nlogn)O(n\log n)
Average Case: O(nlogn)O(n\log n)
Best Case: O(nlogn)O(n\log n)

Instruction

1.
Split the unsorted array until it is not splittable
2.
Compare the values of the two sublists from the index 0
3.
Append the smaller value to the new list
4.
If either sublist is done comparing, merge the rest of the values from the leftover sublist
5.
Repeat 2 - 4 until sorted

Instructions for Merge Sort

import sys from load import load_numbers # load list of numbers numbers = load_numbers(sys.argv[i]) def merge_sort(values): if len(values) <= 1: return values middle_index = len(values) // 2 left_values = merge_sort(values[:middle_index]) right_values = merge_sort(values[middle_index:]) sorted_values = [] left_index = 0 right_index = 0 while left_index < len(left_values) and right_index < len(right_values): if left_values[left_index] < right_values[right_index]: sorted_values.append(left_values[left_index]) left_index += 1 else: sorted_values.append(right_values[right_index:] right_index += 1 sorted_values += left_values[left_index:] sorted_values += right_values[right_index:] return sorted_values
Python
복사

Instructions for Linked List Merge Sort

from linked_list import LinkedList # O(kn log n) def merge_sort(linked_list): if linked_list.size() == 1: return linked_list elif linked_list.heqad is None: return linked_list left_half, right_half = split(linked_list) left = merge_sort(left_half) right = merge_sort(right_half) return merge(left, right) # O(k log n) def split(linked_list): # split on a single node # if linked_list == None or linked_list.head == None: left_half = linked_list right_half = None return left_half, right_half else: size = linked_list.size() mid = size//2 # have to get one node before the mid node to remove next_node connection mid_node = linked_list.searchByIndex(mid - 1) left_half = linkded_list right_half = LinksedList() # add mid_node + 1 to the newly created right_half right_half.head = mid_node.next_node # remove right_half from the left_half mid_node.next_node = None return left_half, right_half # O(n) def merge(left, right): merged_list = LinkedList() # add fake head merged.add(0) current_node = merged.head left_head = left.head right_head = right_head while left_head or right_head: if left_head is None: current_node.next_node = right_head right_head = right_head.next_node elif right_head is None: current_node.next_node = left.head left_head. left_head.next_node else: left_data = left_head.data right_data = right_head.data if left_data < right_data: current_node.next_node = left_head else: current_node.next_node = right_hxffbead right_head = right_head.next_node current_node = current_node.next_node # remove fake head head = merged_list.head.next_node merged.head = head return merged_list
Python
복사
Split Recursively Sort Recursively Merge When Merge 1. left is empty 2. right is empty 3. have to compare Execute Code l = LinkedList() l.add(10) l.add(2) l.add(44) l.add(15) l.add(200) print(l) sorted_linked_list = merge_sort(l) print(sorted_linked_list) >>> [Head: 200]→ [15]→ [44]→ [2]→ [Tail: 10] >>> [Head: 2]→ [10]→ [15]→ [44]→ [Tail: 200]

Reference

Big O Notation - Full Course
This course will teach you how to understand and apply the concepts of Big O Notation to Software Engineering. Big-O notation is a way to describe how long an algorithm takes to run or how much memory is used by an algorithm. ✏️ This course was developed by selikapro. Check out his channel: https://www.youtube.com/channel/UC5UgemAz061hkjTFHOfxNpg 🔗 Twitter: https://twitter.com/selikapro 🔗 Instagram: https://www.instagram.com/selikapro ⭐️ Course Contents ⭐️ ⌨️ (0:00:00) Intro ⌨️ (0:00:39) What Is Big O? ⌨️ (0:07:08) O(n^2) Explanation ⌨️ (0:14:06) O(n^3) Explanation ⌨️ (0:26:29) O(log n) Explanation Recursive ⌨️ (0:31:12) O(log n) Explanation Iterative ⌨️ (0:36:08) O(log n) What Is Binary Search? ⌨️ (0:41:30) O(log n) Coding Binary Search ⌨️ (0:58:12) O(n log n) Explanation ⌨️ (1:02:50) O(n log n) Coding Merge Sort ⌨️ (1:17:04) O(n log n) Merge Sort Complexity Deep Dive ⌨️ (1:28:06) O(2^n) Explanation With Fibonacci ⌨️ (1:36:02) O(n!) Explanation ⌨️ (1:47:19) Space Complexity & Common Mistakes ⌨️ (1:55:53) End 🎉 Thanks to our Champion and Sponsor supporters: 👾 Wong Voon jinq 👾 hexploitation 👾 Katia Moran 👾 BlckPhantom 👾 Nick Raker 👾 Otis Morgan 👾 DeezMaster 👾 Treehouse 👾 AppWrite -- Learn to code for free and get a developer job: https://www.freecodecamp.org Read hundreds of articles on programming: https://freecodecamp.org/news
Algorithms and Data Structures Tutorial - Full Course for Beginners
In this course you will learn about algorithms and data structures, two of the fundamental topics in computer science. There are three main parts to this course: algorithms, data structures, and a deep dive into sorting and searching algorithms. By the end, you will understand what algorithms and data structures are, how they are measured and evaluated, and how they are used to solve problems. This course was developed by Pasan Premaratne and Jay McGavren. It was made possible by a grant from teamtreehouse.com ⭐️ Course Contents ⭐️ ⌨️ (0:00:00) Introduction to Algorithms ⌨️ (1:57:44) Introduction to Data Structures ⌨️ (4:11:02) Algorithms: Sorting and Searching ⭐️ Code Snippets for Course ⭐️ 💻 Introduction to Algorithms: ⌨️ Algorithms in Code: 🔗 Linear Search Implementations: https://teamtreehouse.com/library/introduction-to-algorithms/algorithms-in-code/linear-search-implementations 🔗 Binary Search Implementations: https://teamtreehouse.com/library/introduction-to-algorithms/algorithms-in-code/binary-search-implementations 💻 Introduction to Data Structures ⌨️ Exploring Arrays: 🔗 Array Characteristics and Storage: https://teamtreehouse.com/library/introduction-to-data-structures/exploring-arrays/array-characteristics-and-storage 🔗 Operations on Arrays: https://teamtreehouse.com/library/introduction-to-data-structures/exploring-arrays/operations-on-arrays ⌨️ Building a Linked List: 🔗 Singly and Doubly Linked Lists: https://teamtreehouse.com/library/introduction-to-data-structures/building-a-linked-list/singly-and-doubly-linked-lists-2 🔗 Linked List Operations: https://teamtreehouse.com/library/introduction-to-data-structures/building-a-linked-list/linked-lists-operations ⌨️ The Merge Sort Algorithm: 🔗 Merge Sort Implementations: https://teamtreehouse.com/library/introduction-to-data-structures/the-merge-sort-algorithm/merge-sort-implementations 🔗 Alternate Versions of Merge Sort: https://teamtreehouse.com/library/introduction-to-data-structures/the-merge-sort-algorithm/alternate-versions-of-merge-sort ⌨️ Merge Sort and Linked Lists: 🔗 Implementing Merge Sort on Linked Lists: https://teamtreehouse.com/library/introduction-to-data-structures/merge-sort-and-linked-lists/implementing-merge-sort-on-linked-lists 💻 Algorithms: Sorting and Searching ⌨️ Sorting Algorithms: 🔗 Code for Bogosort: https://teamtreehouse.com/library/algorithms-sorting-and-searching/sorting-algorithms/code-for-bogosort 🔗 Code for Selection Sort: https://teamtreehouse.com/library/algorithms-sorting-and-searching/sorting-algorithms/code-for-selection-sort 🔗 Code for Quicksort: https://teamtreehouse.com/library/algorithms-sorting-and-searching/sorting-algorithms/code-for-quicksort 🔗 Code for Merge Sort: https://teamtreehouse.com/library/algorithms-sorting-and-searching/sorting-algorithms/code-for-merge-sort ⌨️ Searching Names: 🔗 Code for Linear Search: https://teamtreehouse.com/library/algorithms-sorting-and-searching/searching-names/code-for-linear-search 🔗 Code for Binary Search: https://teamtreehouse.com/library/algorithms-sorting-and-searching/searching-names/code-for-binary-search -- Learn to code for free and get a developer job: https://www.freecodecamp.org Read hundreds of articles on programming: https://freecodecamp.org/news