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
Python3
Input = [ 12.8 , . 178 , 1.8 , 782.7 , 99.8 , 8.7 ]
Output = sorted ( Input , key = lambda x: float (x))
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
Python3
Input = [ 12.8 , . 178 , 1.8 , 782.7 , 99.8 , 8.7 ]
Output = sorted ( Input , key = float )
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
Python3
Input = [ 12.8 , . 178 , 1.8 , 782.7 , 99.8 , 8.7 ]
Input .sort(key = float )
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.
Python3
Input = [ 12.8 , . 178 , 1.8 , 782.7 , 99.8 , 8.7 ]
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]
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
Python3
import numpy as np
Input = [ 12.8 , . 178 , 1.8 , 782.7 , 99.8 , 8.7 ]
sorted_array = np.sort(np.array( Input ))
sorted_list = sorted_array.tolist()
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.
Last Updated :
02 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...