truthy
truthy

Reputation: 11

How do I sort 4 numbers using only conditionals (no arrays)

I'm having some trouble with my intro course in Python. We were tasked to sort four integer inputs in descending order, and then the program should indicate the order you keyed in the inputs as well.

For example: Input: 5, 10, 3, 3 Output: (1) 2nd input (2) 1st input (3) 3rd, 4th input

The only problem is we're NOT supposed to use arrays or built-in sorting functions but ONLY conditionals.

We've already done the first half of the code in class. Here's a sample of the sorting algorithm we did:

# user enters a, b, c, d
if a > b:
    two = a
    one = b
if c > d:
    four = c
    three = d
if two > four:
    handle = three
    three = four
    four = two
    two = handle

...And so on.

I'm not sure how to proceed from there. The problem is doing the code above kind of forgets the order of the original inputs since you assign new values. Any thoughts on what I'm missing here?

Upvotes: 1

Views: 5689

Answers (4)

Ry-
Ry-

Reputation: 225005

One less comparison, which is optimal for a fixed series of conditional swaps:

if a > b: a, b = b, a
if c > d: c, d = d, c
if a > c: a, c = c, a
if b > d: b, d = d, b
if b > c: b, c = c, b

Upvotes: 2

salmanwahed
salmanwahed

Reputation: 9647

If you have enough time you can try the brute force also,

def mymax(a, b, c):
    if a > b:
        if a > c:
            return a
        else:
            return c
    else:
        if b > c:
            return b
        else:
            return c


def sort(p, q, r, s):
    maximum = mymax(p, q, r)
    if maximum > s:
        first_max = maximum
        if first_max == p:
            maximum = mymax(q, r, s)
            second_max = maximum
            if second_max == q:
                if r > s:
                    return first_max, second_max, r, s
                else:
                    return first_max, second_max, s, r
            elif second_max == r:
                if q > s:
                    return first_max, second_max, q, s
                else:
                    return first_max, second_max, s, q
            elif second_max == s:
                if q > r:
                    return first_max, second_max, q, r
                else:
                    return first_max, second_max, r, q
        elif first_max == q:
            maximum = mymax(p, r, s)
            second_max = maximum
            if second_max == p:
                if r > s:
                    return first_max, second_max, r, s
                else:
                    return first_max, second_max, s, r
            elif second_max == r:
                if p > s:
                    return first_max, second_max, p, s
                else:
                    return first_max, second_max, s, p
            elif second_max == s:
                if p > r:
                    return first_max, second_max, p, r
                else:
                    return first_max, second_max, r, p
        elif first_max == r:
            maximum = mymax(p, q, s)
            second_max = maximum
            if second_max == p:
                if q > s:
                    return first_max, second_max, q, s
                else:
                    return first_max, second_max, s, q
            elif second_max == q:
                if p > s:
                    return first_max, second_max, p, s
                else:
                    return first_max, second_max, s, p
            elif second_max == s:
                if p > q:
                    return first_max, second_max, p, q
                else:
                    return first_max, second_max, q, p
    else:
        first_max = s
        second_max = maximum
        if second_max == p:
            if q > r:
                return first_max, second_max, q, r
            else:
                return first_max, second_max, r, q
        elif second_max == q:
            if p > r:
                return first_max, second_max, p, r
            else:
                return first_max, second_max, r, p
        elif second_max == r:
            if p > q:
                return first_max, second_max, p, q
            else:
                return first_max, second_max, q, p

print sort(1, 2, 3, 4)
print sort(4, 3, 2, 1)

Upvotes: 0

Joran Beasley
Joran Beasley

Reputation: 114008

one,two,three,four = [random.random() for _ in range(4)]
changed = 1
while changed:
  changed = 0
  if one > two:
     one,two = two,one
     changed = 1
  if two > three:
     two,three = three,two
     changed = 1
  if three > four:
     three,four = four,three
     changed = 1

is one way you could do it...

Upvotes: 2

Strikeskids
Strikeskids

Reputation: 4052

You could implement a hardcoded bubble sort

if a > b: b, a = a, b
if b > c: c, b = b, c
if c > d: d, c = c, d
if a > b: b, a = a, b
if b > c: c, b = b, c
if a > b: b, a = a, b

Upvotes: 6

Related Questions