I want to know how to sort the list in descending order without using the built-in function of the list.

Asked 2 years ago, Updated 2 years ago, 95 views

import random a = random.sample(range(1,100,10) #using the random module sample, random.sample(range, end value, sampling count) smallest = a[0] for i in a: if i < smallest: smallest = i A = smallest

print(a)
print(A)

I want to learn how to sort in descending order without using the list embedding function.

python list sorting

2022-09-22 16:56

1 Answers

Code using the famous sorting algorithm quicksort.

def sort(array):
    less = []
    equal = []
    greater = []

    if len(array) > 1:
        pivot = array[0]
        for x in array:
            if x < pivot:
                less.append(x)
            if x == pivot:
                equal.append(x)
            if x > pivot:
                greater.append(x)
        return sort(less)+equal+sort(greater)
    else:
        return array

Note: This code is partially modified from a similar question posted in stack overflow.


2022-09-22 16:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.