21.4k views
13 votes
Write a program that takes a list of integers as input. The input begins with an integer indicating the number of integers that follow. You can safely assume that the number of integers entered is always less than or equal to 10. The program then determines whether these integers are sorted in the ascending order (i.e., from the smallest to the largest). If these integers are sorted in the ascending order, the program will output Sorted; otherwise, it will output Unsorted

User Amol Pol
by
4.2k points

1 Answer

3 votes

Answer:

is_sorted = True

sequence = input("")

data = sequence.split()

count = int(data[0])

for i in range(1, count):

if int(data[i]) >= int(data[i+1]):

is_sorted = False

break

if is_sorted:

print("Sorted")

else:

print("Unsorted")

Step-by-step explanation:

*The code is in Python.

Initialize a variable named is_sorted as True. This variable will be used as a flag if the values are not sorted

Ask the user to enter the input (Since it is not stated, I assumed the values will be entered in one line each having a space between)

Split the input using split method

Since the first indicates the number of integers, set it as count (Note that I converted the value to an int)

Create a for loop. Inside the loop, check if the current value is greater than or equal to the next value, update the is_sorted as False because this implies the values are not sorted (Note that again, I converted the values to an int). Also, stop the loop using break

When the loop is done, check the is_sorted. If it is True, print "Sorted". Otherwise, print "Unsorted"

User Dominik Palo
by
3.8k points