1// C algorithm for SelectionSort
2
3void selectionSort(int arr[], int n)
4{
5 for(int i = 0; i < n-1; i++)
6 {
7 int min = i;
8
9 for(int j = i+1; j < n; j++)
10 {
11 if(arr[j] < arr[min])
12 min = j;
13 }
14
15 if(min != i)
16 {
17 // Swap
18 int temp = arr[i];
19 arr[i] = arr[min];
20 arr[min] = temp;
21 }
22 }
23}
1#include <bits/stdc++.h>
2
3using namespace std;
4
5void selectionSort(int arr[], int n){
6 int i,j,min;
7
8 for(i=0;i<n-1;i++){
9 min = i;
10 for(j=i+1;j<n;j++){
11 if(arr[j] < arr[min]){
12 min = j;
13 }
14 }
15 if(min != i){
16 swap(arr[i],arr[min]);
17 }
18 }
19}
20
21int main()
22{
23 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 };
24 int n = sizeof(arr) / sizeof(arr[0]);
25
26 selectionSort(arr, n);
27
28 for(int i = 0; i < n; i++){
29 cout << arr[i] << " ";
30 }
31
32 return 0;
33}
1SelectionSort(List) {
2 for(i from 0 to List.Length) {
3 SmallestElement = List[i]
4 for(j from i to List.Length) {
5 if(SmallestElement > List[j]) {
6 SmallestElement = List[j]
7 }
8 }
9 Swap(List[i], SmallestElement)
10 }
11}
1//I Love Java
2import java.util.*;
3import java.io.*;
4import java.util.stream.*;
5import static java.util.Collections.*;
6import static java.util.stream.Collectors.*;
7
8public class Selection_Sort_P {
9 public static void main(String[] args) throws IOException {
10 BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
11 List<Integer> arr = Stream.of(buffer.readLine().replaceAll(("\\s+$"), "").split(" ")).map(Integer::parseInt)
12 .collect(toList());
13
14 int high = arr.size();
15 selection_sort(arr, high);
16
17 System.out.println(arr);
18 }
19
20 public static void swap(List<Integer> arr, int i, int j) {
21 int temp = arr.get(i);
22 arr.set(i, arr.get(j));
23 arr.set(j, temp);
24 }
25
26 public static void selection_sort(List<Integer> arr, int high) {
27 for (int i = 0; i <= high - 1; i++) {
28 steps(arr, i, high);
29 }
30 }
31
32 public static void steps(List<Integer> arr, int start, int high) {
33 for (int i = start; i <= high - 1; i++) {
34 if (arr.get(i) < arr.get(start)) {
35 swap(arr, start, i);
36 }
37 }
38 }
39}
40
1class Sort
2{
3 void selectionSort(int arr[])
4 {
5 int pos;
6 int temp;
7 for (int i = 0; i < arr.length; i++)
8 {
9 pos = i;
10 for (int j = i+1; j < arr.length; j++)
11 {
12 if (arr[j] < arr[pos]) //find the index of the minimum element
13 {
14 pos = j;
15 }
16 }
17
18 temp = arr[pos]; //swap the current element with the minimum element
19 arr[pos] = arr[i];
20 arr[i] = temp;
21 }
22 }
23
24 void display(int arr[]) //display the array
25 {
26 for (int i=0; i<arr.length; i++)
27 {
28 System.out.print(arr[i]+" ");
29 }
30 }
31
32 public static void main(String args[])
33 {
34 Sort ob = new Sort();
35 int arr[] = {64,25,12,22,11};
36 ob.selectionSort(arr);
37 ob.display(arr);
38 }
39}
40
1# Selection Sort
2A = [5, 2, 4, 6, 1, 3]
3for i in range(len(A)):
4 minimum = i
5 for j in range(i, len(A)):
6 if A[j] < A[minimum]:
7 minimum = j
8 if i != minimum:
9 A[minimum], A[i] = A[i], A[minimum]
1def ssort(lst):
2 for i in range(len(lst)):
3 for j in range(i+1,len(lst)):
4 if lst[i]>lst[j]:lst[j],lst[i]=lst[i],lst[j]
5 return lst
6if __name__=='__main__':
7 lst=[int(i) for i in input('Enter the Numbers: ').split()]
8 print(ssort(lst))
1//selection sort; timecomplexity=O(n^2);space complexity=O(n);auxiliary space complexity=O(1)
2#include <iostream>
3
4using namespace std;
5void swap(int*,int*);
6void selection_sort(int arr[],int n)
7{
8 for(int i=0;i<n-1;i++)
9 {
10 for(int j=i+1;j<n;j++)
11 {
12 if(arr[i]>arr[j])
13 {
14 swap(&arr[i],&arr[j]);
15 }
16 }
17 }
18}
19void display(int arr[],int n)
20{
21 for(int i=0;i<n;i++)
22 {
23 cout<<arr[i]<<" ";
24 }
25 cout<<endl;
26}
27
28int main()
29{
30 int n;
31 cout<<"enter the size of the array:"<<endl;
32 cin>>n;
33 int array_of_numbers[n];
34 cout<<"enter the elements of the array:"<<endl;
35 for(int i=0;i<n;i++)
36 {
37 cin>>array_of_numbers[i];
38 }
39 cout<<"array as it was entered"<<endl;
40 display(array_of_numbers,n);
41 cout<<"array after sorting:"<<endl;
42 selection_sort(array_of_numbers,n);
43 display(array_of_numbers,n);
44 return 0;
45}
46void swap(int *a,int *b)
47{
48 int temp=*a;
49 *a=*b;
50 *b=temp;
51}
52
1procedure selection sort
2 list : array of items
3 n : size of list
4
5 for i = 1 to n - 1
6 /* set current element as minimum*/
7 min = i
8
9 /* check the element to be minimum */
10
11 for j = i+1 to n
12 if list[j] < list[min] then
13 min = j;
14 end if
15 end for
16
17 /* swap the minimum element with the current element*/
18 if indexMin != i then
19 swap list[min] and list[i]
20 end if
21 end for
22
23end procedure