LAB 3 SAMPLE ANSWERS 1. # findmin.py # Finds the minimum integer in a list of integers def main(): A = [4, 7, 3, 9, 8] i = 0 min = A[0] while i < len(A)-1: i = i + 1 if A[i] < min: min = A[i] print "The minimum positive integer is", min main() 2. # findminindex.py # Finds the index of the minimum integer in a list of integers def main(): A = [4, 7, 3, 9, 8] i = 0 min = A[0] minIndex = 0 while i < len(A)-1: i = i + 1 if A[i] < min: min = A[i] minIndex = i print "The minimum positive integer is at index", minIndex main() 3. # Python implementation of Improved Bubble Sort def main(): A = [8, 4, 9, 3, 7] print "Before sort: ", A j = 1 while j < n: # repeat j loop n-1 times (note: j starts at 1) i = 0 s = 0 # number of swaps in this pass # repeat i loop n-j times (note: i starts at 0) while i < n-j: if A[i] > A[i+1]: temp = A[i] A[i] = A[i+1] A[i+1] = temp s = s + 1 i = i + 1 j = j + 1 if s == 0: j = n # forces first while loop to terminate print "After sort: " , A main()