selection sort program in c
snippet in c

selection sort in java

user8370

public static void SelectionSort(int[] arr)
{
  int small;
  for (int i = 0; i <arr.length - 1; i++)
  {
    small = i;
    for (int j = i + 1; j < arr.length; j++)
    {
      //if current position is less than previous smallest
      if (arr[j] < arr[small])
      {
        small = j;
        
        //swap values
        int temp = arr[i];
        arr[i] = arr[small];
        arr[small] = temp; 
      }
  	}
  }
}

how to make a selection sort C++

user9386

// C++ program for implementation of selection sort  
#include <bits/stdc++.h> 
using namespace std; 
  
void swap(int *xp, int *yp)  
{  
    int temp = *xp;  
    *xp = *yp;  
    *yp = temp;  
}  
  
void selectionSort(int arr[], int n)  
{  
    int i, j, min_idx;  
  
    // One by one move boundary of unsorted subarray  
    for (i = 0; i < n-1; i++)  
    {  
        // Find the minimum element in unsorted array  
        min_idx = i;  
        for (j = i+1; j < n; j++)  
        if (arr[j] < arr[min_idx])  
            min_idx = j;  
  
        // Swap the found minimum element with the first element  
        swap(&arr[min_idx], &arr[i]);  
    }  
}  
  
/* Function to print an array */
void printArray(int arr[], int size)  
{  
    int i;  
    for (i=0; i < size; i++)  
        cout << arr[i] << " ";  
    cout << endl;  
}  
  
// Driver program to test above functions  
int main()  
{  
    int arr[] = {64, 25, 12, 22, 11};  
    int n = sizeof(arr)/sizeof(arr[0]);  
    selectionSort(arr, n);  
    cout << "Sorted array: \n";  
    printArray(arr, n);  
    return 0;  
}  
  
// This is code is contributed by rathbhupendra 

selection sort program in c

user2287

#include<stdio.h>
int main(){
   /* Here i & j for loop counters, temp for swapping,
    * count for total number of elements, number[] to
    * store the input numbers in array. You can increase
    * or decrease the size of number array as per requirement
    */
   int i, j, count, temp, number[25];

   printf("How many numbers u are going to enter?: ");
   scanf("%d",&count);

   printf("Enter %d elements: ", count);
   // Loop to get the elements stored in array
   for(i=0;i<count;i++)
      scanf("%d",&number[i]);
 
   // Logic of selection sort algorithm
   for(i=0;i<count;i++){
      for(j=i+1;j<count;j++){
         if(number[i]>number[j]){
            temp=number[i];
            number[i]=number[j];
            number[j]=temp;
         }
      }
   }

   printf("Sorted elements: ");
   for(i=0;i<count;i++)
      printf(" %d",number[i]);

   return 0;
}

selection sort

user2616

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

user943

// Por ter uma complexidade alta,
// não é recomendado para um conjunto de dados muito grande.
// Complexidade: O(n²) / O(n**2) / O(n^2)
// @see https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html

function selectionSort(vetor) {
    let minor;
    for (let i = 0; i < vetor.length - 1; i += 1) {
        minor = i;
        for (let j = i + 1; j < vetor.length; j += 1) {
            if (vetor[j] < vetor[minor]) {
                minor = j;
            }
        }
        if (i !== minor) {
            [vetor[i], vetor[minor]] = [vetor[minor], vetor[i]];
        }
    }
    return vetor;
}

selectionSort([1, 2, 5, 8, 3, 4]);

how to send values to a selection sorting function

user1948

 1  // Fig. 8.15: fig08_15.cpp
 2  // This program puts values into an array, sorts the values into
 3  // ascending order and prints the resulting array.
 4  #include <iostream>
 5  using std::cout;
 6  using std::endl;
 7
 8  #include <iomanip>
 9  using std::setw;
10
11  void selectionSort( int * const, const int ); // prototype
12  void swap( int * const, int * const ); // prototype
13
14  int main()
15  {
16     const int arraySize = 10;
17     int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
18
19     cout << "Data items in original order\n";
20
21     for ( int i = 0; i < arraySize; i++ )
22        cout << setw( 4 ) << a[ i ];
23
24     selectionSort( a, arraySize ); // sort the array
25
26     cout << "\nData items in ascending order\n";
27
28     for ( int j = 0; j < arraySize; j++ )
29        cout << setw( 4 ) << a[ j ];
30
31     cout << endl;
32     return 0; // indicates successful termination
33  } // end main
34
35  // function to sort an array
36  void selectionSort( int * const array, const int size )
37  {
38     int smallest; // index of smallest element
39
40     // loop over size - 1 elements
41     for ( int i = 0; i < size - 1; i++ )
42     {
43        smallest = i; // first index of remaining array
44
45        // loop to find index of smallest element
46        for ( int index = i + 1; index < size; index++ )
47
48           if ( array[ index ] < array[ smallest ] )
49              smallest = index;
50
51        swap( &array[ i ], &array[ smallest ] );
52     } // end if
53  } // end function selectionSort
54
55  // swap values at memory locations to which                  
56  // element1Ptr and element2Ptr point                         
57  void swap( int * const element1Ptr, int * const element2Ptr )
58  {                                                            
59     int hold = *element1Ptr;                                  
60     *element1Ptr = *element2Ptr;                              
61     *element2Ptr = hold;                                      
62  } // end function swap