Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BubbleSort {
- public static void main(String[] args) {
- int[] nums = new int[10];
- Random random = new Random();
- System.out.print("Original array is: ");
- for (int a = 0; a < nums.length; a++) {
- nums[a] = random.nextInt(100);
- System.out.print(nums[a] + " ");
- }
- System.out.println();
- bubbleSortAscending(nums);
- System.out.println();
- bubbleSortDescending(nums);
- }
- private static void bubbleSortAscending(int[] numbers) {
- int temp;
- for (int i = 0; i < numbers.length; i++) {
- for (int j = 1; j < numbers.length; j++) {
- if (numbers[j - 1] > numbers[j]) {
- temp = numbers[j - 1];
- numbers[j - 1] = numbers[j];
- numbers[j] = temp;
- }
- }
- }
- System.out.print("Ascending order: ");
- for (int a = 0; a < numbers.length; a++) {
- System.out.print(numbers[a] + " ");
- }
- }
- private static void bubbleSortDescending(int[] numbers) {
- int temp;
- for (int i = 0; i < numbers.length; i++) {
- for (int j = 1; j < numbers.length; j++) {
- if (numbers[j] > numbers[j - 1]) {
- temp = numbers[j];
- numbers[j] = numbers[j - 1];
- numbers[j - 1] = temp;
- }
- }
- }
- System.out.print("Descending Order: ");
- for (int a = 0; a <numbers.length; a++) {
- System.out.print(numbers[a] + " ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement