|
1 |
| -importjava.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 | classBubbleSort
|
11 | 8 | {
|
12 |
| -/** |
13 |
| - * Main Method |
14 |
| - * |
15 |
| - * @param args Command line arguments |
16 |
| - */ |
17 |
| -publicstaticvoidmain(String[]args) |
18 |
| -{ |
19 |
| -intsize =6; |
20 |
| -intarray[]=newint[size]; |
21 |
| -booleanswap; |
22 |
| -intlast =size -1; |
23 |
| -Scannerinput=newScanner(System.in); |
24 |
| - |
25 |
| - |
26 |
| -//Input |
27 |
| -System.out.println("Enter any 6 Numbers for Unsorted Array : "); |
28 |
| -for(inti=0;i<6;i++) |
29 |
| -{ |
30 |
| -array[i]=input.nextInt(); |
31 |
| -} |
32 |
| - |
33 |
| -//Sorting |
34 |
| -do |
35 |
| - { |
36 |
| -swap =false; |
37 |
| -for (intcount =0;count <last;count++) |
38 |
| - { |
39 |
| -if (array[count] >array[count +1]) |
40 |
| - { |
41 |
| -inttemp =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(inti=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 | +publicstatic <TextendsComparable<T>>voidBS(Tarray[],intlast) { |
| 18 | +//Sorting |
| 19 | +booleanswap; |
| 20 | +do |
| 21 | + { |
| 22 | +swap =false; |
| 23 | +for (intcount =0;count <last-1;count++) |
| 24 | + { |
| 25 | +intcomp =array[count].compareTo(array[count +1]); |
| 26 | +if (comp >0) |
| 27 | + { |
| 28 | +Ttemp =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 | +publicstaticvoidmain(String[]args) |
| 40 | + { |
| 41 | +// Integer Input |
| 42 | +int[]arr1 = {4,23,6,78,1,54,231,9,12}; |
| 43 | +intlast =arr1.length; |
| 44 | +Integer[]array =newInteger[last]; |
| 45 | +for (inti=0;i<last;i++) { |
| 46 | +array[i] =arr1[i]; |
| 47 | + } |
| 48 | + |
| 49 | +BS(array,last); |
| 50 | + |
| 51 | +// Output => 1 4 6912235478231 |
| 52 | +for(inti=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 cde |
| 65 | +for(inti=0;i<last;i++) |
| 66 | + { |
| 67 | +System.out.print(array1[i]+"\t"); |
| 68 | + } |
| 69 | + } |
58 | 70 | }
|