Answers for "what is a selection sort algorithm"

C
2

selection sort

// C algorithm for SelectionSort

void selectionSort(int arr[], int n)
{
	for(int i = 0; i < n-1; i++)
	{
		int min = i;
        
		for(int j = i+1; j < n; j++)
		{
			if(arr[j] < arr[min])
            	min = j;
		}
        
		if(min != i)
		{
        	// Swap
			int temp = arr[i];
			arr[i] = arr[min];
			arr[min] = temp;
		}
	}
}
Posted by: Guest on January-05-2021
0

selection sort

// Easy-peasy
#include<iostream>
#include<algorithm>
using namespace std;

void selectionSort(vector<int> &arr) {
    for(int i = 0; i < arr.size() - 1; ++i) {
		for(int j = i + 1; j < arr.size(); ++j) {
			if(arr[j] < arr[i]) swap(arr[i], arr[j]);
		}
    }
}

int main() {
    vector<int> arr = {3,7,12,99,231,4,-6,-77,10};
    selectionSort(arr);
    for(auto it : arr) cout << it << " ";
    return 0;
}
Posted by: Guest on January-20-2022

Code answers related to "what is a selection sort algorithm"

Code answers related to "C"

Browse Popular Code Answers by Language