2nd with operations
unknown
plain_text
2 years ago
1.5 kB
9
Indexable
def bubble_sort(arr): n = len(arr) operations = 0 for i in range(n): for j in range(0, n-i-1): operations += 1 if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] return operations def selection_sort(arr): n = len(arr) operations = 0 for i in range(n): min_index = i for j in range(i+1, n): operations += 1 if arr[j] < arr[min_index]: min_index = j arr[i], arr[min_index] = arr[min_index], arr[i] return operations def insertion_sort(arr): n = len(arr) operations = 0 for i in range(1, n): key = arr[i] j = i - 1 while j >= 0 and key < arr[j]: operations += 1 arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key return operations input_list = [64, 34, 25, 12, 22, 11, 90] bubble_sorted = input_list.copy() selection_sorted = input_list.copy() insertion_sorted = input_list.copy() bubble_operations = bubble_sort(bubble_sorted) print("Bubble Sort:", bubble_sorted) print("Bubble Sort Operations:", bubble_operations) selection_operations = selection_sort(selection_sorted) print("Selection Sort:", selection_sorted) print("Selection Sort Operations:", selection_operations) insertion_operations = insertion_sort(insertion_sorted) print("Insertion Sort:", insertion_sorted) print("Insertion Sort Operations:", insertion_operations)
Editor is loading...