-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
45 lines (36 loc) · 788 Bytes
/
Copy pathBubbleSort.java
File metadata and controls
45 lines (36 loc) · 788 Bytes
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
// Jonathan Rumley
// CSC161-101
// November 12, 2020
// Exercise 7 Bubble Sort
//
public class BubbleSort
{
public static void main(String[] args)
{
int[] nums = new int[25];
for(int i = 0; i<nums.length; i++)
nums[i] = (int)(Math.random() * 12345);
printArray(nums);
bubbleSort(nums);
printArray(nums);
}
static void bubbleSort(int arry[])
{
int n = arry.length;
for(int i =0; i < n-1; i++)
for(int j = 0; j < n-i-1; j++)
if(arry[j] > arry[j+1])
{
int temp = arry[j];
arry[j] = arry[j+1];
arry[j+1] = temp;
}
}
static void printArray(int arry[])
{
int n = arry.length;
for(int i = 0; i < n; i ++)
System.out.println(arry[i] + "");
System.out.println();
}
}