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.2
10
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;
}
}
Thank you!
10
0
3.8
5
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)
}
}
Thank you!
5
0
4
6
#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;
}
Thank you!
6
0
4
5
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);
}
}
Thank you!
5
0
4
4
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] < 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
Thank you!
4
0
0
0
//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);
}
}
}
}
Thank you!
0
0
4.17
6
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))
Thank you!
6
0
4.63
8
// C algorithm for SelectionSort
void selectionSort(int arr[], int n)
{
for(int i = 0; i < n-1; i++)
{
int min = i;
for(int j = i+1; j < n; j++)
{
if(arr[j] < arr[min])
min = j;
}
if(min != i)
{
// Swap
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
}
Thank you!
8
0
3.75
4
//selection sort; timecomplexity=O(n^2);space complexity=O(n);auxiliary space complexity=O(1)
#include <iostream>
using namespace std;
void swap(int*,int*);
void selection_sort(int arr[],int n)
{
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
swap(&arr[i],&arr[j]);
}
}
}
}
void display(int arr[],int n)
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
int main()
{
int n;
cout<<"enter the size of the array:"<<endl;
cin>>n;
int array_of_numbers[n];
cout<<"enter the elements of the array:"<<endl;
for(int i=0;i<n;i++)
{
cin>>array_of_numbers[i];
}
cout<<"array as it was entered"<<endl;
display(array_of_numbers,n);
cout<<"array after sorting:"<<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;
}
Thank you!
4
0
Are there any code examples left?
New code examples in category Java
-
Java 2022-03-27 21:35:04 Sort string array in case insensitive order and case sensitive order java
-
Java 2022-03-27 21:25:10 java -jar -l resources\es.porperties -i ejemplo.txt -o inject.bin
-
Java 2022-03-27 21:20:21 Debug & Fix a 2-Dimensional Array Java Console Application
-
Java 2022-03-27 20:40:19 TreeSet headSet(E toElement) method in java
-
Java 2022-03-27 19:30:06 close keyboard android
-
Java 2022-03-27 19:20:40 java measure execution time
-
Java 2022-03-27 19:10:06 how to add cardview support in android studio
-
Java 2022-03-27 18:55:08 android studio lower case letters on a button