Selection Sort

Selection Sort :

Author(s) : Unknown Date : Ancient

Selection sort works by repeatedly finding the minimum (or maximum) element in the unsorted portion of the array and swapping it with the first element in that unsorted portion. This process iterates until the entire array is sorted. Selection sort is conceptually easy to understand but suffers from a time complexity of O(n²) in the worst case. This is because it involves finding the minimum/maximum element in each pass, leading to redundant comparisons.

Time Complexity O(n²)
Best Case O(n²)
Worst Case O(n²)
Space Complexity O(1)
Stable No

Code Integration:

* the code might contain some bugs or may not work correctly

Python JavaScript Java Go C
def selectionsort(arr):
	n = len(arr)
	for i in range(n):
		# Find the index of the minimum element in the unsorted part
		min_index = i
		for j in range(i + 1, n):
			if arr[j] < arr[min_index]:
				min_index = j
		# Swap the minimum element with the first element of the unsorted part
		arr[i], arr[min_index] = arr[min_index], arr[i]

# Test array
arr = [64, 25, 12, 22, 11]
selectionsort(arr)
print('Sorted array is:', arr)
function selectionsort(arr) {
	let n = arr.length;
	for (let i = 0; i < n - 1; i++) {
		// Find the index of the minimum element in the unsorted part
		let minIndex = i;
		for (let j = i + 1; j < n; j++) {
			if (arr[j] < arr[minIndex]) {
				minIndex = j;
			}
		}
		// Swap the minimum element with the first element of the unsorted part
		[arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
	}

	// Test array
	let arr = [64, 25, 12, 22, 11];
	selectionsort(arr);
	console.log('Sorted array is:', arr);
}
public static void selectionsort(int[] arr) {
	int n = arr.length;
	for (int i = 0; i < n - 1; i++) {
		// Find the index of the minimum element in the unsorted part
		int minIndex = i;
		for (int j = i + 1; j < n; j++) {
			if (arr[j] < arr[minIndex]) {
				minIndex = j;
			}
		}
		// Swap the minimum element with the first element of the unsorted part
		int temp = arr[i];
		arr[i] = arr[minIndex];
		arr[minIndex] = temp;
	}

	// Test array
	int[] arr = {64, 25, 12, 22, 11};
	selectionsort(arr);
	System.out.println("Sorted array is:" + java.util.Arrays.toString(arr));
}
func selectionsort(arr []int) {
	n := len(arr)
	for i := 0; i < n - 1; i++ {
		// Find the index of the minimum element in the unsorted part
		minIndex := i
		for j := i + 1; j < n; j++ {
			if arr[j] < arr[minIndex] {
				minIndex = j
			}
		}
		// Swap the minimum element with the first element of the unsorted part
		arr[i], arr[minIndex] = arr[minIndex], arr[i]
	}

	// Test array
	arr := []int{64, 25, 12, 22, 11}
	selectionsort(arr)
	fmt.Println("Sorted array is:", arr)
}
void selectionsort(int arr[], int n) {
	for (int i = 0; i < n - 1; i++) {
		// Find the index of the minimum element in the unsorted part
		int minIndex = i;
		for (int j = i + 1; j < n; j++) {
			if (arr[j] < arr[minIndex]) {
				minIndex = j;
			}
		}
		// Swap the minimum element with the first element of the unsorted part
		int temp = arr[i];
		arr[i] = arr[minIndex];
		arr[minIndex] = temp;
	}

	// Test array
	int arr[] = {64, 25, 12, 22, 11};
	int n = sizeof(arr) / sizeof(arr[0]);
	selectionsort(arr, n);
	printf("Sorted array is: ");
	for (int i = 0; i < n; i++) {
		printf("%d ", arr[i]);
	}
}
light dark