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:" ))
n=len(A)
print ("These following", n,"numbers are in the inputted file:\n", A)

new_list=[]

while A:
minimum=A
for i in range(n):
for j in range(n-1):
if A<A:
minimum=A
new_list.append(minimum)
A.remove(minimum)

minimum=A
for i in range(n):
for j in range(1,3):
if A<A:
minimum=A
new_list.append(minimum)
A.remove(minimum)

minimum=A
for i in range(n):
for j in range(1,2):
if A<A:
minimum=A
new_list.append(minimum)
A.remove(minimum)

minimum=A
for i in range(n):
for j in range(1,1):
if A < A:
minimum=A
new_list.append(minimum)
A.remove(minimum)
#minimum=A
#for i in range(n):
#for j in range(n-i):
#if A<A:
#minimum=A
#new_list.append(A)

#if A < minimum:
# minimum = A

print (new_list)
break

file.close()

except IOError as e:
print("({})".format(e))

#new_list = []

#while A:
#minimum = A
#for x in A:
#if x < minimum:
#minimum = x
#new_list.append(minimum)
#data_list.remove(minimum)

#print (new_list)

main()
``````

## All 4 Replies

You have the pseudo code for your sort algorithm, so simply expand it to Python and test it ...

``````mylist = [5, 3, 7, 2, 8, 4]
print(mylist)
n = len(mylist)

for i in range(n):
for j in range(1, n-i):
# swap if prev value is less than current value
# change < to > to reverse the order
if mylist[j-1] < mylist[j]:
# do a tuple swap
(mylist[j-1], mylist[j]) = (mylist[j], mylist[j-1])

print(mylist)

''' result ...
[5, 3, 7, 2, 8, 4]
[8, 7, 5, 4, 3, 2]
'''
``````

You have no idea how much you've helped! thank you so much. I just have another question, how do I turn my list into strings?
Heres what I did but for some reason it only sorts every 3 numbers and not the whole thing. I figure if I make A into a string it would fix my problem!

``````def main():
try:

file=open(input("Please enter the name of the file you wish to open:" ))
file.close()
n = len(A)
print ("These following", n,"numbers are in the inputted file:\n", A)

for i in range(n):
for j in range(1,n-i):
if A[j-1] < A[j]:
(A[j-1], A[j]) = (A[j],A[j-1])
print("We can now organize it in descending order:\n", A)

except IOError as e:
print("({})".format(e))

Output_File = input("Where would you like to save this data?")
fileObject = open(Output_File, 'a')
fileObject.write(str(Output_File)+'\n')
print("Your file is now saved as",Output_File,".\nHave a nice day!")
fileObject.close()

if __name__ == '__main__':
main()
``````

Without using join() you can do it this way ...

``````mylist = [8, 7, 5, 4, 3, 2]

mystr = ""
space = " "
for n in mylist:
mystr += str(n) + space

print(mystr)

'''
8 7 5 4 3 2
'''
``````

If you are allowed to use join(), you can do this ...

``````mylist = [8, 7, 5, 4, 3, 2]
mystr = " ".join(str(n) for n in mylist)
print(mystr)

'''
8 7 5 4 3 2
'''
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.