Simpler way of sorting three numbers
if (a > c)
swap(a, c);
if (a > b)
swap(a, b);
//Now the smallest element is the 1st one. Just check the 2nd and 3rd
if (b > c)
swap(b, c);
Note: Swap changes the values of two
variables.
How to manually sort a list of numbers in Python?
For three items, you could use max
and min
to sort them:
a, b, c = 3, 1, 8
x = min(a, b, c) # Smallest of the three
z = max(a, b, c) # Largest of the three
y = (a + b + c) - (x + z) # Since you have two of the three, you can solve for
# the third
print(a, b, c)
print(x, y, z)
If you don't want to use a sorting algorithm but can use lists, you could just pop out the smallest item each time and store it in a new list:
numbers = [1, 8, 9, 6, 2, 3, 1, 4, 5]
output = []
while numbers:
smallest = min(numbers)
index = numbers.index(smallest)
output.append(numbers.pop(index))
print(output)
It's pretty inefficient, but it works.
Sorting three numbers in ascending order without using functions
I think using your approach in the second code, this is the easiest way out:
a = float(input("Enter a: "))
b = float(input("Enter b: "))
c = float(input("Enter c: "))
if a > b:
a,b = b,a
if a > c:
a,c = c,a
if b > c:
b,c = c,b
print (a, "<", b, "<", c)
Manually sort a list of 10 integers in python
You've just got some of the order wrong: you need to append to your ordered list each time around
import random
unordered = list(range(10))
ordered = []
i = 0
random.shuffle(unordered)
print unordered
lowest = unordered[0]
while len(unordered) > 0:
if unordered[i] < lowest:
lowest = unordered[i]
i += 1
if i == len(unordered):
ordered.append(lowest)
unordered.remove(lowest)
if unordered:
lowest = unordered[0]
i = 0
print(ordered)
sorting a list manually on python 3
You need to bring some of your initialization into the loop like:
Code:
unordered = [18, 13, 44, 12, 19, 27, 2, 31]
print(unordered)
ordered = []
while unordered:
lowest = unordered[0]
index_of_lowest = 0
for i, number in enumerate(unordered):
if number < lowest:
lowest = number
index_of_lowest = i
del unordered[index_of_lowest]
ordered.append(lowest)
print(ordered)
Results:
[18, 13, 44, 12, 19, 27, 2, 31]
[2, 12, 13, 18, 19, 27, 31, 44]
python: order a list of numbers without built-in sort, min, max function
I guess you are trying to do something like this:
data_list = [-5, -23, 5, 0, 23, -6, 23, 67]
new_list = []
while data_list:
minimum = data_list[0] # arbitrary number in list
for x in data_list:
if x < minimum:
minimum = x
new_list.append(minimum)
data_list.remove(minimum)
print (new_list)
#Added parenthesis
Sorting 3 numbers without branching
No conditionals. Only a cast to uint. Perfect solution.
int abs (int a)
{
int b = a;
b = (b >> (sizeof(int)*CHAR_BIT-1) & 1);
return 2 * b * (a) + a;
}
int max (int a, int b) { return (a + b + abs(a - b)) / 2; }
int min (int a, int b) { return (a + b - abs(a - b)) / 2; }
void sort (int & a, int & b, int & c)
{
int maxnum = max(max(a,b), c);
int minnum = min(min(a,b), c);
int middlenum = a + b + c - maxnum - minnum;
a = maxnum;
b = middlenum;
c = minnum;
}
Sorting 3 Numbers from Least to Greatest
If you want to stick with the if/else logic, here's a slight modification to your original solution. Note the use of an else if.
I have commented out your earlier lines of code for comparison.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("Enter three numbers."); //Use println instead of print, that way the input begins on the next line
double x = reader.nextDouble();
double y = reader.nextDouble();
double z = reader.nextDouble();
if (x >= y){ //In the three responses below, y is always before x.
if (y >= z)
System.out.print("In order " + z + " "+ y + " " + x);
else if (z >= x)
System.out.print("In order " + y + " "+ x + " " + z);
else if (x > z)
System.out.print("In order " + y + " " + z + " " + x);
}
if (y > x){// In the three responses below, x is always before y
if (z >= y)
System.out.print("In order " + x + " " + y + " "+ z);
else if (z >= x)
//System.out.print("In order " + y + " " + x + " " + z); //In this case, z has to be smaller than y. The order was off
System.out.print("In order " + x + " " + z + " " + y);
else if (x > z)
//System.out.print("In order " + y + " " + z + " " + x);
System.out.print("In order " + z + " " + x + " " + y); //Y is the biggest. The order here was off.
}
}
}
Related Topics
How to Count the Number of Messages
Remove White Space from Entire Dataframe
How to Remove All Characters Before a Specific Character in Python
How to Read Numbers from File in Python
Can Anyone Explain Me What This Python 3 Command Do
Python How to Use Excelwriter to Write into an Existing Worksheet
What Does Sqlite3.Operationalerror: Near "-": Syntax Error Mean
Python Pandas: Drop Rows of a Timeserie Based on Time Range
How to Mention a User in Discord.Py
How to Clear Your Printed Text in Python
How to Limit a Number to Be Within a Specified Range (Python)
Django: Check Whether an Object Already Exists Before Adding
How to Install Python Packages from the Tar.Gz File Without Using Pip Install