Python program to Sort first half in ascending order and second half in descending order in an array

Sort First half in Ascending and Second half in descending order in Python

Sort First half in Ascending and Second half in descending order in Python

Here, in this page we will discuss the program to sort first half in ascending and second half in descending order in python programming language. We are given with an array and need to print the array in desired sorting way i.e, first half in increasing order and second half in descending order.

Here, in this page we will discuss two different methods to sort the given array such that it’s first half is in ascending order and second half in descending order.

  • Method 1 : Sort the entire array then, print first half in ascending and second half in descending.
  • Method 2 : Using alternative way of above method

Method 1 (Sort using inbuilt sort function):

  • Sort the entire array using inbuilt sort function
  • Run a loop till first half and print the element.
  • Run a loop for second half and print the elements.
sort the array in python

Method 1 : Code in Python

Run
# Python program to print first half in
# Python program to print first half in
# ascending order and the second half
# in descending order


# function to print half of the array in
# ascending order and the other half in
# descending order

def printOrder(arr,n) :
    # sorting the array
    arr.sort()
    
    # printing first half in ascending order
    i = 0
    while i < n/2: print(arr[i]) i=i+1 # printing second half in descending order j=n-1 while j >= n / 2 :
        print (arr[j])
        j = j - 1

# Driver code
arr = [5, 4, 6, 2, 1, 3, 8, 9, 7]
n = len(arr)

printOrder(arr, n)

Output

1 2 3 4  5 9 8 7 6 

Method 2 :

  • Sort the first half of the array in ascending order just because only the elements in the first half of the input array needs to be sorted in ascending order (In this way the original elements in the first half of the array will remain in the first half but in ascending order).
  • Sort the second half of the array in descending order just because only the elements in the second half of the input array needs to be sorted in descending order (In this way the original elements in the second half of the array will remain in the first half but in descending order)

Method 2 : Code in Python

Run
# Python 3 program to print first half
# in ascending order and the second half
# in descending order

# function to print half of the array in
# ascending order and the other half in
# descending order

def printOrder(arr, n):

    # sorting the array
    arr.sort()

    # printing first half in ascending order

    for i in range(n / 2):
        print(arr[i], end = " ")

    # printing second half in descending order

    for j in range(n - 1, n // 2 -1, -1) :
        print(arr[j], end = " ")

# Driver code
arr = [ 5, 4, 6, 2, 1, 3, 8, -1 ]
n = len(arr)
printOrder(arr, n)

Output

-1 1 2 3 8 6 5 4

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

20 comments on “Python program to Sort first half in ascending order and second half in descending order in an array”


  • Sushmitha

    l=[]
    n=int(input(“Enter the range:”))
    for i in range(n):
    s=int(input(“Enter the number:”))
    l.append(s)
    mid=n//2
    s=[]
    s1=sorted(l[:mid])
    s2=sorted(l[mid:],reverse=True)
    print(s1+s2)


  • Tanmay

    arr =[2,1,3,10,4,7,6,5,9,8]

    arr.sort()
    mid = len(arr)//2
    pt = len(arr) -1

    for i in range(mid,mid+len(arr)//4) :
    temp = arr[i]
    arr[i] =arr[pt]
    arr[pt] = temp
    pt = pt -1

    print(arr)


  • sathvikashri12

    def printOrder(arr, n):
    # sorting the array
    arr.sort()

    # printing first half in ascending order
    i = 0
    while i = n / 2:
    print(arr[j], end=’ ‘)
    j = j – 1

    # Driver code
    arr = [5, 4, 6, 2, 1, 3, 8, 9, 7]
    n = len(arr)

    printOrder(arr, n)


  • mahesh

    def func(arr):
    n = len(arr)
    mid = n // 2
    first_half = arr[:mid]
    second_half = arr[mid:]

    first_half.sort()
    second_half.sort(reverse = True)

    return first_half + second_half
    arr = [6,3,4,5,7,8]
    print(func(arr))


  • Madipadige

    #Sort First half in Ascending and Second half in descending order in Python
    lst = sorted(list(map(int, input().split(“,”))))
    a=len(lst)//2
    b=sorted(lst[:a])
    c=sorted(lst[a:])[::-1]
    for i in c:
    b.append(i)
    print(b)


  • KRISHNA

    arr=list(map(int,input().split()))
    a1=arr[:len(arr)//2]
    print(sorted(a1))
    a2=arr[len(arr)//2:]
    a2=sorted(a2)
    print(list(reversed(a2)))


  • Ratnaprabha

    Try this very very simple logic
    list = [50,20,50,69,90,10]
    mid = len(list)/2
    new1 = []
    new2 = []
    for i in range(0,round(mid)):
    new1.insert(i,list[i])
    new1.sort(key=None, reverse=False)
    print(new1)
    for i in range(round(mid),len(list)):
    new2.append(list[i])
    new2.sort(key = None, reverse=True)
    print(new2)


  • siddharthlikki

    Try this simple code!
    size=int(input(“ENTER ARRAY SIZE”))

    arr=[]
    for i in range(size):
    element=int(input())
    arr.append(element)
    arr.sort()
    rev = arr[::-1]

    print(“first half in ascending order”,arr[:3])
    print(“second half in descending order”,rev[:3])


  • Manish

    arr=list(map(int,input(“enter the number”).split()))
    n=len(arr)//2
    asc=arr[0:n]
    desc=arr[n:len(arr)]
    asc.sort()
    desc.sort(reverse=True)
    new_arr=asc+desc
    print(new_arr)


  • Anirudh

    n = list(map(int, input().split()))
    print(n)
    y = n[len(n)//2:]
    x = n[:len(n)//2]
    x.sort
    y.sort(reverse=True)
    print(x)
    print(y)
    print(x+y)


  • Nikita

    arr = [5,3,12,22,2,1,111,123,34,67]
    m = len(arr)
    n = m//2
    for i in range(0,n):
    for j in range(i+1,n):
    if arr[i]>arr[j]:
    temp = arr[i]
    arr[i]=arr[j]
    arr[j] = temp
    for i in range(n,m):
    for j in range(i+1,m):
    if arr[i]<arr[j]:
    temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp
    print(arr)


  • Arun

    from array import *
    from math import *
    a=array(“i”,map(int,input(“enter the value :”).split()))
    x=[]
    y=[]
    for i in range(ceil(len(a)/2)):
    x.append(a[i])
    for i in range(ceil(len(a)/2),len(a)):
    y.append(a[i])
    print(sorted(x))
    y.sort()
    print(y[::-1])
    xy=sorted(x)+y[::-1]
    print(xy)