GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python Selection Sort Python Selection Sort Tutoriallast modified March 8, 2025 In this article, we explain the selection sort algorithm in Python. We cover basic definitions, provide examples for sorting numeric and textual data, and compare selection sort with quick sort. An algorithm is a step-by-step procedure for solving a problem or performing a computation. Algorithms are the backbone of computer programs. Sorting is the process of arranging data in a specific order, such as ascending or descending. Sorting is a fundamental operation in computer science. Common Sorting AlgorithmsSome common sorting algorithms include:
Selection Sort AlgorithmThe selection sort algorithm works by repeatedly finding the minimum (or maximum) element from the unsorted portion of the list and swapping it with the first unsorted element. This process continues until the entire list is sorted. Selection Sort ExampleThe following is a Python implementation of the selection sort algorithm. selection_sort.py
def selection_sort(arr, ascending=True):
n = len(arr)
for i in range(n):
idx = i
for j in range(i + 1, n):
if (ascending and arr[j] < arr[idx]) or (not ascending and arr[j] > arr[idx]):
idx = j
arr[i], arr[idx] = arr[idx], arr[i]
return arr
# Sorting numeric data
numbers = [64, 25, 12, 22, 11]
print("Sorted in ascending order:", selection_sort(numbers))
print("Sorted in descending order:", selection_sort(numbers, ascending=False))
# Sorting textual data
words = ["apple", "banana", "kiwi", "cherry"]
print("Sorted in ascending order:", selection_sort(words))
print("Sorted in descending order:", selection_sort(words, ascending=False))
The $ ./selection_sort.py Sorted in ascending order: [11, 12, 22, 25, 64] Sorted in descending order: [64, 25, 22, 12, 11] Sorted in ascending order: ['apple', 'banana', 'cherry', 'kiwi'] Sorted in descending order: ['kiwi', 'cherry', 'banana', 'apple'] Comparing Selection Sort with Quick SortSelection sort is simple but inefficient for large datasets. Quick sort, on the other hand, is much faster for large datasets due to its divide-and-conquer approach. Let's compare their performance. benchmark.py
import time
import random
def selection_sort(arr):
n = len(arr)
for i in range(n):
idx = i
for j in range(i + 1, n):
if arr[j] < arr[idx]:
idx = j
arr[i], arr[idx] = arr[idx], arr[i]
return arr
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)
# Generate a large dataset
data = [random.randint(1, 1000) for _ in range(1000)]
# Benchmark selection sort
start = time.time()
selection_sort(data.copy())
end = time.time()
print(f"Selection Sort Time: {end - start:.6f} seconds")
# Benchmark quick sort
start = time.time()
quick_sort(data.copy())
end = time.time()
print(f"Quick Sort Time: {end - start:.6f} seconds")
The example benchmarks the performance of selection sort and quick sort on a dataset of 1000 random integers. SourceIn this article, we explained the selection sort algorithm in Python and compared it with quick sort. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |