Bevezetés a válogatásba Java-ban

Kijelölés Sort a Java-ban egy olyan válogatási módszer, amely folyamatosan megtalálja a legkisebb elemet a válogatás nélküli részben, és kezdetben tartja (növekvő sorrendben). A folyamatot addig ismételjük, amíg a bemeneti tömb rendezve van. Ezenkívül a Kiválasztás sorrendben a bemeneti tömböt két alsó tömbre osztjuk, ahol az egyik tömböt válogatott elemekhez, a másik tömböt válogatás nélküli elemekhez használjuk. Kezdetben nem lesznek elemek a rendezett alcsoportban. Lásd a következő szakaszban részletesen a válogatás működését.

Hogyan működik a kiválasztási rendezés a Java-ban

A szelekciós rendezés egyszerű módon működik, amikor két al-mátrixot tart meg a bemeneti tömbtől. Ők:

  • Rendezett alcsoport, hogy megőrizze a rendezett elemeket
  • Válogatás nélküli alcsoport, hogy megőrizze a nem válogatott elemeket.

Algoritmus:

Az alábbiakban bemutatjuk a kiválasztási rendezéshez használt algoritmust

  1. Állítsa a minimális (MIN) mutatót 0-ra.
  2. Keresse meg a legkisebb elemet a tömb elemeinek listájából
  • Cserélje ki a minimális elemet 0-ra
  1. Vigye a MIN mutatót a következő helyzetbe
  2. Ismételje meg a folyamatot, amíg a bemeneti tömb rendezve van.

Értelmezzük egy példával a kiválasztási rendezést. Az alábbiakban látható a bemeneti tömb, amelyet rendezni kell. A vastag kék színű elemek a rendezett tömb részét képezik.

1. lépés : Állítsa a MIN mutatót az első helyre. Tehát a MIN mutató 15-re mutat.

Legkisebb: = 15

2. lépés : Keresse meg a legkisebb elemet összehasonlítva a többi elemmel. A 15. és a 21. összehasonlításával a 15-es a legkisebb. Tehát a legkisebb ebben az esetben nem változik.

Legkisebb: = 15

A 15-ös és a 6-os összehasonlításban a legkisebb.

Legkisebb: = 6

A 6. és a 3. összehasonlításával a 3-as a legkisebb.

Legkisebb: = 3

A 3 érték ebben az esetben is kisebb, mivel a 19 nagyobb, mint 3.

Legkisebb: = 3

Legkisebb: = 3

Végül ebben az iterációban a 3 a legkisebb.

3. lépés : Cserélje ki a legkisebb elemet az 0-as helyre.

4. lépés: Növelje a MIN mutatót a következő helyzetbe.

5. lépés: Keresse meg a következő legkisebb elemet, összehasonlítva azt a többi elemmel.

Legkisebb: = 21

Legkisebb: = 6

Legkisebb: = 6

Legkisebb: = 6

Legkisebb: = 6

6. lépés: Cserélje ki a legkisebb elemet az 1. helyen található elemre.

Ismételje meg a folyamatot, amíg az alább látható módon válogatott tömb képződik.

Példák a kiválasztás végrehajtására Rendezés Java-ban

Mint fentebb már említettük, a szelekció a minimál megtalálásán és a cserén alapul. Most nézzük meg, hogyan valósíthatjuk meg a választási rendezést a Java segítségével.

Java program az elemek tömbbe rendezéséhez a Kiválasztási rendezés segítségével

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

Minta output:

A fenti programban két módszer-fő módszer és eladási mód van. A fő módszer az eladási rendezési módszert hívja argumentumként a bemeneti tömb átadására. A minimális elem azonosítása és cseréje megtörténik a MIN jelzéssel.

A választási rendezés akkor is használható, ha a bemeneti tömb nincs meghatározva kódban. Nézzük meg, hogyan működik az alábbi program.

Java program az elemek rendezéséhez a Selection Sort segítségével

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

Minta output:

Itt a felhasználó által megadott bemeneti elemeket összehasonlítják az ideiglenes változóval, és felcserélik őket. A folyamatot addig ismételjük, amíg válogatott tömb jön létre.

Kiválasztási rendezés teljesítménye

Ezt a válogatási technikát az egyszerűség kedvéért és bizonyos egyéb teljesítmény előnyeihez használják más válogatási technikákhoz képest.

Következtetés

A válogatás nem működik hatékonyan a nagy listákon, mivel több időt vesz igénybe az összehasonlításhoz. A szelekciós rendezés olyan módszer, amelyben egy bemeneti tömböt két alsó tömbre osztanak fel, hogy azok rendezett és válogatás nélkül maradjanak. A tömb minimális elemét felcseréljük, amikor az elem az első helyzetben van, és a folyamat addig folytatódik, amíg egy rendezett tömb kialakul.

Ajánlott cikkek

Ez egy útmutató a Java választás szerinti rendezéshez. Itt a Bevezetés, a Munka és a Kiválasztási Rendezés teljesítménye, néhány példával együtt vitatkozunk. A következő cikkeket is megnézheti további információkért -

  1. Merge Sort a Java alkalmazásban
  2. Heap Sort in Java
  3. Másoló kivitelező Java-ban
  4. Csillagminták Java-ban
  5. Halom Rendezés Pythonban

Kategória: