Skip to content

Commit c0a8b78

Browse files
author
weizhaoquan
committed
两数之和
1 parent 2a462ab commit c0a8b78

2 files changed

Lines changed: 60 additions & 32 deletions

File tree

src/main/java/cn/byhieg/arithmetic/BubbleSort.java

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package cn.byhieg.arithmetic.sort;
2+
3+
/**
4+
* Created by weizhaoquan on 2019/04/17.
5+
*/
6+
public class BubbleSort {
7+
8+
static void swap (int[] arr,int i,int j){
9+
int temp = arr[i];
10+
arr[i]=arr[j];
11+
arr[j]=temp;
12+
}
13+
static void bubbleSort1(int[] arr){
14+
for (int end = arr.length-1;end>0;end--) {
15+
boolean isSort = true;
16+
for (int i = 0 ;i<end;i++) {
17+
if (arr[i]>arr[i+1]) {
18+
swap( arr,i,i+1 );
19+
isSort = false;
20+
}
21+
}
22+
if (isSort) {
23+
break;
24+
}
25+
}
26+
}
27+
28+
public static int[] bubbleSort(int[] array){
29+
if (array.length == 0) {
30+
return array;
31+
}
32+
for (int i = 0;i<array.length-1;i++) {
33+
for (int j=0;j<array.length -1-i;j++ ) {
34+
if (array[j+1]<array[j]) {
35+
int temp = array[j+1];
36+
array[j+1] = array[j];
37+
array[j] = temp;
38+
}
39+
}
40+
}
41+
42+
return array;
43+
}
44+
45+
public static void main(String[] args) {
46+
int[] arr = {6,3,5,7,3,7,8,9,1};
47+
int[] ints = bubbleSort( arr );
48+
System.out.printf( "1排序后的数组为:" );
49+
for (int num :ints) {
50+
System.out.printf( num+" " );
51+
}
52+
System.out.println( );
53+
int[] arr1 = {6,3,5,7,3,7,8,9,1};
54+
bubbleSort1( arr1 );
55+
System.out.printf( "2排序后的数组为:" );
56+
for (int num :arr1) {
57+
System.out.printf( num+" " );
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)