0

First of all I am not allow to use any of the internal command that sort the list not any of the other sorted method such as Selection, buble. Compose a function that sorts a list known to have exactly two values this is what I have done :

def sort_Two_values(list):
Sorted_List=[]
Sorted_List.append(list[0])
for i in range(1,len(list)):
    if (list[i-1]>list[i]):
        Sorted_List.insert(0,list[i])
    elif (list[i-1]<list[i]):
        Sorted_List.append(list[i])
    else:
        if (list[i]==Sorted_List[0]):
            Sorted_List.insert(0,list[i])
        else:
            Sorted_List.append(list[i])
return Sorted_List

sort_Two_values([1,0,0,0,0,0,1,1,0])

It seems to be working well!

Now, I am trying to do that for three values. For example [1,0,0,0,2,2,1,0,0,2] Would someone help me out with that !

ADAM
  • 105
  • 1
  • 7
  • 1
    Similar to [this](http://stackoverflow.com/questions/11067209/sorting-how-to-sort-an-array-that-contains-3-kind-of-numbers) – Cyclotron3x3 Oct 03 '16 at 20:33
  • @Cyclotron3x3 OP said they cannot use any other sorting method, this includes bucket or merge sorts as the answer to your linked question includes – Nick is tired Oct 03 '16 at 21:19

1 Answers1

0

All you needs is to push 0 to beginning, 2 to last of list and 1 will remain wherever it is. By the end of the loop, you will have a sorted list.

Cyclotron3x3
  • 2,188
  • 23
  • 40