<jupyter_start><jupyter_text>Sorting Algorithms made Pythonic  Q1. Given an unordered list, sort it using the classical bubble sort<jupyter_code>a = [7, 8, 6, 3, 4, 55, 2, 1, 4, 6, 8, 7]
def bubble_sort(arr):
    for i,_ in enumerate(arr):
        for j in range(0, len(arr)-1):
            (arr[j], arr[j+1]) = (arr[j+1], arr[j]) if arr[j] > arr[j + 1] else (arr[j], arr[j+1])
bubble_sort(a)
a<jupyter_output><empty_output><jupyter_text>Q2. Given an unordered list, sort it using the classical selection sort<jupyter_code>a = [7, 8, 6, 3, 4, 55, 2, 1, 4, 6, 8, 7]
def selection_sort(arr):
    for i,_ in enumerate(arr):
        for j in range(i + 1, len(arr)):
            (arr[i], arr[j]) = (arr[j], arr[i]) if arr[i] > arr[j] else (arr[i], arr[j])
selection_sort(a)
a<jupyter_output><empty_output><jupyter_text>Q3. Given an unordered list, sort it using the classical insertion sort<jupyter_code>a = [7, 8, 6, 3, 4, 55, 2, 1, 4, 6, 8, 7]
def insertion_sort(arr):
    for i in range(1, len(arr)):
        hole = arr[i]
        j = i
        while j > 0 and arr[j - 1] > hole:
            arr[j] = arr[j - 1]
            j -= 1
        arr[j] = hole
insertion_sort(a)
a<jupyter_output><empty_output><jupyter_text>Q4. Given an unordered list, sort it using the classical merge sort<jupyter_code>a = [7, 8, 6, 3, 4, 55, 2, 1, 4, 6, 8, 7]
def merge_sort(arr):
    if len(arr) <= 1:
        return arr 

    left, right = arr[:len(arr)//2], arr[len(arr)//2:]
    left = merge_sort(left)
    right = merge_sort(right)
    # merge
    i = j = 0
    c = []
    while(i < len(left) and j < len(right)):
        c.append(left[i] if left[i] < right[j] else right[j])
        i, j = i + 1 if left[i] <= right[j] else i, j + 1 if right[j] < left[i] else j
    else:
        return (c + left[i:] if i != len(left) else c + right[j:]) if len(c) != len(left) + len(right) else c
merge_sort(a)<jupyter_output><empty_output><jupyter_text>Q5. Given an unordered list, sort it using the classical quick sort<jupyter_code>def quick_sort(arr):
    if len(arr) <= 1:
        return arr
    pivot = arr[len(arr)//2]
    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]
    return quick_sort(left) + middle + quick_sort(right)
quick_sort(a)<jupyter_output><empty_output><jupyter_text>Q6. Given an unordered list, sort it using the randomized quick sort<jupyter_code>import random
def randomized_quick_sort(arr):
    if len(arr) <= 1:
        return arr
    pivot = arr[random.randint(0, len(arr)-1)]
    left = [x for x in arr if x < pivot]
    middle = [x for x in arr if x == pivot]
    right = [x for x in arr if x > pivot]
    return quick_sort(left) + middle + quick_sort(right)
randomized_quick_sort(a)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14879885263535317, 'fraction_numerical': 0.029042667622803872, 'mean_word_length': 3.2018072289156625, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '94310', 'license': 'MIT', 'n_tokens_mistral': 1158, 'n_tokens_neox': 1079, 'n_words': 341, 'path': 'Pythonic Sorting.ipynb', 'repo_name': 'AnshumaanDash/Sorting-Algorithms-Made-Pythonic', 'size': '2789'}