有序数组 与二分查找算法的理解
- 有序数组是指数组里的数是按照规定次序排序的,
- 虽然仍然是一些书,但是排列次序不同,看作是不同的数组
- 同时有序数组的优点会比无序数组快得多
- 但是不好的方面是在插入操作中由于所有靠后的数据都需要移动以腾开空间,所以速度比较慢
- 有序数组和无序数组中的删除操作都很慢,这是因为数据项必须向前移动来填补已删除数据项的洞
- 有序数组在查找频繁的情况下十分有用,但是若插入与删除较为频繁时,则无法高效的工作
- 二分查找的条件是对有序的数组进行排序,
- 二分查找需要的时间与数组中数据项的个数成正比
- 循环中的每一步将范围缩小一半,最终这个范围会小到无法再分割
二分查找算法的思路
- 一个有序数组,想查找一个数字key的下标,首先算出中间的下边mid,利用min把这个数组分成两半,前一半是0到mid-1,后一半从mid+1到数组最后的一个元素(下标是数组长度-1)。首先是把查找的元素和中间的元素mid比较,然后判断这个元素是大于还是小于,大于的话就走后一半(也就是mid+1到数组的最后一个元素 数组长度-1),反之小于就走前面的一半(0到mid-1),这样反复去中间取中间比较最后就会定位要获取到的元素的下标。
二分查找有两种实现方式
- 1.非递归实现
- 2.递归实现
非递归方式实现
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; int low = 0; int high = arr.length-1; // 查找的元素 int i = 6; while (low <= high) { int mid =low + (high - low) / 2; if (i == arr[mid]) { System.out.println("查找成功" + i + "在下标" + mid + "处"); break; } else if (i < arr[mid]) { high = mid - 1; System.out.println("查找的下标小于此中间下标,当前下标为"+mid); } else { low = mid + 1; System.out.println("查找的下标大于中间的下标,当前下标为"+mid); }
<span class="token punctuation">}</span>
输出
递归方式实现
public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; System.out.println(binSearch(arr, 0, arr.length - 1, 6)); }
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">binSearch</span><span class="token punctuation">(</span><span class="token keyword">int</span> arr<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">int</span> start<span class="token punctuation">,</span> <span class="token keyword">int</span> end<span class="token punctuation">,</span> <span class="token keyword">int</span> key<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> mid <span class="token operator">=</span> start <span class="token operator">+</span> <span class="token punctuation">(</span>end <span class="token operator">-</span> start<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>arr<span class="token punctuation">[</span>mid<span class="token punctuation">]</span> <span class="token operator">==</span> key<span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"查找成功"</span> <span class="token operator">+</span> key <span class="token operator">+</span> <span class="token string">"在下标"</span> <span class="token operator">+</span> mid <span class="token operator">+</span> <span class="token string">"处"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> mid<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>start <span class="token operator">>=</span> end<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>key <span class="token operator">></span> arr<span class="token punctuation">[</span>mid<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"查找的下标小于此中间下标,当前下标为"</span> <span class="token operator">+</span> mid<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token function">binSearch</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> mid <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">,</span> end<span class="token punctuation">,</span> key<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>key <span class="token operator"><</span> arr<span class="token punctuation">[</span>mid<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"查找的下标大于中间的下标,当前下标为"</span> <span class="token operator">+</span> mid<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token function">binSearch</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> start<span class="token punctuation">,</span> mid <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">,</span> key<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
输出