xxxxxxxxxx
Void SelectionSort(int arr[ ], int n){
int i, j, minIndex;
for(i=0; i<n-1; i++){
minIndex=i; //index of minimum element
for(j=i+1; j<n; j++){
if(arr[ j ] < arr[ minIndex ]){
minIndex=j; //update the min element
swap(arr[ minIndex ], arr[ i ]); //Swapping
}
}
}
}
xxxxxxxxxx
// 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;
}
}
}
xxxxxxxxxx
#include <bits/stdc++.h>
using namespace std;
void selectionSort(int arr[], int n){
int i,j,min;
for(i=0;i<n-1;i++){
min = i;
for(j=i+1;j<n;j++){
if(arr[j] < arr[min]){
min = j;
}
}
if(min != i){
swap(arr[i],arr[min]);
}
}
}
int main()
{
int arr[] = { 1,4,2,5,333,3,5,7777,4,4,3,22,1,4,3,666,4,6,8,999,4,3,5,32 };
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
for(int i = 0; i < n; i++){
cout << arr[i] << " ";
}
return 0;
}
xxxxxxxxxx
public void selectionsort(int array[])
{
int n = array.length; //method to find length of array
for (int i = 0; i < n-1; i++)
{
int index = i;
int min = array[i]; // taking the min element as ith element of array
for (int j = i+1; j < n; j++)
{
if (array[j] < array[index])
{
index = j;
min = array[j];
}
}
int t = array[index]; //Interchange the places of the elements
array[index] = array[i];
array[i] = t;
}
}
xxxxxxxxxx
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
xxxxxxxxxx
// 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;
}
xxxxxxxxxx
function swap(arr, i, j) {
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
function selectionSort(arr) {
let min;
// idx is position to fill up with next smallest value
for (let idx = 0; idx < arr.length - 1; idx++) {
min = idx;
// Look for next smallest value in rest of array
for (let scan = idx + 1; scan < arr.length; scan++) {
if (arr[scan] < arr[min]) {
min = scan;
}
}
// Exchange current value with the next smallest value
swap(arr, idx, min);
}
return arr;
}
xxxxxxxxxx
# 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]
xxxxxxxxxx
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))
xxxxxxxxxx
// C program for implementation of selection sort
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[],int n){
int min_idx;
for(int i=0;i<n-1;i++){
min_idx=i;
for(int j=i+1;j<n;j++){
if(arr[j]<arr[min_idx]){
min_idx=j;
}
}
swap(&arr[min_idx],&arr[i]);
}
}
void bubble_sort(int arr[],int n){
for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;j++){
if(arr[j]>arr[j+1]){
swap(&arr[j],&arr[j+1]);
}
}
}
}
void insertionSort(int arr[],int n){
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j--){
if(arr[j]<arr[j-1]){
swap(&arr[j],&arr[j-1]);
}else{
break;
}
}
}
}
/* 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]);
insertionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}