I tried to use a different forum but I was completely out of my league with the responses. I am extrememly new to python. As a project, I'm supposed to ask the user to input a file name, check that the file exists, output the numbers in the file, and sort them for biggest to smallest. The trick is, I can't use built in functions to do the sorting. I have to use this algorithm:
for each i from range(n)
for each j from range(n-i)
if A[j-1]<A[j]
swap(A[j], A[j-1])
Here's what I've been playing with:
def main():
try:
file=open(input("Please enter the name of the file you wish to open:" ))
A =file.read().split()
n=len(A)
print ("These following", n,"numbers are in the inputted file:\n", A)
new_list=[]
while A:
minimum=A[1]
for i in range(n):
for j in range(n-1):
if A[1]<A[2]:
minimum=A[1]
new_list.append(minimum)
A.remove(minimum)
minimum=A[2]
for i in range(n):
for j in range(1,3):
if A[2]<A[1]:
minimum=A[2]
new_list.append(minimum)
A.remove(minimum)
minimum=A[2]
for i in range(n):
for j in range(1,2):
if A[3]<A[4]:
minimum=A[3]
new_list.append(minimum)
A.remove(minimum)
minimum=A[3]
for i in range(n):
for j in range(1,1):
if A[4] < A[0]:
minimum=A[4]
new_list.append(minimum)
A.remove(minimum)
#minimum=A[4]
#for i in range(n):
#for j in range(n-i):
#if A[4]<A[0]:
#minimum=A[4]
#new_list.append(A[4])
#if A < minimum:
# minimum = A
print (new_list)
break
file.close()
except IOError as e:
print("({})".format(e))
#new_list = []
#while A:
#minimum = A[0]
#for x in A:
#if x < minimum:
#minimum = x
#new_list.append(minimum)
#data_list.remove(minimum)
#print (new_list)
main()