Bubble Sort w/ Dictionaries
- sort based on values.
def bubble_sort_dict_by_value(dictionary):
items = list(dictionary.items())
# convert dictionary into a list of tuples
n = len(items)
# Traverse through all dictionary elements
for i in range(n):
# Last i elements are already in place
for j in range(0, n-i-1):
# Traverse the dictionary from 0 to n-i-1
# Swap if the value of the current element is greater than the next element
if items[j][1] > items[j+1][1]:
items[j], items[j+1] = items[j+1], items[j]
# Convert sorted items back to dictionary
sorted_dict = dict(items)
return sorted_dict
# Example
dictionary = {'apple': 5, 'banana': 3, 'cherry': 7, 'date': 10, 'fig': 2}
print("Original dictionary:", dictionary)
sorted_dict = bubble_sort_dict_by_value(dictionary)
print("Sorted dictionary by value:", sorted_dict)
# OUTPUT
# Original dictionary: {'apple': 5, 'banana': 3, 'cherry': 7, 'date': 10, 'fig': 2}
# Sorted dictionary by value: {'fig': 2, 'banana': 3, 'apple': 5, 'cherry': 7, 'date': 10}
items = list(dictionary.items()) - TypeError: ‘list’ object is not callable
bubble_sort_dict_by_value(dictionary):
- This function performs bubble sort on a dictionary by its values.
- It converts the dictionary into a list of tuples (key, value), sorts them based on values using bubble sort, and then converts them back into a dictionary.
- Bubble sort is applied similarly to how it is applied to lists, comparing adjacent elements based on their values.
Bubble Sort based on keys.
- Sorts a dictionary by its keys using Bubble Sort.
def bubble_sort_dict_by_keys(dictionary):
"""
Args:
dictionary: The dictionary to be sorted.
Returns:
A new dictionary with keys sorted in ascending order.
"""
keys = list(dictionary.keys())
# Converts dict keys into a list (easier manipulation).
n = len(keys)
# of keys in the dict.
# Traverse through all keys
for i in range(n):
# Outer loop iterates through each key in the list
for j in range(0, n - i - 1):
# Compare the keys within the unsorted portion.
if keys[j] > keys[j + 1]:
# Swap keys
# Swaps the positions of the keys if the current key is greater than the next key.
keys[j], keys[j + 1] = keys[j + 1], keys[j]
# Create a new dictionary with sorted keys
sorted_dict = {}
for key in keys:
sorted_dict[key] = dictionary[key]
return sorted_dict
# Example :
data = {'c': 3, 'a': 1, 'b': 2}
# Sort the dictionary by keys
sorted_data = bubble_sort_dict_by_keys(data)
# Print the sorted dictionary
print(sorted_data)
- inefficient sorting algorithm used to sort the keys.
- Dictionary Sorting: The code focuses on sorting the keys of a dictionary, preserving the associated values.
- New Dictionary: It creates a new dictionary with sorted keys rather than modifying the original dictionary.
- Bubble Sort for sorting keys is generally not recommended for larger dictionaries due to its time complexity of O(n^2).
- built-in sorted() function with the key argument to specify the key to sort by.
Bubble Sort based on keys #2
- performs bubble sort based on a specified key.
data
: The list of dictionaries to be sorted.key
: The key in the dictionaries to use for comparison.
def bubble_sort_by_key(data, key):
"""
Sorts a list of dictionaries based on a specific key using Bubble Sort.
Args:
data: A list of dictionaries to be sorted.
key: The key in the dictionaries to sort by.
Returns:
The sorted list of dictionaries.
"""
n = len(data)
# Traverse through all array elements
for i in range(n):
# Last i elements are already in place
for j in range(0, n - i - 1):
# Compare the values of the dictionaries based on the specified key
if data[j][key] > data[j + 1][key]:
# Swap if the current element is greater than the next element
data[j], data[j + 1] = data[j + 1], data[j]
return data
# Returns the sorted list of dictionaries
# Example usage:
data = [
{'name': 'Alice', 'age': 25},
{'name': 'Bob', 'age': 30},
{'name': 'Charlie', 'age': 20},
]
# Sort by age
sorted_data = bubble_sort_by_key(data, 'age')
# Print the sorted data
print(sorted_data)
- Sorting by Key: The code allows you to specify which key in the dictionaries should be used for comparison and sorting.
- Flexibility: This code can be adapted to sort any list of dictionaries based on any key.