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

Appearance settings

Latest commit

 

History

History
History
55 lines (48 loc) · 1.59 KB

File metadata and controls

55 lines (48 loc) · 1.59 KB
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);
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.