selection sort

Solutions on MaxInterview for selection sort by the best coders in the world

showing results for - "selection sort"
Giacomo
08 Jan 2018
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}
Jaret
19 Jan 2019
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}  
Sara
22 Sep 2020
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}
Alessandro
30 Jun 2016
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
Anissa
08 Oct 2016
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
Maximiliano
14 Mar 2017
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]
Sebastián
03 Jul 2020
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))
Ianto
01 Mar 2020
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
Irene
23 Jan 2017
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
Christopher
11 Apr 2019
1array([1, 2, 3, 4, 5])
queries leading to this page
select sortc 2b 2bselection sort c 2b 2bselection sort exampleselection sort for ascending ordersort elements selection sortselection sort gfgsorting algorithms selection sortwhat is selection sort in data structuresequential sortexplain the selection sort algorithm 3fjava selection sortselection order algorithmselection sortingnalgorithmlist the steps that selection sort algorithmsorting algorithm selection sorthow does a selection sort work 3fhow to make a selection sort c 2b 2blinear selection sort with group of 5selection sort descendinga 29selection sortselection sort wikiselection sort csselection sort 7c python 7c hindi 7c urdu 7c yncselect sort cselection osrtselection sort descending orderdefine selection sort algorithmselection sort code c 2b 2bwhat is selection sort algorithmwhen selection sort is being usedwhat is a selection sort brief defselection sort algorithm c 2b 2b codewhat does selection sort meansorting arrays in c 2b 2bselection sort o notationtexplain selection sort with exampleselection sort algorithm analysisselection sort algorithm explanationselection sort advantageswhat are the types of selection sort algorithmwhen selection sort is usedselection sort definition algorithmselection sorwhich principle is used in selection sortselection sortrselection sorting algorithm in javaselection sort com15 29 write a program to implement selection sort using array as a data structure order of selection sortselection sort select methodselection sorting algorithms in data structureselection sort function gfg selection sortsort by selectionimplementaton of selection sort algorithmselection sort coding exampleinsersion sort and selection sort algohow does a selection sort workselection sort workingc 2b 2b selection sort algorithmselection sor in cppselection sort programmizselection sort in data structrure geeks for geeksselection sort algorith4 09write a program to sort the given array using selection sort basic block of selection sortselection sort treeselection sort in ascending orderdescribe selection sortselection sort que sworking of selection sortselection sort algorithm in cwhat is selection sortselection sort 5cwhat is a selection sort 3f 2fselection sort thetsselection sort in placewhat is selection sort 3fselection sort in masmselection sort toolselection sortselect sorthow selection sort works 3fselection sort oselection sort alselection sort demodescription of selection sortselection sort algorithm 2cselection sort sortc 2b 2b selection sort array accumulate iterationswrite a menu driven program to sort an array using selection sort 26 radix sort display each iteration of sorting selection sort que esinsertion sort geeksforgeekssort selectionselection sort algorithm 5cwhat is a selection sort 3fhow does selection sort workselection sort in sorted listsort array using selection sortselection sort vedselection sorting algorithms based on system archi tectureselection sort simple definitionselection sort program in c 2b 2b using arrayselection sort algorithmselection sort methodselection sort in c 2b 2bselection sort exampleswhat is order of selection sortdefine selection sortselection sort programizunderstanding selection sortsort selection wikihow does the selection sort workwhen we use selection sortwrite a menu driven program to sort an array using selection sort 26amp 3b radix sort display each iteration of sortingselection sort worksselection sort conceptselection sort algorithm void selectionsortselection sprt3 d selection sortselection sort on geek for geek in pythonsequntial sortingselection sort in descending order 0 2f 10selection sorting algorithm logicdoes selection sort not work for larger arraysselection sort 3fselection sort implementationselection sort programselection sort algorithm examplebasic block of selection sort in tocselection sortingwrite a menu driven program to sort an array using selection sort 26 radix sort display each iteration of sortingselection sort swapselection sortsortselection sort explanationfrom selection sort import selection 2ais selection sort goodis selection sort in place 3fgeeks for geeks sequential sortselection sortiselectionsort in chow does an selection sort work selection sortselection sorthow does selection sort work to sort an array 3fselection sort program in cselection sort cselection sort with stepscpp sort array using selection sort and printwhat is sorting by selectionalgorithm of selection sorthow does selection sort work 3fwhat does the selection sort doselection sort alogirthmhow selection sort workssselection sort algorithmselection sort definitiongive the sort algorithm by selectionselection sort codeimplement selection sort in cselection sorting algorithmselection sort analysisselection sort in python geeks for geekswhere we use selection sortselection sort algorithm c 2b 2bsorting selectionselection sort founderselection sort how it worksselectionsort algorithmpython selection sortselection sorting implememntation in c 2b 2buse of selection sort algorithmselect sort c 2b 2b codeselection sort tselection sortc int selectionsort 5cwrite a pseudocode for selection sort algorithm 2c and prove that the algorithm is correct selection sort inoutalgorithm selection sortc 2b 2b selection sortbasic block of selection sort in theory of computationwhat is a selection sortselection sort flowchartselection sort pseudocodeselection sort descriptionselection sorting algorithmsselection sort with counter ascending and descendingselection sort arrayselection sort in c 2bselection sort c codeexample of selection sortwhat is selection sortingsorting array using delection sort in ascending order in c 2b 2bselection sort formulaselection sort stepsis selection sort in placeint selectionsort c 2b 2bin selection sort 2c position in the sorted array is fixed 2c it selects element for thatselection sort in cselection sort list selection sort on sorted listselection sort explainedsorting refers to arranging data in a particular format sort an array of integers in ascending order one of the algorithm is selection sortselection sort selection sort using select algorithmselection sort logicexplain selection sortselection sort