aktuelle.kurse/oldies/m411/docs/Daten-Uebungen-CodeBeispiele/BubbleSortExample.txt
Müller Harald 3fdacd20c0 muh
2022-07-28 09:14:44 +02:00

50 lines
905 B
Plaintext

import java.util.ArrayList;
public class BubbleSortExample {
public static void main(String args[]){
ArrayList<String> string;
int numbers[] = {99, -100, 566, 12, 5,
978, 5623, 463, -9, 49, 12};
int a, b, temp;
int size;
//display original array:
System.out.println("Original array is: ");
for(int i=0; i < numbers.length; i++){
System.out.println(" " + numbers[i]);
}
//bubble sort:
//outer loop: loops through the whole list
for (a=1; a < numbers.length; a++){
//inner loop: checks pairs next to each other
for(b=numbers.length-1; b >= a; b--){
if(numbers[b-1] > numbers[b]){
temp = numbers[b-1];
numbers[b-1] = numbers[b];
numbers[b] = temp;
}
}
}
//display sorted list:
System.out.println("Sorted list: ");
for(int i=0; i < numbers.length; i++){
System.out.println(" " + numbers[i]);
}
}
}