Úvod do výberu Zoradenie v jazyku Java

Výber Zoradenie v Jave je metóda triedenia, ktorá nepretržite nájde najmenší prvok v netriedenej časti a ponechá ho na začiatku (pre zoradenie vzostupne). Proces sa bude opakovať, až kým nebude zoradené vstupné pole. Tiež pri výbere triedenia rozdelíme vstupné pole do dvoch čiastkových polí, kde jedno pole sa používa pre zoradené prvky a druhé pole je pre netriedené prvky. Na začiatku nebudú v triedenom čiastkovom poli žiadne prvky. Pozrime sa podrobne na fungovanie výberového konania v nasledujúcej časti.

Ako funguje triedenie výberu v Jave

Zoradenie výberu funguje jednoduchým spôsobom, keď z vstupného poľa zachováva dve čiastkové polia. Oni sú:

  • Zoradené čiastkové pole, aby sa zachovali zoradené prvky
  • Netriedené čiastkové pole na zachovanie netriedených prvkov.

algoritmus:

Nasleduje algoritmus, ktorý sa používa na zoradenie výberu

  1. Nastavte minimálny (MIN) ukazovateľ do polohy 0.
  2. Nájdite najmenší prvok zo zoznamu prvkov v poli
  • Zameniť minimálny prvok za umiestnenie 0
  1. Posuňte ukazovateľ MIN na ďalšiu pozíciu
  2. Tento postup opakujte, až kým sa nezoradí vstupné pole.

Pochopme výberový príklad s príkladom. Nasleduje vstupné pole, ktoré sa musí triediť. Prvky v tučnej modrej farbe budú súčasťou zoradeného poľa.

Krok 1 : Nastavte ukazovateľ MIN na prvé miesto. Takže ukazovateľ MIN ukazuje na 15.

Najmenšia: = 15

Krok 2 : Nájdite najmenší prvok porovnaním so zvyškom prvkov. Pri porovnaní 15 a 21 je 15 najmenších. Takže najmenší sa v tomto prípade nezmení.

Najmenšia: = 15

Pri porovnaní 15 a 6 je 6 najmenšia.

Najmenšia: = 6

Pri porovnaní 6 a 3 je číslo 3 najmenšie.

Najmenšia: = 3

3 bude v tomto prípade tiež menší, pretože 19 je viac ako 3.

Najmenšia: = 3

Najmenšia: = 3

Nakoniec sa v tejto iterácii zistilo, že 3 je najmenší.

Krok 3 : Vymieňajte najmenší prvok za prvok v umiestnení 0.

Krok 4: Zvýšte ukazovateľ MIN na ďalšiu pozíciu.

Krok 5: Nájdite ďalší najmenší prvok porovnaním so zvyškom prvkov.

Najmenšia: = 21

Najmenšia: = 6

Najmenšia: = 6

Najmenšia: = 6

Najmenšia: = 6

Krok 6: Vymeňte najmenší prvok za prvok v mieste 1.

Postup opakujte, kým sa nevytvorí zoradené pole, ako je uvedené nižšie.

Príklady implementácie triedenia výberu v jazyku Java

Ako už bolo uvedené vyššie, výber je založený na nájdení minima a výmene. Teraz sa pozrime, ako implementovať výberový výber pomocou jazyka Java.

Program Java na usporiadanie prvkov v poli pomocou zoradenia výberu

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));
)
)

Vzorový výstup:

V uvedenom programe máme dve metódy - hlavné metódy a metódu predaja. Hlavná metóda volá metódu sell sort, ktorá vstupuje do vstupného poľa ako argument. Minimálny prvok bude identifikovaný a zamenený za prvok označený MIN.

Triedenie výberu možno použiť aj v prípade, že vstupné pole nie je definované v kóde. Pozrime sa, ako to funguje pomocou nižšie uvedeného programu.

Program Java na usporiadanie prvkov pomocou výberu triedenia

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)+ " ");
)
)
)

Vzorový výstup:

Tu budú vstupné prvky poskytnuté používateľom porovnané s dočasnou premennou a zamenené. Proces sa bude opakovať, kým sa nevytvorí usporiadané pole.

Výkon výberu zoradenia

Táto technika triedenia sa používa pre svoju jednoduchosť a určité ďalšie výhody oproti iným triediacim technikám.

záver

Triedenie výberu nefunguje efektívne na veľkých zoznamoch, pretože na porovnanie vyžaduje viac času. Výberové triedenie je metóda, v ktorej sa vstupné pole rozdelí do dvoch čiastkových polí, aby sa zachovali triedené a netriedené prvky. Minimálny prvok v poli sa zamení s prvkom v prvej polohe a proces pokračuje, kým sa nevytvorí triedené pole.

Odporúčané články

Toto je sprievodca výberom triedenia v jazyku Java. Tu diskutujeme o úvode, práci a výkone triedenia výberu spolu s niektorými príkladmi. Ďalšie informácie nájdete aj v nasledujúcich článkoch -

  1. Zlúčiť zoradenie v jazyku Java
  2. Usporiadanie haldy v Jave
  3. Kopírovanie Constructor v Jave
  4. Hviezdne vzory v Jave
  5. Halda Zoradiť v Pythone

Kategórie: