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
1,067 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,950 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,170 points
546 views
+3 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
• 180 points
17,597 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,820 points
1,798 views
0 votes
2 answers
–1 vote
2 answers

Data of XYZ company is stored in sorted list. Write a program for searching specific data from that list.

https://www.youtube.com/watch?v=mqaf7vj1AdA This link has the program. It uses ...READ MORE

answered Dec 3, 2019 in Python by Raveendiran
• 960 points
2,195 views
0 votes
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,000 points
60,931 views