Sissejuhatus valiku sortimisse Java-s

Valik Sort Java-s on sortimismeetod, mis leiab sortimata osast pidevalt väikseima elemendi ja hoiab seda alguses (kasvavas järjekorras sortimiseks). Protsessi korratakse, kuni sisendmassiiv on sorteeritud. Samuti jagame jaotuses Valik sortimise sisendmassiivi kaheks alammassiiviks, kus ühte massiivi kasutatakse sorteeritud elementide jaoks ja teist massiivi sorteerimata elementide jaoks. Alguses ei ole sorteeritud alammassiivis ühtegi elementi. Vaadakem järgmises jaotises üksikasjalikult valiku sorteerimise toimimist.

Kuidas Java-s valik sortimine töötab

Valiku sortimine toimib lihtsal viisil, kus see hoiab sisendmassiivist kaks alammassiivi. Nemad on:

  • Sorteeritud alammass, et säilitada sorteeritud elemendid
  • Sorteerimata alammass sortimata elementide hoidmiseks.

Algoritm:

Järgmine on algoritm, mida kasutatakse valiku Sortimiseks

  1. Seadke minimaalne (MIN) osuti asukohta 0.
  2. Massiivi elementide loendist leiate väikseima elemendi
  • Vahetage minimaalne element väljaga 0
  1. Viige kursor MIN järgmisesse kohta
  2. Korda protseduuri, kuni sisendmassiiv on sorteeritud.

Mõistagem valiku sorteerimist näitega. Järgmine on sisendmassiiv, mis tuleb sortida. Pakssinise värvi elemendid on sorteeritud massiivi osa.

1. samm : määrake MIN-kursor esimesse kohta. Niisiis, MIN osuti osutab 15-le.

Väikseim: = 15

2. samm : leidke väikseim element, võrreldes seda ülejäänud elementidega. Kui võrrelda 15 ja 21, siis 15 on väikseim. Niisiis, väikseim sel juhul ei muutu.

Väikseim: = 15

Võrreldes 15 ja 6, 6 on väikseim.

Väikseim: = 6

Võrreldes 6 ja 3, 3 on väikseim.

Väikseim: = 3

3 on ka sel juhul väiksem, kuna 19 on suurem kui 3.

Väikseim: = 3

Väikseim: = 3

Lõpuks leitakse selles iteratsioonis 3 väikseim.

3. samm : vahetage väikseim element välja positsiooniga 0.

4. samm: suurendage MIN-i kursorit järgmisse asendisse.

5. samm: leidke järgmine väikseim element, võrreldes seda ülejäänud elementidega.

Väikseim: = 21

Väikseim: = 6

Väikseim: = 6

Väikseim: = 6

Väikseim: = 6

6. samm: vahetage väikseim element elemendi vastu asukohas 1.

Korda protseduuri, kuni moodustub sorteeritud massiiv, nagu allpool näidatud.

Näited valiku sorteerimiseks Java-s

Nagu eespool juba mainitud, põhineb valiku sorteerimine miinimumi leidmisel ja vahetusel. Nüüd vaatame, kuidas Java-valiku abil sorteerida.

Java-programm elementide sortimiseks massiivis valiku sortimise abil

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

Näite väljund:

Ülaltoodud programmis on meil kaks meetodit - peamised meetodid ja müümismeetod. Põhimeetod nimetab argumendiks sisendmassiivi edastava sortimismeetodi. Minimaalne element tuvastatakse ja vahetatakse elemendiga, millele osutab MIN.

Valiku sortimist saab kasutada ka siis, kui sisendmassiiv pole koodis määratletud. Vaatame, kuidas see allpool asuvat programmi kasutades töötab.

Java programm elementide sortimiseks, kasutades valikut 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)+ " ");
)
)
)

Näite väljund:

Siin võrreldakse kasutaja antud sisestuselemente ajutise muutujaga ja vahetatakse. Protsessi korratakse, kuni moodustub sorteeritud massiiv.

Valiku sortimise tulemuslikkus

Seda sorteerimistehnikat kasutatakse selle lihtsuse ja teatud muude toimivuseeliste osas võrreldes teiste sorteerimistehnikatega.

Järeldus

Valiku sort ei tööta suurtes loendites tõhusalt, kuna see võtab võrdlemiseks rohkem aega. Valiku sortimine on meetod, mille korral sisendmassiiv jagatakse kaheks alammassiiviks, et hoida neid sorteeritud ja sortimata elementidena. Massiivi minimaalset elementi vahetatakse koos esimese positsiooniga elemendiga ja protsess jätkub, kuni moodustub sorteeritud massiiv.

Soovitatavad artiklid

See on juhend valiku Java Java sortimiseks. Siin käsitleme sissejuhatust, töötamist ja valiku sortimist koos mõne näitega. Lisateabe saamiseks võite vaadata ka järgmisi artikleid -

  1. Ühenda sortimine Java-s
  2. Hunnik sorteerib Java
  3. Kopeeri konstruktor Java-s
  4. Tähemustrid Java-s
  5. Hunnik sorteerimine Pythonis

Kategooria: