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

Sign up
Appearance settings

Commit 568daa2

Browse files
二分查找变异写法
1 parent 3ac57e0 commit 568daa2

File tree

2 files changed

+144
-33
lines changed

2 files changed

+144
-33
lines changed

‎src/main/java/com/algorithm/study/demo/algorithm/FindProject.java‎

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
public class FindProject {
1515
public static void main(String[] args) {
1616

17-
// System.out.println(search(is,is.length-1,6));
17+
int[] is=new int[]{3,5,6,7,10};
18+
System.out.println(binary_search_xiaoyu(is,10));
1819
// System.out.println(binarySearch(is,2));
1920
// Shape redRectangle = new RedShapeDecorator(new Rectangle());
2021
// redRectangle.doShaper();
@@ -95,7 +96,7 @@ public static int binary_search(int[] srcArray, int des){
9596
int height=srcArray.length-1;//最高下标
9697
int middle=0;//中间下标
9798
while (low<=height){
98-
middle=(low+height)/2;//等价于 low+(height-low)/2
99+
middle=(low+height)>>1;//等价于 low+(height-low)>>1
99100
// middle=low+(des-srcArray[low])/(srcArray[height]-srcArray[low])*(height-low);//插值算法-适合分布均匀的数据查找
100101
System.out.println("middle:"+middle);
101102
if (des==srcArray[middle]){
@@ -109,4 +110,107 @@ public static int binary_search(int[] srcArray, int des){
109110
return -1;
110111

111112
}
113+
114+
/**
115+
* 二分查找第一个值等于给定值的元素
116+
* @param srcArray
117+
* @param des
118+
* @return
119+
*/
120+
public static int binary_search_first(int[] srcArray,int des){
121+
int low=0;
122+
int height=srcArray.length-1;
123+
int middle=0;
124+
while (low<=height){
125+
middle=(low+height)>>1;
126+
if (des>srcArray[middle]){
127+
low=middle+1;
128+
}else if(des<srcArray[middle]){
129+
height=middle-1;
130+
}else{
131+
if (middle==0 || srcArray[middle-1]!=des){
132+
return middle;
133+
}else{
134+
height=middle-1;
135+
}
136+
}
137+
}
138+
return -1;
139+
}
140+
141+
/**
142+
* 二分查找最后一个个值等于给定值的元素
143+
* @param srcArray
144+
* @param des
145+
* @return
146+
*/
147+
public static int binary_search_last(int[] srcArray,int des){
148+
int low=0;
149+
int height=srcArray.length-1;
150+
int middle=0;
151+
while (low<=height){
152+
middle=(low+height)>>1;
153+
if (des>srcArray[middle]){
154+
low=middle+1;
155+
}else if(des<srcArray[middle]){
156+
height=middle-1;
157+
}else{
158+
if (middle==0 || srcArray[middle+1]!=des){
159+
return middle;
160+
}else{
161+
low=middle+1;
162+
}
163+
}
164+
}
165+
return -1;
166+
}
167+
168+
/**
169+
* 二分查找第一个值大于等于给定值的元素
170+
* @param srcArray
171+
* @param des
172+
* @return
173+
*/
174+
public static int binary_search_dayu(int[] srcArray,int des){
175+
int low=0;
176+
int height=srcArray.length-1;
177+
int middle=0;
178+
while (low<=height){
179+
middle=(low+height)>>1;
180+
if (srcArray[middle]>=des){
181+
if (middle==0 || srcArray[middle-1]<des){
182+
return middle;
183+
}else{
184+
height=middle-1;
185+
}
186+
}else{
187+
low=middle+1;
188+
}
189+
}
190+
return -1;
191+
}
192+
193+
/**
194+
* 查找最后一个小于等于给定值的元素
195+
* @param srcArray
196+
* @param des
197+
* @return
198+
*/
199+
public static int binary_search_xiaoyu(int[] srcArray,int des){
200+
int low=0;
201+
int height=srcArray.length-1;
202+
int middle=0;
203+
while (low<=height){
204+
middle=(low+height)>>1;
205+
if (srcArray[middle]>des){
206+
height=middle-1;
207+
}else{
208+
if (middle==srcArray.length-1 || srcArray[middle+1]>des)
209+
return middle;
210+
else
211+
low=middle+1;
212+
}
213+
}
214+
return -1;
215+
}
112216
}

‎src/main/java/com/algorithm/study/demo/algorithm/SortProject.java‎

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.algorithm.study.demo.algorithm;
22

33
import com.algorithm.study.demo.model.User;
4+
import com.alibaba.fastjson.JSON;
45

56
import java.lang.reflect.Method;
67
import java.util.Arrays;
@@ -24,14 +25,10 @@ public class SortProject {
2425
"heapSort",
2526
"binaryTreeSort"
2627
};
27-
public static void main(String[] args) {
28-
int[] ls=new int[]{1,30,15,11,40};
29-
quick(ls);
30-
// try {
31-
// performanceTest(10000);
32-
// } catch (Exception e) {
33-
// e.printStackTrace();
34-
// }
28+
public static void main(String[] args) throws Exception {
29+
int[] ls=new int[]{30,1,15,11,40};
30+
Method method = SortProject.class.getDeclaredMethod(methodNames[4], ls.getClass());
31+
method.invoke(SortProject.class.newInstance(),ls);
3532
}
3633

3734
/**
@@ -58,10 +55,14 @@ public static void performanceTest(int len) throws Exception{
5855
}
5956
}
6057
/**
61-
* 冒泡排序
62-
* 两两相邻比较记录的关键字,如果反序就交换,直到没有反序的记录为止。
58+
* 冒泡排序:两两相邻比较记录的关键字,如果反序就交换,直到没有反序的记录为止。
59+
* 原地排序算法
60+
* 稳定排序算法
61+
* 时间复杂度为O(n2)
62+
* 空间复杂度为O(1)
6363
*/
6464
private static void maopaoSort(int score[]){
65+
System.out.println("排序前:"+ JSON.toJSONString(score));
6566
boolean flag=true;//数据发生了交换才继续冒泡
6667
for (int i = 1; i < score.length && flag; i++){ //最多做n-1趟排序
6768
flag=false;
@@ -73,12 +74,8 @@ private static void maopaoSort(int score[]){
7374
flag=true;//发生了数据交换
7475
}
7576
}
76-
// System.out.print("第" + (i) + "趟排序结果:");
77-
// for(int a = 0; a < score.length; a++){
78-
// System.out.print(score[a] + "\t");
79-
// }
80-
// System.out.println("");
8177
}
78+
System.out.println("排序后:"+ JSON.toJSONString(score));
8279
}
8380

8481
/**
@@ -93,9 +90,13 @@ private static void listSort(int ls[]){
9390
/**
9491
* 简单选择排序算法
9592
* 每一趟从待排序的记录中选出最小的元素,顺序放在已排好序的序列最后,直到全部记录排序完毕
93+
* 空间复杂度为O(1)
94+
* 非稳定排序
95+
* 时间复杂度为O(n2)
9696
* 性能上优于冒泡
9797
*/
9898
private static void selectSort(int ls[]){
99+
System.out.println("排序前:"+ JSON.toJSONString(ls));
99100
for (int i=0;i<ls.length-1;i++){
100101
int min=i;//记录数字最小的那个值的索引
101102
for (int j=(i+1);j<ls.length;j++){
@@ -108,35 +109,34 @@ private static void selectSort(int ls[]){
108109
ls[i]=ls[min];
109110
ls[min]=temp;
110111
}
111-
// System.out.print("第" + (i + 1) + "趟排序结果:");
112-
// for(int a = 0; a < ls.length; a++){
113-
// System.out.print(ls[a] + "\t");
114-
// }
115-
// System.out.println("");
116112
}
113+
System.out.println("排序后:"+ JSON.toJSONString(ls));
117114
}
118115

119116
/**
120117
* 插入排序算法
121118
* 插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕。
119+
* 将数组中的数据分为两个区间,已排序区间和未排序区间。初始已排序区间只有一个元素,就是数组的第一个元素。
120+
* 空间复杂度为O(1)
121+
* 稳定排序
122+
* 时间复杂度为O(n2)
122123
* 性能优于选择排序、冒泡排序
123-
* {69, 70, 2, 87}
124124
*/
125125
private static void insertSort( int ls[]){
126+
System.out.println("排序前:"+ JSON.toJSONString(ls));
126127
for (int i=1;i<ls.length;i++){
127128
int key = ls[i];//需要插入的元素
128129
int j = i-1;//已经排好序的末索引
129-
for(;j>=0&&key<ls[j];j--){
130-
ls[j+1]=ls[j]; //将大于temp的值整体后移一个单位
130+
for(;j>=0;j--){
131+
if (key<ls[j]) {
132+
ls[j + 1] = ls[j];//将大于temp的值整体后移一个单位
133+
}else {
134+
break;
135+
}
131136
}
132137
ls[j+1] = key;
133-
134-
// System.out.print("第" + (i + 1) + "趟排序结果:");
135-
// for(int a = 0; a < ls.length; a++){
136-
// System.out.print(ls[a] + "\t");
137-
// }
138-
// System.out.println("");
139138
}
139+
System.out.println("排序后:"+ JSON.toJSONString(ls));
140140
}
141141
/**
142142
* 希尔排序(Shell)算法
@@ -229,9 +229,11 @@ public static void quickSortByList(List<User> list, int low, int high) {
229229
quickSortByList(list, hi, high);
230230
}
231231
public static void quick(int[] a2) {
232+
System.out.println("排序前:"+JSON.toJSONString(a2));
232233
if (a2.length > 1) { //查看数组是否为空
233234
quickSort(a2, 0, a2.length - 1);
234235
}
236+
System.out.println("排序后:"+JSON.toJSONString(a2));
235237
}
236238

237239
/**
@@ -272,10 +274,14 @@ public static int[] merge(int[] a, int[] b){
272274
/**
273275
* 归并排序<br>
274276
* 把数组从中间一分为二,并对左右两部分递归调用,直到数组长度为1的时候,开始两两归并;<br>
277+
* 递推公式:
278+
* merge_sort(p...r) = merge(merge_sort(p...q), merge_sort(q+1...r))
279+
* 终止条件:
280+
* p >= r 不用再继续分解
275281
* 时间复杂度: 平均:O(nlogn),最好:O(nlogn);最坏:O(nlogn);
276282
* 空间复杂度: O(n);要为归并的结果分配空间
277-
* @param a 待排序数组;
278-
* @return 有序数组;
283+
* 稳定排序算法
284+
* 不是原地排序算法
279285
*/
280286
public static int[] mergeSort(int[] a){
281287
if(a.length==1){
@@ -292,6 +298,7 @@ public static int[] mergeSort(int[] a){
292298
}
293299

294300

301+
295302
/**
296303
* 堆排序<br>
297304
* 堆的定义:堆是一个完全,或近似完全的二叉树,堆顶元素的值大于左右孩子的值,左右孩子也需要满足这个条件;<br>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /