Answers for "selection sort analysis"

C++
6

selection sort

#include <bits/stdc++.h>

using namespace std; 

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

    return 0;  
}
Posted by: Guest on January-16-2021
1

Selection sort

SelectionSort(List) {
  for(i from 0 to List.Length) {
    SmallestElement = List[i]
    for(j from i to List.Length) {
      if(SmallestElement > List[j]) {
        SmallestElement = List[j]
      }
    }
    Swap(List[i], SmallestElement)
  }
}
Posted by: Guest on April-25-2021
1

selection sort

//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<Integer> arr = Stream.of(buffer.readLine().replaceAll(("\s+$"), "").split(" ")).map(Integer::parseInt)
                .collect(toList());

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

        System.out.println(arr);
    }

    public static void swap(List<Integer> 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<Integer> arr, int high) {
        for (int i = 0; i <= high - 1; i++) {
            steps(arr, i, high);
        }
    }

    public static void steps(List<Integer> arr, int start, int high) {
        for (int i = start; i <= high - 1; i++) {
            if (arr.get(i) < arr.get(start)) {
                swap(arr, start, i);
            }
        }
    }
}
Posted by: Guest on June-11-2021
1

selection sort

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))
Posted by: Guest on September-30-2020
0

selection sort

void sort(int *arr, int n){
        
        // Incrementa di 1 il limite inferiore del sub array da ordinare
        for (int i = 0; i < n-1; i++) 
        { 
            // Trova il minimo nel subarray da ordinare
            int indice_min = i; 
            for (int j = i+1; j < n; j++) {
                
                // Confronto per trovare un nuovo minimo
                if (arr[j] < 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;
    }
    
}
Posted by: Guest on April-04-2021

Browse Popular Code Answers by Language