Uvod o odabiru sortiranja na Javi

Odabir Sortiranje u Javi je metoda sortiranja koja neprestano pronalazi najmanji element u nesortiranom dijelu i zadržava ga na početku (za razvrstavanje uzlaznim redoslijedom). Postupak će se ponavljati dok se ulazni niz ne razvrsta. Također, u Selection Sort, podijelit ćemo ulazni niz na dva podrasla, gdje se jedan niz koristi za sortirane elemente, a drugi niz za nesortirane elemente. Na početku neće biti nikakvih elemenata u razvrstanom podgrađu. Pogledajmo detaljno rad selekcije u sljedećem odjeljku.

Kako djeluje sortiranje selekcije u Javi

Odabir sortiranja djeluje na jednostavan način gdje zadržava dva podrasla od ulaznog niza. Oni su:

  • Poredani podred za zadržavanje sortiranih elemenata
  • Nortorirano pod nizu da bi se zadržali nesortirani elementi.

Algoritam:

Slijedi algoritam koji se koristi za sortiranje odabira

  1. Postavite minimalni (MIN) pokazivač na lokaciju 0.
  2. Nađite najmanji element s popisa elemenata u nizu
  • Zamijenite minimalni element s lokacijom 0
  1. Pomaknite MIN pokazivač na sljedeću poziciju
  2. Ponavljajte postupak dok se ne sortira ulazni niz.

Razjasnimo nam vrstu selekcije na primjeru. Slijedi ulazni niz koji se mora sortirati. Elementi u Bold Blue boji bit će dio razvrstanog niza.

Korak 1 : Postavite MIN pokazivač na prvo mjesto. Dakle, pokazivač MIN upućuje na 15.

Najmanji: = 15

Korak 2 : Pronađite najmanji element uspoređujući ga s ostatkom elemenata. Ako usporedimo 15 i 21, 15 je najmanji. Dakle, najmanji se neće promijeniti u ovom slučaju.

Najmanji: = 15

Ako usporedimo 15 i 6, 6 je najmanji.

Najmanji: = 6

Ako usporedimo 6 i 3, 3 je najmanji.

Najmanji: = 3

3 će u ovom slučaju biti manja, jer je 19 veća od 3.

Najmanji: = 3

Najmanji: = 3

Konačno, u ovoj se iteraciji 3 nalazi kao najmanja.

Korak 3 : Zamijenite najmanji element elementom na lokaciji 0.

Korak 4: Povećajte MIN pokazivač na sljedeću poziciju.

Korak 5: Pronađite sljedeći najmanji element uspoređujući ga s ostatkom elemenata.

Najmanja: = 21

Najmanji: = 6

Najmanji: = 6

Najmanji: = 6

Najmanji: = 6

Korak 6: Zamijenite najmanji element s elementom u lokaciji 1.

Ponavljajte postupak dok se ne formira razvrstani niz kao što je prikazano u nastavku.

Primjeri za implementaciju izborne sortiranja u Javi

Kao što je već spomenuto gore, vrsta odabira temelji se na pronalaženju minimuma i zamjeni. Sada, pogledajmo kako implementirati selekcijsku vrstu pomoću Java.

Program Java za sortiranje elemenata u nizu pomoću Sort Selection

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

Uzorak izlaza:

U gornjem programu imamo dvije glavne metode i metodu prodaje vrsta. Glavna metoda poziva metodu sortiranja prodaje prolazeći unosni niz kao argument. Minimalni element bit će identificiran i zamijenjen elementom označenim sa MIN.

Odabir sortiranja se također može koristiti tamo gdje ulazni niz nije definiran u kodu. Pogledajmo kako to funkcionira pomoću donjeg programa.

Java program za razvrstavanje elemenata pomoću Sort Sort

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

Uzorak izlaza:

Ovdje će se ulazni elementi koje daje korisnik uspoređivati ​​s privremenom varijablom i zamjenjivati. Postupak će se ponavljati sve dok se ne formira razvrstani niz.

Izvođenje sortiranja odabira

Ova tehnika sortiranja koristi se zbog svoje jednostavnosti i određenih drugih prednosti u odnosu na ostale tehnike sortiranja.

Zaključak

Vrsta odabira ne djeluje učinkovito na velikim popisima jer zahtijeva više vremena za usporedbu. Odabir sortiranja je metoda u kojoj će se ulazni niz podijeliti u dva podrasla kako bi se zadržali sortirani i nesortirani elementi. Minimalni element u nizu zamijenit će se elementom u prvom položaju i postupak se nastavlja sve dok se ne formira razvrstani niz.

Preporučeni članci

Ovo je vodič za odabir sortiranja na Javi. Ovdje smo raspravljali o uvođenju, radu i izvedbi sortiranja odabira zajedno s nekim primjerima. Možete pogledati i sljedeće članke da biste saznali više -

  1. Spajanje sortiranja u Javi
  2. Poredaj u Javi
  3. Kopiraj konstruktor u Javu
  4. Zvjezdani uzorci na Javi
  5. Poredajte u Python

Kategorija: