Hey, please how can i logically sort for numbers in a list
list = [10, 5, 2, 7, 20]
without the sort()
using only if statements and for loops and no while loop.

## Recommended Answers

It's very ineffient but here goes:

``````a= [10, 5, 2, 7, 20]
b=[]
for i in xrange(len(a)):
b.append(min(a))
a.pop(a.index(b[-1]))
``````

Here is a sorting method called bubble sort, the best way to learn is try and do it yourself even if you fail. This is wrote in pseudo code so you can impliment it into python.

``````define MyFunction (list):

for OuterForLoopCounter in 1 to listlength-1:
for InnerForLoopCounter …``````

## All 8 Replies

It's very ineffient but here goes:

``````a= [10, 5, 2, 7, 20]
b=[]
for i in xrange(len(a)):
b.append(min(a))
a.pop(a.index(b[-1]))
``````

Bro please can you explain this codes from line 3 to 5 .. Thanks.

You can follow the progress with a test-print:

``````a = [10, 5, 20, 7, 2]
print(a)
print('-'*44)

# start with an empty list
b = []
for i in range(len(a)):
# take the minimum value of list a and append it to list b
b.append(min(a))
# take the value you added to list b and remove it from list a
a.pop(a.index(b[-1]))
# optionally follow progress
print("a = %-16s  b = %s" % (a, b))

print('-'*44)
# final result
print(b)

''' output >>>
[10, 5, 20, 7, 2]
--------------------------------------------
a = [10, 5, 20, 7]    b = 
a = [10, 20, 7]       b = [2, 5]
a = [10, 20]          b = [2, 5, 7]
a =               b = [2, 5, 7, 10]
a = []                b = [2, 5, 7, 10, 20]
--------------------------------------------
[2, 5, 7, 10, 20]
'''
``````

I think using min() is cheating.
I think, you cannot sort a list without using the length of the list.
Take a look at the sorting algorythms
For first I recommend bubble sort.

``````A = [10, 5, 2, 7, 20]

for _ in range(len(A)*len(A)): # worst case
swapped=False
for i in range(len(A)):
if A[i-1]>A[i] and i>0:
A[i-1],A[i]=A[i],A[i-1]
swapped=True
if not swapped: break

print(A)
``````

Dudes am a raw beginner .. please in really simple lines explain this codes

Here is a sorting method called bubble sort, the best way to learn is try and do it yourself even if you fail. This is wrote in pseudo code so you can impliment it into python.

``````define MyFunction (list):

for OuterForLoopCounter in 1 to listlength-1:
for InnerForLoopCounter in 1 to listlength-1:
if list[InnerForLoopCounter] is greater than list[InnerForLoopCounter+1]:
swap list[item] and list[item+1]

return (list)
``````

Post back if you have any questions.

If that confuses you read up about nested for loops and bubble sort to get a better understanding.

Our friend rrashkin gave you a modified insertion sort (quoted wikipedia):
Insertion sort is a simple sorting algorithm that is relatively efficient for small lists and mostly sorted lists, and often is used as part of more sophisticated algorithms. It works by taking elements from the list one by one and inserting them in their correct position into a new sorted list.

If you are uneasy with Python's min() function, write your own.

Be a part of the DaniWeb community

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