Decode School

Decode School - The School for Problem Solvers

Welcome to my blog. Subscribe and get my latest blog post in your inbox.

Python Program to sort half of the list elements in ascending order and next half in descending order.

Python Program to get list size n and n elements of list, then sort the first half elements of list in ascending order and sort second half elements of list in descending order.

Sample Input 1:

5 5 7 9 3 1

Sample Output:

1 3 9 7 5

Program or Solution

					    
							
l=list(map(int,input("Enter Numbers:").split()))
for i in range(0,len(l)-1):
    for j in range(0,len(l)//2-1):
        if(l[j+1]<l[j]):
            l[j],l[j+1]=l[j+1],l[j]
    for j in range(len(l)//2,len(l)-1):
        if(l[j+1]>l[j]):
            l[j],l[j+1]=l[j+1],l[j]
            
print(l)

					    
					

Program Explanation

Bubble Sort Algorithm: Refer : https://www.tutorialspoint.com/data_structures_algorithms/bubble_sort_algorithm.htm