selection sort
#include
using namespace std;
void selectionSort(int arr[], int n){
int i,j,min;
for(i=0;i
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;
}
}
}
Selection Sort
import numpy as np
def selection_sort(x):
for i in range(len(x)):
swap = i + np.argmin(x[i:])
(x[i], x[swap]) = (x[swap], x[i])
return x
Selection Sort
# Selection Sort
A = [5, 2, 4, 6, 1, 3]
for i in range(len(A)):
minimum = i
for j in range(i, len(A)):
if A[j] < A[minimum]:
minimum = j
if i != minimum:
A[minimum], A[i] = A[i], A[minimum]
selection sort
def ssort(lst):
for i in range(len(lst)):
for j in range(i+1,len(lst)):
if lst[i]>lst[j]:lst[j],lst[i]=lst[i],lst[j]
return lst
if __name__=='__main__':
lst=[int(i) for i in input('Enter the Numbers: ').split()]
print(ssort(lst))
Selection Sort
#include
using namespace std;
void print(int arr[], int n)
{
for(int i=0;i
selection sort
// Easy-peasy
#include
#include
using namespace std;
void selectionSort(vector &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 arr = {3,7,12,99,231,4,-6,-77,10};
selectionSort(arr);
for(auto it : arr) cout << it << " ";
return 0;
}
Selection Sort
x = np.array([2, 1, 4, 3, 5])
selection_sort(x)
Selection Sort
array([1, 2, 3, 4, 5])
|