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]

4
13

                                         void sort(int *arr, int n){
        
        // Incrementa di 1 il limite inferiore del sub array da ordinare
        for (int i = 0; i &lt; n-1; i++) 
        { 
            // Trova il minimo nel subarray da ordinare
            int indice_min = i; 
            for (int j = i+1; j &lt; n; j++) {
                
                // Confronto per trovare un nuovo minimo
                if (arr[j] &lt; arr[indice_min]) 
                    indice_min = j; // Salvo l'indice del nuovo minimo
            }
            
            // Scambia il minimo trovato con il primo elemento
            swap(arr,indice_min,i);    
        } 
    }
    
     void swap(int *arr, int a , int b){
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }
    
}

4 (10 Votes)
0
3.63
6
Sac 110 points

                                    #include &lt;bits/stdc++.h&gt;

using namespace std; 

void selectionSort(int arr[], int n){
    int i,j,min;
    
    for(i=0;i&lt;n-1;i++){
        min = i;
        for(j=i+1;j&lt;n;j++){
            if(arr[j] &lt; 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 &lt; n; i++){
        cout &lt;&lt; arr[i] &lt;&lt; &quot; &quot;;
    }

    return 0;  
}  

3.63 (8 Votes)
0
4
2
ZXX 90 points

                                    //selection sort; timecomplexity=O(n^2);space complexity=O(n);auxiliary space complexity=O(1)
#include &lt;iostream&gt;

using namespace std;
void swap(int*,int*);
void selection_sort(int arr[],int n)
{
    for(int i=0;i&lt;n-1;i++)
    {
        for(int j=i+1;j&lt;n;j++)
        {
            if(arr[i]&gt;arr[j])
            {
                swap(&amp;arr[i],&amp;arr[j]);
            }
        }
    }
}
void display(int arr[],int n)
{
    for(int i=0;i&lt;n;i++)
    {
        cout&lt;&lt;arr[i]&lt;&lt;&quot; &quot;;
    }
    cout&lt;&lt;endl;
}

int main()
{
    int n;
    cout&lt;&lt;&quot;enter the size of the array:&quot;&lt;&lt;endl;
    cin&gt;&gt;n;
    int array_of_numbers[n];
    cout&lt;&lt;&quot;enter the elements of the array:&quot;&lt;&lt;endl;
    for(int i=0;i&lt;n;i++)
    {
        cin&gt;&gt;array_of_numbers[i];
    }
    cout&lt;&lt;&quot;array as it was entered&quot;&lt;&lt;endl;
    display(array_of_numbers,n);
    cout&lt;&lt;&quot;array after sorting:&quot;&lt;&lt;endl;
    selection_sort(array_of_numbers,n);
    display(array_of_numbers,n);
    return 0;
}
void swap(int *a,int *b)
{
    int temp=*a;
    *a=*b;
    *b=temp;
}

4 (2 Votes)
0
0
2

                                    def ssort(lst):
    for i in range(len(lst)):
        for j in range(i+1,len(lst)):
            if lst[i]&gt;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))

0
0
4
2
Jon Whitmer 120 points

                                    procedure selection sort 
   list  : array of items
   n     : size of list

   for i = 1 to n - 1
   /* set current element as minimum*/
      min = i    
  
      /* check the element to be minimum */

      for j = i+1 to n 
         if list[j] &lt; list[min] then
            min = j;
         end if
      end for

      /* swap the minimum element with the current element*/
      if indexMin != i  then
         swap list[min] and list[i]
      end if
   end for
	
end procedure

4 (2 Votes)
0
4.33
3

                                    class Sort 
{ 
    void selectionSort(int arr[]) 
    { 
        int pos;
        int temp;
        for (int i = 0; i &lt; arr.length; i++) 
        { 
            pos = i; 
            for (int j = i+1; j &lt; arr.length; j++) 
           {
                if (arr[j] &lt; arr[pos])                  //find the index of the minimum element
                {
                    pos = j;
                }
            }

            temp = arr[pos];            //swap the current element with the minimum element
            arr[pos] = arr[i]; 
            arr[i] = temp; 
        } 
    } 
  
    void display(int arr[])                     //display the array
    { 
        for (int i=0; i&lt;arr.length; i++) 
        {
            System.out.print(arr[i]+&quot; &quot;);
        }  
    } 
  
    public static void main(String args[]) 
    { 
        Sort ob = new Sort(); 
        int arr[] = {64,25,12,22,11}; 
        ob.selectionSort(arr); 
        ob.display(arr); 
    } 
} 

4.33 (3 Votes)
0
4.22
9
RedRiderX 130 points

                                    // C algorithm for SelectionSort

void selectionSort(int arr[], int n)
{
	for(int i = 0; i &lt; n-1; i++)
	{
		int min = i;
        
		for(int j = i+1; j &lt; n; j++)
		{
			if(arr[j] &lt; arr[min])
            	min = j;
		}
        
		if(min != i)
		{
        	// Swap
			int temp = arr[i];
			arr[i] = arr[min];
			arr[min] = temp;
		}
	}
}

4.22 (9 Votes)
0
3.67
3
KnightOfNi 120 points

                                    //I Love Java
import java.util.*;
import java.io.*;
import java.util.stream.*;
import static java.util.Collections.*;
import static java.util.stream.Collectors.*;

public class Selection_Sort_P {
    public static void main(String[] args) throws IOException {
        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
        List&lt;Integer&gt; arr = Stream.of(buffer.readLine().replaceAll((&quot;\\s+$&quot;), &quot;&quot;).split(&quot; &quot;)).map(Integer::parseInt)
                .collect(toList());

        int high = arr.size();
        selection_sort(arr, high);

        System.out.println(arr);
    }

    public static void swap(List&lt;Integer&gt; arr, int i, int j) {
        int temp = arr.get(i);
        arr.set(i, arr.get(j));
        arr.set(j, temp);
    }

    public static void selection_sort(List&lt;Integer&gt; arr, int high) {
        for (int i = 0; i &lt;= high - 1; i++) {
            steps(arr, i, high);
        }
    }

    public static void steps(List&lt;Integer&gt; arr, int start, int high) {
        for (int i = start; i &lt;= high - 1; i++) {
            if (arr.get(i) &lt; arr.get(start)) {
                swap(arr, start, i);
            }
        }
    }
}

3.67 (3 Votes)
0
5
1
Cherrykate 100 points

                                    SelectionSort(List) {
  for(i from 0 to List.Length) {
    SmallestElement = List[i]
    for(j from i to List.Length) {
      if(SmallestElement &gt; List[j]) {
        SmallestElement = List[j]
      }
    }
    Swap(List[i], SmallestElement)
  }
}

5 (1 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
selection sort tree selection sort program selection sort in place explain selection sort selection sort programiz selection sort wiki selection sorti selection sort tool what is selection sorting selection sort com define selection sort selection sort definition algorithm selection sort programmiz selection sort algorithm. selection sort algorithm\ selection sort works what is order of selection sort selection sort algorithm analysis selection sort algorithm explanation selection sort analysis selection sort\ understanding selection sort a)Selection sort example of selection sort what is a selection sort? what is a selection sort?/ order of selection sort selection sort formula describe selection sort selection sort in sorted list explain selection sort with example is selection sort in place ? selection sort method what is selection sort? Selection Sort que es Selection Sort que s where we use selection sort selection sort definition how does an selection sort work selection sort steps sort selection wiki selection sort o notationt selection sort t selection sort ved how does a selection sort work? selection sort using select algorithm selection sort list working of selection sort selection sort implementation HOw selection sort works? selection sort with steps select sort selection sorting algorithm is selection sort good use of selection sort algorithm Write a menu driven program to sort an array using selection sort &amp;amp; Radix sort. Display each iteration of sorting selection sort explanation selection sort sort selection sort al selection sort founder when selection sort is used algorithm selection sort what does the selection sort do selection sort in masm selection sort demo selection sort description when we use selection Sort description of selection sort selection sort algorithm selection sort descending how does the selection sort work selection sort explained selection sort example selection order algorithm define selection sort algorithm what is selection sort algorithm sequntial sorting selection sorting algorithm in java selection sort program in c++ using array select sort c 3-d selection sort selection sort advantages selection sort flowchart void selectionsort what is a selection sort brief def what is a selection sort selection sort in python geeks for geeks selection sort on geek for geek in python gfg selection sort insersion sort and selection sort algo selection sor selection osrt what is sorting by selection selection sorting algorithms in data structure selection sortc sselection sort algorithm java Selection sort sorting arrays in c++ selection sorting algorithm logic algorithm of selection sort what are the types of selection sort algorithm selection sort algorithm c++ code implementaton of Selection sort algorithm selection sort gfg linear selection sort with group of 5 sequential sort insertion sort geeksforgeeks sort array using selection sort sorting selection selection sort in data structrure geeks for geeks selection sort algorith selection sort logic 15). Write a program to implement SELECTION SORT using array as a data structure. how does selection sort work? Sort by selection c++ selection sort array accumulate iterations which principle is used in selection sort selection sort with counter ascending and descending int selectionsort c++ int selectionsort\ How does Selection Sort work to sort an array? selection sortingnalgorithm sorting algorithms selection sort selection sort cs sort selection how does selection sort work how does a selection sort work selection sort examples selection sort alogirthm selectionsort algorithm selection sort swap In selection sort, position in the sorted array is fixed , it selects element for that selection sort from selection sort import selection* selection sprt selectionsort in c Selection Sort | Python | Hindi | Urdu | YNC what is selection sort in data structure selection sort algorithm in c Explain the selection sort algorithm? selection sorting algorithms based on system archi tecture selection sort program in c what does selection sort mean sorting refers to arranging data in a particular format.Sort an array of integers in ascending order.one of the algorithm is selection sort selection sort working Selection Sort. 4. Write a program to sort the given array using Selection Sort. selection sort coding example implement selection sort in c how selection sort works how to make a selection sort C++ c++ selection sort algorithm cpp sort array using selection sort and print does selection sort not work for larger arrays Selection sort in descending order 0 / 10 selection sort code selection sorting implememntation in c++ select sort c++ code basic block of selection sort in theory of computation basic block of selection sort in toc basic block of selection sort list the steps that selection sort algorithm what is selection sort selection sort c Write a pseudocode for selection sort algorithm, and prove that the algorithm is correct. selection sort pseudocode selection sort c code selection sortr Write a menu driven program to sort an array using selection sort &amp; Radix sort. Display each iteration of sorting. selection sort in c++ selection sort descending order selection sort in c+ selection sort algorithm c++ selection sort c++ Select sortc++ Give the sort algorithm by selection c++ selection sort selection sorting selection sorting algorithms selection sor in cpp Write a menu driven program to sort an array using selection sort &amp; Radix sort. Display each iteration of sorting selection sort algorithm, selection sort select method selection sort in c sorting array using delection sort in ascending order in c++ GEEKS for geeks sequential sort sorting algorithm selection sort selection sort code c++ python selection sort selection sort
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source