Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite414753

Browse files
Merge pull requestTheAlgorithms#83 from varunu28/master
Updated BubbleSort.java
2 parents90ebd8a +9f0d7b6 commite414753

File tree

1 file changed

+63
-51
lines changed

1 file changed

+63
-51
lines changed

‎Sorts/BubbleSort.java

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,70 @@
1-
importjava.util.Scanner;
2-
31
/**
4-
* This class implements BubbleSort
5-
*
6-
* @author Unknown
2+
*
3+
* @author Varun Upadhyay (https://github.com/varunu28)
74
*
85
*/
96

107
classBubbleSort
118
{
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+
}
5870
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp