Rennie44/Sorting-Algorithm-Visualization
0
1"""2Selection sort step-by-step for visualization.3"""4 5def selection_sort_steps(arr):6 steps = []7 a = arr.copy()8 9 n = len(a)10 steps.append({"array": a.copy(), "compare": None, "swapped": None, "swapped_indices": None})11 12 for i in range(n):13 min_index = i14 15 for j in range(i+1, n):16 # Comparing current j with min17 steps.append({18 "array": a.copy(),19 "compare": (min_index, j),20 "swapped": False,21 "swapped_indices": None22 })23 24 if a[j] < a[min_index]:25 min_index = j26 27 # Swap smallest to front28 if min_index != i:29 a[i], a[min_index] = a[min_index], a[i]30 steps.append({31 "array": a.copy(),32 "compare": (i, min_index),33 "swapped": True,34 "swapped_indices": (i, min_index)35 })36 else:37 # No swap38 steps.append({39 "array": a.copy(),40 "compare": None,41 "swapped": False,42 "swapped_indices": None43 })44 45 steps.append({"array": a.copy(), "compare": None, "swapped": None, "swapped_indices": None})46 return steps47 