FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/Sorts/BubbleSortRecursion.java at master · comelf/Java · GitHub
comelf
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
Sorts
/
BubbleSortRecursion.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
55 lines (48 loc) · 1.59 KB
Breadcrumbs
Java
/
Sorts
/
BubbleSortRecursion.java
Copy path
File metadata and controls
55 lines (48 loc) · 1.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package
Sorts
;
import
java
.
util
.
Random
;
/**
* BubbleSort algorithm implements using recursion
*/
public
class
BubbleSortRecursion
implements
SortAlgorithm
{
public
static
void
main
(
String
[]
args
) {
Integer
[]
array
=
new
Integer
[
10
];
Random
random
=
new
Random
();
/* generate 10 random numbers from -50 to 49 */
for
(
int
i
=
0
;
i
<
array
.
length
; ++
i
) {
array
[
i
] =
random
.
nextInt
(
100
) -
50
;
}
BubbleSortRecursion
bubbleSortRecursion
=
new
BubbleSortRecursion
();
bubbleSortRecursion
.
sort
(
array
);
/* check array is sorted or not */
for
(
int
i
=
0
;
i
<
array
.
length
-
1
; ++
i
) {
assert
(
array
[
i
].
compareTo
(
array
[
i
+
1
]) <=
0
);
}
}
/**
* @param unsorted - an array should be sorted
* @return sorted array
*/
@
Override
public
<
T
extends
Comparable
<
T
>>
T
[]
sort
(
T
[]
unsorted
) {
bubbleSort
(
unsorted
,
unsorted
.
length
);
return
unsorted
;
}
/**
* BubbleSort algorithm implements using recursion
*
* @param unsorted array contains elements
* @param len length of given array
*/
private
static
<
T
extends
Comparable
<
T
>>
void
bubbleSort
(
T
[]
unsorted
,
int
len
) {
boolean
swapped
=
false
;
/* flag to check if array is sorted or not */
for
(
int
i
=
0
;
i
<
len
-
1
; ++
i
) {
if
(
SortUtils
.
greater
(
unsorted
[
i
],
unsorted
[
i
+
1
])) {
SortUtils
.
swap
(
unsorted
,
i
,
i
+
1
);
swapped
=
true
;
}
}
if
(
swapped
) {
bubbleSort
(
unsorted
,
len
-
1
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL