|
1 | | -import java.util.Scanner; |
2 | | - |
3 | 1 | /** |
4 | | - * This class implements BubbleSort |
5 | | - * |
6 | | - * @author Unknown |
| 2 | + * |
| 3 | + * @author Varun Upadhyay (https://github.com/varunu28) |
7 | 4 | * |
8 | 5 | */ |
9 | 6 |
|
10 | 7 | class BubbleSort |
11 | 8 | { |
12 | | - /** |
13 | | - * Main Method |
14 | | - * |
15 | | - * @param args Command line arguments |
16 | | - */ |
17 | | - public static void main(String[] args) |
18 | | - { |
19 | | - int size = 6; |
20 | | - int array[]=new int[size]; |
21 | | - boolean swap; |
22 | | - int last = size - 1; |
23 | | - Scanner input=new Scanner(System.in); |
24 | | - |
25 | | - |
26 | | - //Input |
27 | | - System.out.println("Enter any 6 Numbers for Unsorted Array : "); |
28 | | - for(int i=0; i<6; i++) |
29 | | - { |
30 | | - array[i]=input.nextInt(); |
31 | | - } |
32 | | - |
33 | | - //Sorting |
34 | | - do |
35 | | - { |
36 | | - swap = false; |
37 | | - for (int count = 0; count < last; count++) |
38 | | - { |
39 | | - if (array[count] > array[count + 1]) |
40 | | - { |
41 | | - int temp = array[count]; |
42 | | - array[count] = array[count + 1]; |
43 | | - array[count + 1] = temp; |
44 | | - swap = true; |
45 | | - } |
46 | | - } |
47 | | - |
48 | | - last--; |
49 | | - } while (swap); |
50 | | - |
51 | | - //Output |
52 | | - for(int i=0; i<6; i++) |
53 | | - { |
54 | | - System.out.print(array[i]+"\t"); |
55 | | - } |
56 | | - input.close(); |
57 | | - } |
| 9 | + /** |
| 10 | + * This method implements the Generic Bubble Sort |
| 11 | + * |
| 12 | + * @param array The array to make the binary search |
| 13 | + * @param last The count of total number of elements in array |
| 14 | + * Sorts the array in increasing order |
| 15 | + **/ |
| 16 | + |
| 17 | + public static <T extends Comparable<T>> void BS(T array[], int last) { |
| 18 | + //Sorting |
| 19 | + boolean swap; |
| 20 | + do |
| 21 | + { |
| 22 | + swap = false; |
| 23 | + for (int count = 0; count < last-1; count++) |
| 24 | + { |
| 25 | + int comp = array[count].compareTo(array[count + 1]); |
| 26 | + if (comp > 0) |
| 27 | + { |
| 28 | + T temp = array[count]; |
| 29 | + array[count] = array[count + 1]; |
| 30 | + array[count + 1] = temp; |
| 31 | + swap = true; |
| 32 | + } |
| 33 | + } |
| 34 | + last--; |
| 35 | + } while (swap); |
| 36 | + } |
| 37 | + |
| 38 | + // Driver Program |
| 39 | + public static void main(String[] args) |
| 40 | + { |
| 41 | + // Integer Input |
| 42 | + int[] arr1 = {4,23,6,78,1,54,231,9,12}; |
| 43 | + int last = arr1.length; |
| 44 | + Integer[] array = new Integer[last]; |
| 45 | + for (int i=0;i<last;i++) { |
| 46 | + array[i] = arr1[i]; |
| 47 | + } |
| 48 | + |
| 49 | + BS(array, last); |
| 50 | + |
| 51 | + // Output => 1 4 6 9 12 23 54 78 231 |
| 52 | + for(int i=0; i<last; i++) |
| 53 | + { |
| 54 | + System.out.print(array[i]+"\t"); |
| 55 | + } |
| 56 | + System.out.println(); |
| 57 | + |
| 58 | + // String Input |
| 59 | + String[] array1 = {"c", "a", "e", "b","d"}; |
| 60 | + last = array1.length; |
| 61 | + |
| 62 | + BS(array1, last); |
| 63 | + |
| 64 | + //Output => a b c d e |
| 65 | + for(int i=0; i<last; i++) |
| 66 | + { |
| 67 | + System.out.print(array1[i]+"\t"); |
| 68 | + } |
| 69 | + } |
58 | 70 | } |
0 commit comments