Selection Sort
Introduction to Selection Sort
Selection sort is a simple comparison-based sorting algorithm. The main idea behind the algorithm is to divide the input list into two parts: the sorted part and the unsorted part. Initially, the sorted part is empty, and the unsorted part contains all the elements. The algorithm repeatedly selects the smallest (or largest, depending on the ordering) element from the unsorted part and moves it to the end of the sorted part. This process continues until the unsorted part becomes empty, and the sorted part contains the elements in the desired order.
The selection sort algorithm has a time complexity of O(n^2), making it inefficient for large lists. However, it is straightforward to understand and implement, making it a suitable choice for educational purposes and small-scale applications.
Real-World Examples and Scenarios
Selection sort has various real-world applications, especially in scenarios where simplicity is preferred over performance, or the dataset is relatively small. Here are some real-world examples:
- Sorting a list of students by their grades or names in a small class.
- Organizing files in a directory by their creation date or size.
- Sorting a deck of cards in ascending or descending order.
- Arranging a list of items by price in a small e-commerce store.
Real-World Scenario: Sorting a List of Products
Imagine an online store that sells electronic devices. The store owner wants to display the products in ascending order based on their prices. The store has a limited number of products, so performance is not a significant concern. In this case, selection sort can be utilized to sort the list of products by their prices.
Problem Statement
Given a list of products, each product having a name and a price, sort the list in ascending order based on the product prices.
Input:- A list of n products (1 ≤ n ≤ 1000), where each product has a name and a price.
Output:- The same list of products sorted in ascending order based on their prices.
Tying the Problem Statement with the Real-World Scenario
In the context of the online store, the problem statement can be translated as follows:
The store owner wants to display the products in a sorted manner, where the products with lower prices appear at the top, and the products with higher prices appear at the bottom. The selection sort algorithm can be used to achieve this ordering.
Solution to the Problem
To solve the problem, we will implement the selection sort algorithm. We will use a custom comparison function to compare the prices of the products. The algorithm will work as follows:
- Iterate through the list of products and find the product with the smallest price.
- Swap the smallest product with the product at the first position in the list.
- Repeat steps 1 and 2 for the remaining unsorted portion of the list until the entire list is sorted.
Step-by-Step Solution with the Real-World Scenario
Let's walk through the steps of the selection sort algorithm using the given list of products as an example:
[{"name": "Laptop", "price": 800}, {"name": "Tablet", "price": 300}, {"name": "Smartphone", "price": 500}, {"name": "Smartwatch", "price": 200}]
Find the smallest product price: {"name": "Smartwatch", "price": 200}.
Swap the smallest product with the first product in the list: [{"name": "Smartwatch", "price": 200}, {"name": "Tablet", "price": 300}, {"name": "Smartphone", "price": 500}, {"name": "Laptop", "price": 800}]
Repeat steps 2 and 3 for the remaining unsorted portion of the list:
- Find the smallest product price: {"name": "Tablet", "price": 300}
- Swap the smallest product with the second product in the list: [{"name": "Smartwatch", "price": 200}, {"name": "Tablet", "price": 300}, {"name": "Smartphone", "price": 500}, {"name": "Laptop", "price": 800}]
Continue the process until the entire list is sorted: [{"name": "Smartwatch", "price": 200}, {"name": "Tablet", "price": 300}, {"name": "Smartphone", "price": 500}, {"name": "Laptop", "price": 800}]
The sorted list of products is now ready to be displayed on the website.
Actual Code Solution
Here's a Python implementation of the selection sort algorithm to solve the problem:
def selection_sort(products):
for i in range(len(products)):
min_index = i
for j in range(i+1, len(products)):
if products[j]["price"] < products[min_index]["price"]:
min_index = j
products[i], products[min_index] = products[min_index], products[i]
# Example list of products
products = [
{"name": "Laptop", "price": 800},
{"name": "Tablet", "price": 300},
{"name": "Smartphone", "price": 500},
{"name": "Smartwatch", "price": 200}
]
# Sort the list of products
selection_sort(products)
# Print the sorted list
print(products)
Explanation of the Code Solution
The selection_sort
function takes a list of products as input and sorts it in-place using the selection sort algorithm. The outer loop iterates through each element in the list, and the inner loop iterates through the remaining unsorted portion of the list. The inner loop finds the index of the minimum-priced product and swaps it with the current element in the outer loop. The process continues until the entire list is sorted.
Solving Other Real-World Problems
The selection sort algorithm can be adapted to solve other real-world problems that involve sorting based on a specific attribute. By modifying the comparison function, we can sort objects based on different attributes, such as names, dates, or sizes.
For example, the store owner might want to sort the products based on their names. In this case, we can modify the comparison function in the selection sort algorithm to compare the product names instead of their prices.
By understanding the fundamental concepts behind the selection sort algorithm and adapting it to specific use cases, we can solve a wide range of sorting problems in various real-world scenarios.