Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.lang.reflect.Array;
- import java.util.Arrays;
- import java.util.Scanner;
- public class Algorithm4 {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.print("Please input the number of elements :");
- int n = Integer.parseInt(scan.nextLine());
- char[] arr = new char[n]; // Define array for elements
- for (int i = 0; i < n; i++) // Input values to array
- {
- System.out.printf("Please insert the [%d] element of array :", i);
- arr[i] = scan.next().charAt(0);
- }
- System.out.println("BEFORE SORTING :");
- System.out.println(Arrays.toString(arr));
- boolean swap;
- do {
- swap = false;
- for (int i = 0; i < arr.length - 1; i++) {
- if (arr[i] > arr[i + 1]) {
- int free = arr[i];
- arr[i] = arr[i + 1];
- arr[i + 1] = (char) free;
- swap = true;
- }
- }
- } while (swap) ;
- System.out.println("AFTER SORTING :");
- System.out.println(Arrays.toString(arr));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement