Open In App

Python | Ways to sort list of float values

Given a list of float values, write a Python program to sort the list. 
Examples:

Input: list = ['1.2', '.8', '19.8', '2.7', '99.8', '80.7']
Output: ['.8', '1.2', '2.7', '19.8', '80.7', '99.8']

Input: list = [12.8, .178, 1.8, 782.7, 99.8, 8.7]
Output: [0.178, 1.8, 8.7, 12.8, 99.8, 782.7]

Let’s discuss different ways to solve this problem. 



Method #1 :Using lambda 




# Python code to sort list of decimal values
 
# List initialization
Input =  [12.8, .178, 1.8, 782.7, 99.8, 8.7]
 
# Using sorted and lambda
Output = sorted(Input, key = lambda x:float(x))
 
# Printing output
print(Output)

Output:

[0.178, 1.8, 8.7, 12.8, 99.8, 782.7]

Time complexity: The time complexity of this code is O(n log n), where n is the number of elements in the input list. 
Auxiliary space: The auxiliary space used by this code is O(n), where n is the number of elements in the input list.

Method #2 : Using sorted 




# Python code to sort list of decimal values
 
# List initialization
Input =  [12.8, .178, 1.8, 782.7, 99.8, 8.7]
 
# Using sorted + key
Output = sorted(Input, key = float)
 
# Printing output
print(Output)

Output:
[0.178, 1.8, 8.7, 12.8, 99.8, 782.7]

Time complexity: The time complexity of this code is O(n log n), where n is the number of elements in the input list. 
Auxiliary space: The auxiliary space used by this code is O(n), where n is the number of elements in the input list.

  Method #3 : Using sort 




# Python code to sort list of decimal values
 
# List initialization
Input =  [12.8, .178, 1.8, 782.7, 99.8, 8.7]
 
# Using sort + key
Input.sort(key = float)
 
# Printing output
print(Input)

Output:
[0.178, 1.8, 8.7, 12.8, 99.8, 782.7]

METHOD 3:  we can use bubble sort algorithm. 

This implementation iterates through the list multiple times, comparing adjacent elements and swapping them if they are in the wrong order. It repeats this process until the list is fully sorted.




# List initialization
Input =  [12.8, .178, 1.8, 782.7, 99.8, 8.7]
 
# Bubble sort algorithm
n = len(Input)
for i in range(n):
    for j in range(0, n-i-1):
        if Input[j] > Input[j+1]:
            Input[j], Input[j+1] = Input[j+1], Input[j]
 
# Printing output
print(Input)

Output
[0.178, 1.8, 8.7, 12.8, 99.8, 782.7]

Time complexity: O(n^2) in the worst case, where n is the length of the list.
Auxiliary space: O(1), which means it does not require any extra memory beyond the input list. 

Method #4: Using numpy.sort

This method involves using the numpy library’s sort() function to sort the list of float values. It takes in the list as an argument and returns a sorted numpy array.

Algorithm:

Import the numpy library
Initialize the input list
Convert the input list to a numpy array using np.array()
Sort the numpy array using np.sort()
Convert the sorted numpy array back to a list using .tolist()
Print the sorted list
 




# importing numpy library
import numpy as np
  
# List initialization
Input = [12.8, .178, 1.8, 782.7, 99.8, 8.7]
  
# Converting list to numpy array and sorting
sorted_array = np.sort(np.array(Input))
  
# Converting sorted numpy array to list
sorted_list = sorted_array.tolist()
  
# Printing output
print(sorted_list)

Output:

[0.178, 1.8, 8.7, 12.8, 99.8, 782.7]
 

Time complexity: The time complexity of this code is O(n log n), where n is the number of elements in the input list. This is because the np.sort() function uses a quicksort algorithm by default.

Auxiliary space: The auxiliary space used by this code is O(n), where n is the number of elements in the input list. This is because the np.array() function creates a new numpy array and the .tolist() function creates a new list.


Article Tags :