If you're a programmer, chances are you've heard of selection sort. It's one of the simplest sorting algorithms out there, and it works by iterating through an array multiple times, finding the smallest element in each iteration and placing it at the beginning of the array. Selection sort is easy to implement in languages like C, C++, Python and Java. In fact, many introductory programming courses use selection sort as an example algorithm to teach basic programming concepts like loops and conditionals. While selection sort may be straightforward and easy to explain, it's not particularly efficient for large datasets compared to other more complex sorting methods. But despite its limitations, selection sort remains an important concept for any programmer to know.
#include <stdio.h>
void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex])
minIndex = j;
}
temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Array before sorting: ");
printArray(arr, n);
selectionSort(arr, n);
printf("Array after sorting: ");
printArray(arr, n);
return 0;
}
#include <iostream>
using namespace std;
void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex])
minIndex = j;
}
temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Array before sorting: " << endl;
printArray(arr, n);
selectionSort(arr, n);
cout << "Array after sorting: " << endl;
printArray(arr, n);
return 0;
}
def selectionSort(arr):
n = len(arr)
for i in range(n-1):
minIndex = i
for j in range(i+1, n):
if arr[j] < arr[minIndex]:
minIndex = j
arr[i], arr[minIndex] = arr[minIndex], arr[i]
def printArray(arr):
for i in arr:
print(i, end=" ")
print()
arr = [64, 25, 12, 22, 11]
print("Array before sorting:")
printArray(arr)
selectionSort(arr)
print("Array after sorting:")
printArray(arr)
public class Main {
public static void selectionSort(int arr[]) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex])
minIndex = j;
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
public static void printArray(int arr[]) {
for (int i : arr)
System.out.print(i + " ");
System.out.println();
}
public static void main(String args[]) {
int arr[] = {64, 25, 12, 22, 11};
System.out.println("Array before sorting:");
printArray(arr);
selectionSort(arr);
System.out.println("Array after sorting:");
printArray(arr);
}
}
Was this helpful?