Bubble Sort is a simple sorting method. It repeatedly compares adjacent elements in a list and swaps them if they are in the wrong order. This process continues until the list is sorted.
def swap(arr, left_pos, right_pos): temp = arr[left_pos] arr[left_pos] = arr[right_pos] arr[right_pos] = temp def bubble_sort(arr): n = len(arr) for _ in range(n): swapped = False for idx in range(n - 1): if arr[idx] > arr[idx + 1]: swap(arr, idx, idx + 1) swapped = True # If no elements were swapped, the list is already sorted if not swapped: break
To swap two values in Python, you can use a simple function that exchanges the values of two positions in a list.
def swap(arr, pos_1, pos_2): tmp = arr[pos_1] arr[pos_1] = arr[pos_2] arr[pos_2] = tmp
Bubble Sort works by repeatedly stepping through the list, comparing adjacent elements, and swapping them if they are in the wrong order. Each pass through the list places the next largest value in its correct position.
num1, num2 = num2, num1 # swaps values of num1 and num2
Bubble Sort is not the fastest. It compares each element with its neighbor and takes a lot of time if the list is long. Its performance is described as O(N^2), which means it can be slow for large lists.
num1, num2 = num2, num1 # swapping values in Bubble Sort
Bubble Sort uses two loops: an outer loop to go through each item in the list, and an inner loop to compare and swap adjacent items. The time it takes grows with the square of the list size, which is why it's slow for large lists.
Merge Sort is a method that splits the list into smaller parts, sorts each part, and then merges them back together. It continues splitting and merging until the entire list is sorted.
1) Split the list into smaller lists until each list has one element. 2) Merge these single-element lists into sorted lists of two elements, then four, and so on.
Merge Sort has two main steps: splitting the list and merging the sorted sub-lists. The merging part compares elements from two lists and combines them into a sorted list. This process continues until the entire list is sorted.
def merge_sort(lst): if len(lst) <= 1: return lst middle = len(lst) // 2 left = lst[:middle] right = lst[middle:] sleft = merge_sort(left) sright = merge_sort(right) return merge(sleft, sright) def merge(left, right): result = [] while left and right: if left[0] < right[0]: result.append(left.pop(0)) else: result.append(right.pop(0)) result += left + right return result
Merge Sort splits the list into smaller pieces and then merges them back together in sorted order. This process is efficient, with a runtime of Θ(N log N) for best, worst, and average cases.
Quicksort can be slow if it always picks the worst pivot for partitioning the list. This happens if the pivot always splits the list unevenly, causing many recursive calls and making the sorting time O(N^2).
Quicksort sorts by choosing a pivot element, then partitioning the list into two sub-lists – one with elements smaller than the pivot and one with elements larger. It then sorts each sub-list recursively.
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!