forked from Blankj/awesome-java-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
34 lines (30 loc) · 815 Bytes
/
Copy pathBubbleSort.java
File metadata and controls
34 lines (30 loc) · 815 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
package com.java.practice.sort;
/**
* Created by richard02.zhang on 18/1/26.
*/
public class BubbleSort {
private static void swap(int[] arr, int x, int y) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
private static void bubbleSort(int[] arr) {
if (null == arr || 0 == arr.length) {
return;
}
for (int i = 0; i < arr.length; i++){
for (int j = 0; j < arr.length; j++) {
if (arr[i] <= arr[j]) {
swap(arr, i, j);
}
}
}
}
public static void main(String[] args) {
int[] arr = {4, 5, 2, 2, 7, 1, 9, 8};
bubbleSort(arr);
for (int i = 0; i < arr.length; i++){
System.out.println(arr[i]);
}
}
}