sort items in array by asc in python
Code snippet on how to sort items in array by asc in python
array = [5, 1, 2, 4, 3]
for i in range(len(array)):
min_index = i
for j in range(i+1, len(array)):
if array[min_index] > array[j]:
min_index = j
array[i], array[min_index] = array[min_index], array[i]
print(array)
This code sorts the items in an array in ascending order using selection sort. The outer ‘for’ loop will iterate through each item in the array. For each element in the array, the inner ‘for’ loop will search for the minimum value and store the index of the minimum value in the min_index variable. Then, the elements at index i and min_index will be swapped. At the end of the code, the array would be printed with the items in ascending order.