Einführung in die Auswahlsortierung in Java

Auswahlsortierung in Java ist eine Sortiermethode, die das kleinste Element im unsortierten Teil ständig findet und am Anfang behält (zum Sortieren in aufsteigender Reihenfolge). Der Vorgang wird wiederholt, bis das Eingabearray sortiert ist. Außerdem unterteilen wir in Auswahlsortierung das Eingabearray in zwei Unterarrays, wobei ein Array für sortierte Elemente und ein anderes Array für unsortierte Elemente verwendet wird. Am Anfang gibt es keine Elemente im sortierten Subarray. Lassen Sie uns die Arbeitsweise der Auswahlsortierung im folgenden Abschnitt genauer betrachten.

Funktionsweise der Auswahlsortierung in Java

Die Auswahlsortierung funktioniert auf einfache Weise, indem zwei Unterfelder vom Eingabefeld ferngehalten werden. Sie sind:

  • Subarray "Sortiert", um die sortierten Elemente beizubehalten
  • Unsortiertes Subarray, um die unsortierten Elemente beizubehalten.

Algorithmus:

Das Folgende ist der Algorithmus, der für die Auswahlsortierung verwendet wird

  1. Stellen Sie den Minimalzeiger (MIN) auf Position 0.
  2. Suchen Sie das kleinste Element aus der Liste der Elemente im Array
  • Tauschen Sie das minimale Element mit der Position 0 aus
  1. Bewegen Sie den MIN-Zeiger auf die nächste Position
  2. Wiederholen Sie den Vorgang, bis das Eingabearray sortiert ist.

Lassen Sie uns die Auswahlsortierung anhand eines Beispiels verstehen. Es folgt das Eingabearray, das sortiert werden muss. Die Elemente in Fettblau werden als Teil des sortierten Arrays angezeigt.

Schritt 1 : Stellen Sie den MIN-Zeiger auf die erste Position. Der MIN-Zeiger zeigt also auf 15.

Kleinste: = 15

Schritt 2 : Finden Sie das kleinste Element, indem Sie es mit den übrigen Elementen vergleichen. Im Vergleich zu 15 und 21 ist 15 die kleinste. Das kleinste wird sich in diesem Fall also nicht ändern.

Kleinste: = 15

Beim Vergleich von 15 und 6 ist 6 die kleinste.

Kleinste: = 6

Beim Vergleich von 6 und 3 ist 3 die kleinste.

Kleinste: = 3

3 ist auch in diesem Fall kleiner, da 19 größer als 3 ist.

Kleinste: = 3

Kleinste: = 3

Schließlich wird in dieser Iteration festgestellt, dass 3 die kleinste ist.

Schritt 3 : Tauschen Sie das kleinste Element gegen das Element an Position 0 aus.

Schritt 4: Inkrementieren Sie den MIN-Zeiger auf die nächste Position.

Schritt 5: Finden Sie das nächstkleinere Element, indem Sie es mit den übrigen Elementen vergleichen.

Kleinste: = 21

Kleinste: = 6

Kleinste: = 6

Kleinste: = 6

Kleinste: = 6

Schritt 6: Tauschen Sie das kleinste Element gegen das Element an Position 1 aus.

Wiederholen Sie den Vorgang, bis sich ein sortiertes Array wie unten gezeigt gebildet hat.

Beispiele zur Implementierung von Selection Sort in Java

Wie oben bereits erwähnt, basiert die Auswahlsortierung auf dem Auffinden des Minimums und dem Vertauschen. Lassen Sie uns nun sehen, wie die Auswahlsortierung mit Java implementiert wird.

Java-Programm zum Sortieren der Elemente in einem Array mithilfe der Auswahlsortierung

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

Beispielausgabe:

Im obigen Programm haben wir zwei Methoden - Hauptmethoden und Verkaufssortiermethode. Die Methode main ruft die Methode sell sort auf, bei der das Eingabearray als Argument übergeben wird. Das minimale Element wird identifiziert und mit dem Element getauscht, auf das MIN zeigt.

Die Auswahlsortierung kann auch verwendet werden, wenn das Eingabearray nicht im Code definiert ist. Lassen Sie uns sehen, wie es mit dem folgenden Programm funktioniert.

Java-Programm zum Sortieren der Elemente mithilfe der Auswahlsortierung

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

Beispielausgabe:

Hier werden die vom Benutzer angegebenen Eingabeelemente mit der temporären Variablen verglichen und ausgetauscht. Der Vorgang wird wiederholt, bis ein sortiertes Array gebildet ist.

Leistung der Auswahlsortierung

Diese Sortiertechnik wird wegen ihrer Einfachheit und bestimmten anderen Leistungsvorteilen gegenüber anderen mehr Sortiertechniken verwendet.

Fazit

Die Auswahlsortierung funktioniert bei großen Listen nicht effizient, da sie mehr Zeit zum Vergleichen benötigt. Auswahlsortierung ist eine Methode, bei der ein Eingabearray in zwei Unterarrays unterteilt wird, um sie sortiert und nicht sortiert zu halten. Das minimale Element im Array wird mit dem Element an der ersten Position ausgetauscht, und der Prozess wird fortgesetzt, bis ein sortiertes Array gebildet wird.

Empfohlene Artikel

Dies ist eine Anleitung zum Sortieren der Auswahl in Java. Hier diskutieren wir die Einführung, das Arbeiten und die Leistung der Auswahlsortierung zusammen mit einigen Beispielen. Sie können sich auch die folgenden Artikel ansehen, um mehr zu erfahren -

  1. Sortierung in Java zusammenführen
  2. Heap-Sortierung in Java
  3. Konstruktor in Java kopieren
  4. Sternchenmuster in Java
  5. Heap-Sortierung in Python

Kategorie: