-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
38 lines (34 loc) · 1.02 KB
/
Copy pathBubbleSort.java
File metadata and controls
38 lines (34 loc) · 1.02 KB
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
// Bubble Sort
package codingbat;
import java.util.Random;
public class BubbleSort {
static int set[] = new int[10];
public static void generateRandom(){
Random r = new Random();
System.out.println("Unsorted array:");
for(int i = 0; i < set.length; i++){
int ran = r.nextInt(100);
set[i] = ran;
System.out.print(set[i] + " ");
}
}
public static void sortArray(){
for(int i = 0; i < set.length; i++){
for(int j = 0; j < set.length-1; j++){
if(set[j] > set[j+1]){
int temp = set[j];
set[j] = set[j+1];
set[j+1] = temp;
}
}
}
System.out.println("\n\nSorted Array:");
for(int a = 0; a < set.length; a++){
System.out.print(set[a]+" ");
}
}
public static void main(String[] args) {
generateRandom();
sortArray();
}
}