###########################################[br]#12 Algoritmo de clasificación en Python[br]#1. Algoritmo de clasificación de Burbujas:[br]# (Bubble sort algorithm)[br]def bubble_sort(arr):[br] n = len(arr)[br] for i in range(n):[br] for j in range(0, n-i-1):[br] if arr[j] > arr[j+1]:[br] arr[j], arr[j+1] = arr[j+1], arr[j][br] return arr[br][br]#2. Algoritmo de clasificación de Combinar:[br]# (Merge sort algorithm)[br]def merge_sort(arr):[br] if len(arr) > 1:[br] mid = len(arr) // 2[br] left_half = arr[:mid][br] right_half = arr[mid:][br] merge_sort(left_half)[br] merge_sort(right_half)[br] i = j = k = 0[br] while i < len(left_half) and j < len(right_half):[br] if left_half[i] < right_half[j]:[br] arr[k] = left_half[i][br] i += 1[br] else:[br] arr[k] = right_half[j][br] j += 1[br] k += 1[br] while i < len(left_half):[br] arr[k] = left_half[i][br] i += 1[br] k += 1[br] while j < len(right_half):[br] arr[k] = right_half[j][br] j += 1[br] k += 1[br] return arr[br][br]#3. Algoritmo de Búsqueda Binaria:[br]# (Binary search algorithm)[br]def binary_search(arr, x):[br] low = 0[br] high = len(arr) - 1[br] while low <= high:[br] mid = (low + high) // 2[br] if arr[mid] < x:[br] low = mid + 1[br] elif arr[mid] > x:[br] high = mid - 1[br] else:[br] return mid[br] return -1[br][br]#4. Algoritmo de Búsqueda Lineal:[br]# (Linear search algorithm)[br]def linear_search(arr, x):[br] for i in range(len(arr)):[br] if arr[i] == x:[br] return i[br] return -1[br][br]#5. Algoritmo de clasificación por inserción: [br]# (Insertion sort algorithm)[br]def insertion_sort(arr):[br] for i in range(1, len(arr)):[br] key = arr[i][br] j = i - 1[br] while j >= 0 and key < arr[j]:[br] arr[j+1] = arr[j][br] j -= 1[br] arr[j+1] = key[br] return arr[br][br]#6. Algoritmo de clasificación rápida:[br]# (Quick sort algorithm)[br]def quick_sort(arr):[br] if len(arr) <= 1:[br] return arr[br] else:[br] pivot = arr[0][br] left = [][br] right = [][br] for i in range(1, len(arr)):[br] if arr[i] < pivot:[br] left.append(arr[i])[br] else:[br] right.append(arr[i])[br] return quick_sort(left) + [pivot] + quick_sort(right)[br][br]#7. Algoritmo de clasificación de selección[br]# (Selection sort algorithm)[br]def selection_sort(arr):[br] for i in range(len(arr)):[br] min_index = i[br] for j in range(i+1, len(arr)):[br] if arr[j] < arr[min_index]:[br] min_index = j[br] arr[i], arr[min_index] = arr[min_index], arr[i][br] return arr[br][br]#8. Ordene una lista de números en orden ascendente usando la clasificación de burbujas:[br]# Sort a list of numbers in ascending order using bubble sort:[br]def bubble_sort(arr):[br] n = len(arr)[br] for i in range(n):[br] for j in range(n-i-1):[br] if arr[j] > arr[j+1]:[br] arr[j], arr[j+1] = arr[j+1], arr[j][br] return arr[br][br]#9. Ordene una lista de números en orden ascendente usando la ordenación por selección:[br]# Sort a list of numbers in ascending order using selection sort:[br]def selection_sort(arr):[br] n = len(arr)[br] for i in range(n):[br] min_index = i[br] for j in range(i+1, n):[br] if arr[j] < arr[min_index]:[br] min_index = j[br] arr[i], arr[min_index] = arr[min_index], arr[i][br] return arr[br][br]#10. Ordene una lista de números en orden ascendente usando la ordenación por inserción:[br]# Sort a list of numbers in ascending order using insertion sort:[br]def insertion_sort(arr):[br] n = len(arr)[br] for i in range(1, n):[br] key = arr[i][br] j = i-1[br] while j >= 0 and arr[j] > key:[br] arr[j+1] = arr[j][br] j -= 1[br] arr[j+1] = key[br] return arr[br][br]#11. Ordene una lista de números en orden ascendente usando la ordenación por combinación:[br]# Sort a list of numbers in ascending order using merge sort:[br]def merge_sort(arr):[br] if len(arr) > 1:[br] mid = len(arr)//2[br] left_half = arr[:mid][br] right_half = arr[mid:][br] merge_sort(left_half)[br] merge_sort(right_half)[br] i = j = k = 0[br] while i < len(left_half) and j < len(right_half):[br] if left_half[i] < right_half[j]:[br] arr[k] = left_half[i][br] i += 1[br] else:[br] arr[k] = right_half[j][br] j += 1[br] k += 1[br] while i < len(left_half):[br] arr[k] = left_half[i][br] i += 1[br] k += 1[br] while j < len(right_half):[br] arr[k] = right_half[j][br] j += 1[br] k += 1[br] return arr[br][br]#12. Ordene una lista de números en orden ascendente usando la ordenación rápida:[br]# Sort a list of numbers in ascending order using quick sort:[br]def quick_sort(arr):[br] if len(arr) <= 1:[br] return arr[br] pivot = arr[0][br] left_half = [x for x in arr[1:] if x < pivot][br] right_half = [x for x in arr[1:] if x >= pivot][br] return quick_sort(left_half) + [pivot] + quick_sort(right_half)[br][br]#13. Quick Sort[br]quicksort = Y(lambda f: lambda x: ( f([item for item in x if item < x[0]])[br] + [y for y in x if x[0] == y] + f([item for item in x if item > x[0]])) if x else [])[br]quicksort([1, 3, 5, 4, 1, 3, 2])