Write a program that sorts a given list once in ascending and then in descending order without using the sort built-in function

0 votes
Dec 26, 2020 in Python by anonymous
• 120 points
3,843 views

1 answer to this question.

0 votes

Hi, @There,

Given an array of size n, arrange the first k elements of the array in ascending order and the remaining n-k elements in descending order.
Examples: 
 

Input: arr[] = {5, 4, 6, 2, 1, 3, 8, 9, -1}, k = 4 
Output: 2 4 5 6 9 8 3 1 -1
Input: arr[] = {5, 4, 6}, k = 2 
Output: 4 5 6 

# Python 3 program to sort first

# k elements in increasing order

# and remaining n-k elements in

# decreasing


# Function to sort the array

def printOrder(arr, n, k):


    len1 = k

    len2 = n - k

    arr1 = [0] * k

    arr2 = [0] * (n - k)


    # Store the k elements

    # in an array

    for i in range(k):

        arr1[i] = arr[i]


    # Store the remaining n-k

    # elements in an array

    for i in range(k, n):

        arr2[i - k] = arr[i]


    # sorting the array from

    # 0 to k-1 places

    arr1.sort()


    # sorting the array from

    # k to n places

    arr2.sort()


    # storing the values in the

    # final array arr

    for i in range(n):

        if (i < k):

            arr[i] = arr1[i]


        else :

            arr[i] = arr2[len2 - 1]

            len2 -= 1

     

    # printing the array

    for i in range(n):

        print(arr[i], end = " ")


# Driver code

if __name__ == "__main__":

    arr = [ 5, 4, 6, 2, 1,

            3, 8, 9, -1 ]

    k = 4


    n = len(arr)


    printOrder(arr, n, k)


# This code is contributed

# by ChitraNayal
Output:

2 4 5 6 9 8 3 1 -1

answered Dec 28, 2020 by Gitika
• 65,770 points

Related Questions In Python

+1 vote
1 answer
0 votes
1 answer

Print a list in both ascending and descending order

You can use listName.sort(reverse=True). This will sort the ...READ MORE

answered Feb 7, 2019 in Python by Omkar
• 69,220 points
1,562 views
+4 votes
3 answers

Write a for loop that prints all elements of a list and their position in the list. a = [4,7,3,2,5,9]

Try using this question by list comprehension: a=[4,7,3,2,5,9] print([x for ...READ MORE

answered Dec 9, 2019 in Python by vinaykumar
• 200 points
34,850 views
0 votes
1 answer

Write code to create a list of word lengths for the words in original_str using the accumulation pattern and assign the answer to a variable num_words_list.

Hi,  num_words_list = len(original_str.split()) original_str.split() - split words in ...READ MORE

answered May 27, 2020 in Python by Niroj
• 82,840 points
3,889 views
–1 vote
2 answers
+1 vote
4 answers

how to sort a list of numbers without using built-in functions like min, max or any other function?

Yes it is possible. You can refer ...READ MORE

answered Jun 27, 2019 in Python by Arvind
• 3,050 points
188,455 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP