-
Notifications
You must be signed in to change notification settings - Fork 2
Algorithms Selection Sort
The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from the unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array.
- The subarray which is already sorted.
- Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.
arr[] = 64 25 12 22 11
# Placing the minimum element in arr[0...4] in the beginning
11 25 12 22 64
# Placing the minimum element in arr[1...4] in the beginning
11 12 25 22 64
# Placing the minimum element in arr[2...4] in the beginning
11 12 22 25 64
# Placing the minimum element in arr[3...4] in the beginning
11 12 22 25 64
void selection_sort(int array[], int n)
{
// Contains index of minimum element in unsorted subarray
int min_index;
// Move boundary of unsorted subarray
for(int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted subarray
min_index = i;
for(int j = i+1; j < n; j++)
{
// If present element is less than element at min_index
// Then change min_index to present index
if(array[j] < array[min_index])
{
min_index = j;
}
}
// Swap the element at min_index with the first element
int temp;
temp = array[min_index];
array[min_index] = array[i];
array[i] = temp;
}
}
🚀 Run Code
def selection_sort(arr):
for i in range(len(arr)):
min_x = i
for j in range(i+1,len(arr)):
if arr[j] < arr[min_x]:
min_x = j
arr[min_x], arr[i] = arr[i], arr[min_x]
arr = [64, 25, 12, 22, 11]
selection_sort(arr)
print(arr) # Prints [11, 12, 22, 25, 64]
🚀 Run Code
Time Complexity: O(n*n) Due to the two nested loops.
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links