Wprowadzenie do sortowania selekcyjnego w Javie
Wybór Sortuj w Javie to metoda sortowania, która nieustannie znajduje najmniejszy element w nieposortowanej części i utrzymuje go na początku (do sortowania w porządku rosnącym). Proces będzie powtarzany, aż tablica wejściowa zostanie posortowana. Ponadto w Sortowaniu selekcji podzielimy tablicę wejściową na dwie pod-tablice, w których jedna tablica jest używana do sortowania elementów, a druga tablica do elementów nieposortowanych. Na początku w posortowanej podtablicy nie będzie żadnych elementów. Zobaczmy szczegółowo działanie sortowania selekcji w poniższej sekcji.
Jak działa sortowanie selekcyjne w Javie
Sortowanie selekcji działa w prosty sposób, w którym przechowuje dwie podgrupy z tablicy wejściowej. Oni są:
- Posortowana podtablica, aby zachować posortowane elementy
- Nieposortowana podtablica do przechowywania nieposortowanych elementów.
Algorytm:
Poniżej znajduje się algorytm używany do sortowania według selekcji
- Ustaw wskaźnik minimum (MIN) na lokalizację 0.
- Znajdź najmniejszy element z listy elementów w tablicy
- Zamień minimalny element na lokalizację 0
- Przesuń wskaźnik MIN do następnej pozycji
- Powtarzaj proces, aż tablica wejściowa zostanie posortowana.
Pozwól nam zrozumieć sortowanie według przykładu. Poniżej znajduje się tablica wejściowa, którą należy posortować. Elementy w kolorze Bold Bold będą stanowić część posortowanej tablicy.
Krok 1 : Ustaw wskaźnik MIN w pierwszej lokalizacji. Tak więc wskaźnik MIN wskazuje 15.
Najmniejszy: = 15
Krok 2 : Znajdź najmniejszy element, porównując go z resztą elementów. Porównując 15 i 21, 15 jest najmniejsza. Tak więc najmniejszy nie zmieni się w tym przypadku.
Najmniejszy: = 15
Porównując 15 i 6, 6 jest najmniejsze.
Najmniejszy: = 6
Porównując 6 i 3, 3 jest najmniejszy.
Najmniejszy: = 3
3 będzie w tym przypadku również mniejsze, ponieważ 19 jest większe niż 3.
Najmniejszy: = 3
Najmniejszy: = 3
Wreszcie w tej iteracji liczba 3 jest najmniejsza.
Krok 3 : Zamień najmniejszy element na element w położeniu 0.
Krok 4: Zwiększ wskaźnik MIN do następnej pozycji.
Krok 5: Znajdź następny najmniejszy element, porównując go z resztą elementów.
Najmniejszy: = 21
Najmniejszy: = 6
Najmniejszy: = 6
Najmniejszy: = 6
Najmniejszy: = 6
Krok 6: Zamień najmniejszy element na element w położeniu 1.
Powtarzaj proces, aż utworzy się posortowana tablica, jak pokazano poniżej.
Przykłady implementacji sortowania wyboru w Javie
Jak już wspomniano powyżej, sortowanie według wyboru polega na znalezieniu minimum i zamianie. Zobaczmy teraz, jak zaimplementować sortowanie za pomocą Java.
Program Java do sortowania elementów w tablicy za pomocą sortowania zaznaczonego
import java.util.*;
public class SelSortExample (
//Method that implements Selectionsort
public static void selsort(int() arr)
(
int n=arr.length; //length of the array
for(int i=0;i (
int MIN=i; //set the first position as minimum
System.out.println("Sorting based on Number "+(i+1));
//Find the smallest element by comparing with the element in MIN position
for(int j=i+1;j (
System.out.println("Comparing "+ arr(MIN) + " and " + arr(j));
if(arr(j) (
System.out.println(arr(MIN) + " is greater than " + arr(j) );
MIN=j;
)
)
//Swap the smallest element with element in MIN position
int temp=arr(i);
arr(i)=arr(MIN);
arr(MIN)=temp;
)
)
public static void main(String() args) (
int() arr= (15, 21, 6, 3, 19, 20); // input array
System.out.println("Elements in the array before Sorting: "+ Arrays. toString (arr));
selsort (arr);//calling the selection sort method
System.out.println("Elements in the array after Sorting: "+Arrays. toString (arr));
)
)import java.util.*;
public class SelSortExample (
//Method that implements Selectionsort
public static void selsort(int() arr)
(
int n=arr.length; //length of the array
for(int i=0;i (
int MIN=i; //set the first position as minimum
System.out.println("Sorting based on Number "+(i+1));
//Find the smallest element by comparing with the element in MIN position
for(int j=i+1;j (
System.out.println("Comparing "+ arr(MIN) + " and " + arr(j));
if(arr(j) (
System.out.println(arr(MIN) + " is greater than " + arr(j) );
MIN=j;
)
)
//Swap the smallest element with element in MIN position
int temp=arr(i);
arr(i)=arr(MIN);
arr(MIN)=temp;
)
)
public static void main(String() args) (
int() arr= (15, 21, 6, 3, 19, 20); // input array
System.out.println("Elements in the array before Sorting: "+ Arrays. toString (arr));
selsort (arr);//calling the selection sort method
System.out.println("Elements in the array after Sorting: "+Arrays. toString (arr));
)
)import java.util.*;
public class SelSortExample (
//Method that implements Selectionsort
public static void selsort(int() arr)
(
int n=arr.length; //length of the array
for(int i=0;i (
int MIN=i; //set the first position as minimum
System.out.println("Sorting based on Number "+(i+1));
//Find the smallest element by comparing with the element in MIN position
for(int j=i+1;j (
System.out.println("Comparing "+ arr(MIN) + " and " + arr(j));
if(arr(j) (
System.out.println(arr(MIN) + " is greater than " + arr(j) );
MIN=j;
)
)
//Swap the smallest element with element in MIN position
int temp=arr(i);
arr(i)=arr(MIN);
arr(MIN)=temp;
)
)
public static void main(String() args) (
int() arr= (15, 21, 6, 3, 19, 20); // input array
System.out.println("Elements in the array before Sorting: "+ Arrays. toString (arr));
selsort (arr);//calling the selection sort method
System.out.println("Elements in the array after Sorting: "+Arrays. toString (arr));
)
)import java.util.*;
public class SelSortExample (
//Method that implements Selectionsort
public static void selsort(int() arr)
(
int n=arr.length; //length of the array
for(int i=0;i (
int MIN=i; //set the first position as minimum
System.out.println("Sorting based on Number "+(i+1));
//Find the smallest element by comparing with the element in MIN position
for(int j=i+1;j (
System.out.println("Comparing "+ arr(MIN) + " and " + arr(j));
if(arr(j) (
System.out.println(arr(MIN) + " is greater than " + arr(j) );
MIN=j;
)
)
//Swap the smallest element with element in MIN position
int temp=arr(i);
arr(i)=arr(MIN);
arr(MIN)=temp;
)
)
public static void main(String() args) (
int() arr= (15, 21, 6, 3, 19, 20); // input array
System.out.println("Elements in the array before Sorting: "+ Arrays. toString (arr));
selsort (arr);//calling the selection sort method
System.out.println("Elements in the array after Sorting: "+Arrays. toString (arr));
)
)
Przykładowe dane wyjściowe:
W powyższym programie mamy dwie metody - główne metody i metodę sortowania sprzedaży. Główna metoda wywołuje metodę sortowania sprzedaży, przekazując tablicę wejściową jako argument. Element minimalny zostanie zidentyfikowany i zamieniony na element wskazany przez MIN.
Sortowania wyboru można również użyć, gdy tablica wejściowa nie jest zdefiniowana w kodzie. Zobaczmy, jak to działa za pomocą poniższego programu.
Program Java do sortowania elementów za pomocą sortowania zaznaczenia
import java.util.*;
public class SelectionSortExample
(
public static void main(String args())
(
int n, i, j, tempvar;
Scanner sc = new Scanner(System.in); //To take the input from user
System.out.print("Enter the size of array : \n");
n = sc.nextInt();
int array() = new int(n); //initialising the array
System.out.print("Enter the elements that need to be inserted in the array : \n");
//inserting the elements to the array
for(i=0; i (
array(i) = sc.nextInt();
)
System.out.print("array before Sorting: \n"+ Arrays.toString(array));
System.out.print("\nSorting begins here..\n");
for(i=0; i (
for(j=i+1; j (
if(array(i) > array(j))
(
tempvar = array(i);
array(i) = array(j);
array(j) = tempvar;
)
)
)
System.out.print("Array after Sorting is :\n");
for(i=0; i (
System.out.print(array(i)+ " ");
)
)
)import java.util.*;
public class SelectionSortExample
(
public static void main(String args())
(
int n, i, j, tempvar;
Scanner sc = new Scanner(System.in); //To take the input from user
System.out.print("Enter the size of array : \n");
n = sc.nextInt();
int array() = new int(n); //initialising the array
System.out.print("Enter the elements that need to be inserted in the array : \n");
//inserting the elements to the array
for(i=0; i (
array(i) = sc.nextInt();
)
System.out.print("array before Sorting: \n"+ Arrays.toString(array));
System.out.print("\nSorting begins here..\n");
for(i=0; i (
for(j=i+1; j (
if(array(i) > array(j))
(
tempvar = array(i);
array(i) = array(j);
array(j) = tempvar;
)
)
)
System.out.print("Array after Sorting is :\n");
for(i=0; i (
System.out.print(array(i)+ " ");
)
)
)import java.util.*;
public class SelectionSortExample
(
public static void main(String args())
(
int n, i, j, tempvar;
Scanner sc = new Scanner(System.in); //To take the input from user
System.out.print("Enter the size of array : \n");
n = sc.nextInt();
int array() = new int(n); //initialising the array
System.out.print("Enter the elements that need to be inserted in the array : \n");
//inserting the elements to the array
for(i=0; i (
array(i) = sc.nextInt();
)
System.out.print("array before Sorting: \n"+ Arrays.toString(array));
System.out.print("\nSorting begins here..\n");
for(i=0; i (
for(j=i+1; j (
if(array(i) > array(j))
(
tempvar = array(i);
array(i) = array(j);
array(j) = tempvar;
)
)
)
System.out.print("Array after Sorting is :\n");
for(i=0; i (
System.out.print(array(i)+ " ");
)
)
)import java.util.*;
public class SelectionSortExample
(
public static void main(String args())
(
int n, i, j, tempvar;
Scanner sc = new Scanner(System.in); //To take the input from user
System.out.print("Enter the size of array : \n");
n = sc.nextInt();
int array() = new int(n); //initialising the array
System.out.print("Enter the elements that need to be inserted in the array : \n");
//inserting the elements to the array
for(i=0; i (
array(i) = sc.nextInt();
)
System.out.print("array before Sorting: \n"+ Arrays.toString(array));
System.out.print("\nSorting begins here..\n");
for(i=0; i (
for(j=i+1; j (
if(array(i) > array(j))
(
tempvar = array(i);
array(i) = array(j);
array(j) = tempvar;
)
)
)
System.out.print("Array after Sorting is :\n");
for(i=0; i (
System.out.print(array(i)+ " ");
)
)
)import java.util.*;
public class SelectionSortExample
(
public static void main(String args())
(
int n, i, j, tempvar;
Scanner sc = new Scanner(System.in); //To take the input from user
System.out.print("Enter the size of array : \n");
n = sc.nextInt();
int array() = new int(n); //initialising the array
System.out.print("Enter the elements that need to be inserted in the array : \n");
//inserting the elements to the array
for(i=0; i (
array(i) = sc.nextInt();
)
System.out.print("array before Sorting: \n"+ Arrays.toString(array));
System.out.print("\nSorting begins here..\n");
for(i=0; i (
for(j=i+1; j (
if(array(i) > array(j))
(
tempvar = array(i);
array(i) = array(j);
array(j) = tempvar;
)
)
)
System.out.print("Array after Sorting is :\n");
for(i=0; i (
System.out.print(array(i)+ " ");
)
)
)
Przykładowe dane wyjściowe:
Tutaj elementy wejściowe podane przez użytkownika zostaną porównane ze zmienną tymczasową i zamienione. Proces będzie powtarzany do momentu utworzenia posortowanej tablicy.
Wydajność sortowania według wyboru
Tę technikę sortowania stosuje się ze względu na jej prostotę i pewne inne zalety w zakresie wydajności w porównaniu z innymi technikami sortowania.
Wniosek
Sortowanie selekcji nie działa skutecznie na dużych listach, ponieważ zajmuje więcej czasu na porównanie. Sortowanie selekcyjne to metoda, w której tablica wejściowa zostanie podzielona na dwie pod-tablice w celu zachowania ich sortowania i nieposortowanych elementów. Minimalny element w tablicy zostanie zamieniony z elementem w pierwszej pozycji, a proces będzie kontynuowany do momentu utworzenia posortowanej tablicy.
Polecane artykuły
To jest przewodnik po Selekcji sortowania w Javie. Tutaj omawiamy wprowadzenie, działanie i wydajność sortowania selekcyjnego wraz z kilkoma przykładami. Możesz także przejrzeć następujące artykuły, aby dowiedzieć się więcej -
- Scal sortowanie w Javie
- Sortuj sterty w Javie
- Kopiuj konstruktora w Javie
- Wzory gwiazd w Javie
- Sortuj sterty w Pythonie