Bubble sort

mail@pastecode.io avatar
unknown
python
2 months ago
497 B
10
Indexable
Never
import random
import time

def bubbleSort(arr):
    n = len(arr)
    swapped = False
    for i in range(n-1):
        for j in range(0, n-i-1):
            if arr[j] > arr[j + 1]:
                swapped = True
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
         
        if not swapped:
            return

for _ in range(10):
    arr = random.sample(range(1,10001), 10000)
    start_time = time.time()
    bubbleSort(arr)
    fin_time = time.time()
    print(fin_time - start_time)
Leave a Comment