Snippets Collections
#include <bits/stdc++.h>
using namespace std;

void selection_sort(int arr[], int n)
{
    for(int i=0;i<=n-2;i++)
    {
        int mini = i;
        
        for(int j=i;j<=n-1;j++)
        {
            if(arr[mini] > arr[j])
            mini = j;
        }
        
    int temp = arr[mini];
    arr[mini] = arr[i];
    arr[i] = temp;
    }
    
    
}
int main() {
    
    int n;
    cin>>n;
    int arr[n];
    
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    
    selection_sort(arr, n);
    
    for(int i=0;i<n;i++)
    {
        cout<<arr[i]<<" ";
    }
    

    return 0;
}
import java.io.*;

class GFG {
    
    static void selectionSort(int arr[], int n){
        for(int i = 0; i < n; i++){
            int min_ind = i;
            
            for(int j = i + 1; j < n; j++){
                if(arr[j] < arr[min_ind]){
                    min_ind = j;
                }
            }
            
            int temp = arr[i];
            arr[i] = arr[min_ind];
            arr[min_ind] = temp;
        }
    }
    
	public static void main (String[] args) {
	    int a[] = {2, 1, 4, 3};
	    selectionSort(a, 4);
	    
	    for(int i = 0; i < 4; i++){
	        System.out.print(a[i] + " ");    // OUTPUT : 1 2 3 4
	    }
	}
}
#include <String.h>
#include <stdio.h>
int main() {
  // initializing the pointer string array
  char *names[] = {"tree", "bowl", "hat", "mice", "toon"};
  char *temp; // temporary variable for swaping the values
  int i, j, a;
  printf("The names are:\n");
  for (i = 0; i < 5; i++)
    printf("%s\n", names[i]);
  // arranging names in alphabetically using selection sort
  for (i = 0; i < 5; i++) {
    for (j = i + 1; j < 5; j++) {
      // compares the two string and returns an integer value
      // if the value of a is greater than 0 then swapping begins
      a = strcmp(names[i], names[j]);
      
      if (a > 0) {
        temp = names[i];
        names[i] = names[j];
        names[j] = temp;
      }
    }
  }
  printf("The arranged names are:\n");
  for (i = 0; i < 5; i++)
    printf("%s\n", names[i]);
  return 0;
}
star

Sun Jul 16 2023 12:14:20 GMT+0000 (Coordinated Universal Time)

#selectionsort
star

Tue Feb 08 2022 14:54:48 GMT+0000 (Coordinated Universal Time)

#java #gfg #geeksforgeeks #lecture #sorting #selectionsort

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension