Selection Sort
Selection sort is an effective and efficient sort algorithm based on comparison operations. It adds one element in each iteration. You need to select the smallest element in the array and move it to the beginning of the array by swapping with the front element.
Time complexity:
The time complexity of Selection Sort is O(N2).
Space complexity:
O(1) as the only extra memory used is for temporary variables while swapping two values in Array. The selection sort never makes more than O(N) swaps and can be useful when memory write is a costly operation.
Java
python
JavaScript
c
// Javascript program for implementation of selection sort function swap(arr,xp, yp) { var temp = arr[xp]; arr[xp] = arr[yp]; arr[yp] = temp; } function selectionSort(arr, n) { var i, j, min_idx; // One by one move boundary of unsorted subarray for (i = 0; i < n-1; i++) { // Find the minimum element in unsorted array min_idx = i; for (j = i + 1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; // Swap the found minimum element with the first element swap(arr,min_idx, i); } } function printArray( arr, size) { var i; for (i = 0; i < size; i++) document.write(arr[i] + " "); document.write("
"); } var arr = [64, 25, 12, 22, 11]; var n = 5; selectionSort(arr, n); document.write("Sorted array:
"); printArray(arr, n);public class SelectionSortExample { public static void selectionSort(int[] arr){ for (int i = 0; i < arr.length - 1; i++) { int index = i; for (int j = i + 1; j < arr.length; j++){ if (arr[j] < arr[index]){ index = j;//searching for lowest index } } int smallerNumber = arr[index]; arr[index] = arr[i]; arr[i] = smallerNumber; } } public static void main(String a[]){ int[] arr1 = {9,14,3,2,43,11,58,22}; System.out.println("Before Selection Sort"); for(int i:arr1){ System.out.print(i+" "); } System.out.println(); selectionSort(arr1);//sorting array using selection sort System.out.println("After Selection Sort"); for(int i:arr1){ System.out.print(i+" "); } } }
# Python program for implementation of Selection # Sort import sys A = [64, 25, 12, 22, 11] # Traverse through all array elements for i in range(len(A)): # Find the minimum element in remaining # unsorted array min_idx = i for j in range(i+1, len(A)): if A[min_idx] > A[j]: min_idx = j # Swap the found minimum element with # the first element A[i], A[min_idx] = A[min_idx], A[i] # Driver code to test above print ("Sorted array") for i in range(len(A)): print("%d" %A[i],end=" ")
// C program for implementation of selection sort #include
void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp; *yp = temp; } void selectionSort(int arr[], int n) { int i, j, min_idx; // One by one move boundary of unsorted subarray for (i = 0; i < n-1; i++) { // Find the minimum element in unsorted array min_idx = i; for (j = i+1; j < n; j++) if (arr[j] < arr[min_idx]) min_idx = j; // Swap the found minimum element with the first element if(min_idx != i) swap(&arr[min_idx], &arr[i]); } } /* Function to print an array */ void printArray(int arr[], int size) { int i; for (i=0; i < size; i++) printf("%d ", arr[i]); printf("\n"); } // Driver program to test above functions int main() { int arr[] = {64, 25, 12, 22, 11}; int n = sizeof(arr)/sizeof(arr[0]); selectionSort(arr, n); printf("Sorted array: \n"); printArray(arr, n); return 0; }