Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement a search algorithm in Java

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to achieve a search algorithm in Java. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Linear search (linear search) public class LinearSearchDemo {public static void main (String [] args) {int [] data = {2,1,4,6,12,7}; int target = 12; int searchIndex = search (data, target); if (searchIndex! =-1) {System.out.println ("found at:" + searchIndex) } else {System.out.println ("not found");} / * @ param data Array to be looked up * @ param target the value to be looked up * @ return int the index of the target value in the array, if not found, return-1 * / public static int search (int [] data, int target) {int length = data.length / / iterate through each value in the array from scratch, and return its index for (int I = 0; I) if the target value is found.

< length; i++) { if (data[i] == target) { return i; } } //代码能走到这一步就说明上面的循环遍历结束了也没找到目标值 //即目标值不存在于数组中 return -1; }}二分查找( binary search) 二分查找的关键点其实是数据顺序的有序,数据顺序不有序的话,用不了二分查找的 //二分查找:在有序数组中查找某一特定元素的搜索算法public class BinarySearch { public static void main(String[] args) { int[] data = {1, 5, 6, 12, 15, 19, 23, 26, 30, 33, 37, 42, 53, 60}; int target = 19; int index = binarySearch3(data, 0, data.length - 1, target); if (index >

-1) {System.out.println ("found:" + index);} else {System.out.println ("not found") }} / * * Recursive method to achieve binary search * @ param data sorted array (here it is assumed to be sorted from small to large) * @ param from start position * @ param to stop position * @ value to find by param target * @ the position in the array of the value to be found by return If it is not found, return-1 * / private static int binarySearch2 (int [] data, int from, int to, int target) {if (from target) {/ / the middle value is larger than the target value, then continue to look for return binarySearch2 (data, from, mid-1, target) in the right half. } else {/ / found, put the found last because in most cases the intermediate value is either greater or less, which saves the operation of return mid;}} return-1. } / * * non-recursive method to achieve binary search * @ param data sorted array (here it is assumed to be sorted from small to large) * @ param from start position * @ param to stop position * @ value to find by param target * @ the position in the array of the value to be found by return If not, return-1 * / private static int binarySearch3 (int [] data, int from, int to, int target) {while (from target) {to = mid-1 } else {/ / found, put the found last because in most cases the intermediate value is either greater or less, which saves the operation of return mid;}} return-1. }} on how to achieve a search algorithm in Java to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report