mirror of
https://gitlab.com/harald.mueller/aktuelle.kurse.git
synced 2024-11-24 10:41:56 +01:00
50 lines
905 B
Plaintext
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]);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
}
|