Industrielle Fertigung
Industrielles Internet der Dinge | Industrielle Materialien | Gerätewartung und Reparatur | Industrielle Programmierung |
home  MfgRobots >> Industrielle Fertigung >  >> Industrial programming >> Java

Bubble-Sort-Algorithmus in Java:Array-Sortierprogramm &Beispiel

Was ist Bubble Sort?

Bubble Sort ist ein einfacher Algorithmus, der das erste Element des Arrays mit dem nächsten vergleicht. Wenn das aktuelle Element des Arrays numerisch größer ist als das nächste, werden die Elemente vertauscht. Ebenso durchläuft der Algorithmus das gesamte Element des Arrays.

In diesem Tutorial erstellen wir ein JAVA-Programm zur Implementierung von Bubble Sort. Überprüfen Sie die Ausgabe des Codes, der Ihnen hilft, die Programmlogik zu verstehen.

Java-Programm zum Überprüfen der Armstrong-Nummer

package com.guru99;
 
public class BubbleSort {
 
	public static void main(String[] args)
	{  
		int arr[] ={860,8,200,9};  
 
		System.out.println("---Array BEFORE Bubble Sort---"); 
		
	    printArray(arr);
 
		bubbleSort(arr);//sorting array elements using bubble sort  
 
		System.out.println("---Array AFTER Bubble Sort---");  
		
		printArray(arr); 
 
	}  
 	static void bubbleSort(int[] array)
	{  
		int n = array.length;  
		int temp = 0;  
		for(int i=0; i < n; i++) // Looping through the array length
		{  System.out.println("Sort Pass Number "+(i+1)); 
			for(int j=1; j < (n-i); j++)
			{  
			    System.out.println("Comparing "+ array[j-1]+ " and " + array[j]);    
				if(array[j-1] > array[j])
				{   
				    
					//swap elements  
					temp = array[j-1];  
					array[j-1] = array[j];  
					array[j] = temp;  
				    System.out.println(array[j]  + " is greater than " + array[j-1]);
				    System.out.println("Swapping Elements: New Array After Swap");
					printArray(array);
				}  
 
			}  
		}  
 
	} 
	
	static void printArray(int[] array){
	    
	    for(int i=0; i < array.length; i++)
		{  
			System.out.print(array[i] + " ");  
		} 
	    System.out.println();
	    
	}
}

Ausgabe:

860 8 200 9 
Sort Pass Number 1
Comparing 860 and 8
860 is greater than 8
Swapping Elements: New Array After Swap
8 860 200 9 
Comparing 860 and 200
860 is greater than 200
Swapping Elements: New Array After Swap
8 200 860 9 
Comparing 860 and 9
860 is greater than 9
Swapping Elements: New Array After Swap
8 200 9 860 
Sort Pass Number 2
Comparing 8 and 200
Comparing 200 and 9
200 is greater than 9
Swapping Elements: New Array After Swap
8 9 200 860 
Sort Pass Number 3
Comparing 8 and 9
Sort Pass Number 4
---Array AFTER Bubble Sort---
8 9 200 860

Java

  1. Java-Hello-World-Programm
  2. Java-Kopie-Arrays
  3. So erstellen Sie ein Array von Objekten in Java
  4. Java String charAt() Methode mit Beispiel
  5. Java-String-EndsWith()-Methode mit Beispiel
  6. Konstruktorüberladung in Java:Was ist &Programmbeispiele
  7. Java-Programm zum Überprüfen der Primzahl
  8. Bubble-Sort-Algorithmus in Java:Array-Sortierprogramm &Beispiel
  9. Insertion Sort Algorithmus in Java mit Programmbeispiel
  10. Auswahlsortierung im Java-Programm mit Beispiel