aktuelle.kurse/m411/docs/Daten-Uebungen-CodeBeispiele/BubbleSortExample.txt

50 lines
905 B
Plaintext
Raw Normal View History

2021-08-06 18:08:13 +02:00
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]);
}
}
}