Answers for "selection sort working"

C++
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
0

Selection Sort

class Sort 
{ 
    void selectionSort(int arr[]) 
    { 
        int pos;
        int temp;
        for (int i = 0; i < arr.length; i++) 
        { 
            pos = i; 
            for (int j = i+1; j < arr.length; j++) 
           {
                if (arr[j] < 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<arr.length; i++) 
        {
            System.out.print(arr[i]+" ");
        }  
    } 
  
    public static void main(String args[]) 
    { 
        Sort ob = new Sort(); 
        int arr[] = {64,25,12,22,11}; 
        ob.selectionSort(arr); 
        ob.display(arr); 
    } 
}
Posted by: Guest on May-31-2021
0

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]
Posted by: Guest on August-16-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

Browse Popular Code Answers by Language