Python Array Module Selection Sort
# Write a Python program # to input n numbers in # python array and sort them # in ascending order using selection sort algorithm # Perfect Python Programming Tutorials # Author : www.EasyCodebook.com (c) import array # define a function selection_sort() def selection_sort(arr): n = len(arr) for i in range(n): min_index = i for j in range(i+1, n): if arr[min_index] > arr[j] : min_index = j; if min_index != i: arr[i],arr[min_index]=arr[min_index],arr[i] # main # Define array of integer numbers a = array.array('i', []) n = int(input('Enter size of Array=')) # input n numbers in arry for i in range(n): item= int(input("Enter number in array:")) a.append(item) # call selectionsort() function for sorting array selection_sort(a) # print sorted array print ("Sorted array is:") for i in a: print (i, '\t')
Output
Enter size of Array=5 Enter number in array:456 Enter number in array:78 Enter number in array:34 Enter number in array:2 Enter number in array:12 Sorted array is: 2 12 34 78 456