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 sorting Rule and search algorithm in java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces the java sorting rules and search algorithm how to achieve, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.

1. Recursive algorithm

Recursion means that the method calls itself, and each call passes in a different variable, which makes the code concise. In computer science, recursive algorithm refers to a method to solve a problem by repeatedly decomposing a problem into similar sub-problems. Recursive method can be used to solve many computer science problems, so it is a very important concept in computer science.

Basic case: printing data through recursion

Public class M01_Recursion {public static void main (String [] args) {printNum (3);} private static void printNum (int num) {if (num > 1) {printNum (num-1);} System.out.println ("num=" + num);}}

Recursive diagram:

Based on the characteristics of the stack structure, the recursive call will form the above structure. When all the recursive methods are successfully put into the stack, they will execute the stack action in turn and print the data results.

In practical development, recursion is often used to approach tree structure problems, factorial algorithms, sorting search and other mathematical problems.

The condition of the recursive algorithm must be constantly close to the exit condition, otherwise it is easy to cause memory overflow exception caused by infinite loop.

Second, sorting algorithm

Sorting algorithm is to make a group of data records, according to a specific sorting strategy, increasing or decreasing the operation; commonly used sorting algorithms: bubble sorting, selection sorting, insertion sorting, Hill sorting, merging sorting, fast sorting, cardinality sorting, etc.; sorting algorithm selection: different sorting business can pass a variety of algorithm tests, low complexity, short time-consuming priority.

1. Bubble sorting

By comparing the values of the adjacent elements in turn, if the reverse order is found, the elements with larger values are gradually moved from the front to the back. The name of the algorithm comes from the fact that the smaller elements will slowly float to one end of the sequence through the sort exchange, just as the bubbles of carbon dioxide in carbonated drinks will eventually float to the top, hence the name bubbling sorting.

Public class M02_Bubble {public static void main (String [] args) {int [] arr = {3pje 7je 5je 9je 6}; bubbleSort (arr); for (int num:arr) {System.out.println (num);}} public static void bubbleSort (int [] arr) {/ / declare the temporary variable int temp = 0 / / the total number of trips sorted for (int I = 0; I

< arr.length - 1; i++) { // 元素交换 for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] >

Arr [j + 1]) {/ / position exchange temp = arr [j]; arr [j] = arr [j + 1]; arr [j + 1] = temp;}

Core ideas:

Only how many elements are sorted, and the number of times to be processed in theory; the position exchange of each element requires a complete comparison, and the total control of the outer loop. The inner layer loops through the exchange of individual element positions.

2. Select sort

Select the sorting principle: select the smallest (or largest) element from the data elements to be sorted for the first time, store it at the beginning of the sequence, and then find the smallest (largest) element from the remaining unsorted elements. and put it at the end of the sorted sequence. And so on until the number of all data elements to be sorted is zero.

Public class M03_Selection {public static void main (String [] args) {int [] arr = {30 int I = 0; selectionSort (arr);} public static void selectionSort (int [] arr) {for (int I = 0; I)

< arr.length - 1; i++) { int minIndex = i; int minData = arr[i]; for (int j = i + 1; j < arr.length; j++) { // 假设最小值判断 if (minData >

Arr [j]) {/ / swap small value minData = arr [j]; / / reset minIndex, increment minIndex = j }} / / minimum exchange is placed at arr [0] position if (minIndex! = I) {arr [minIndex] = arr [I]; arr [I] = minData;} System.out.println ("th" + (item1) + "round sort:" + Arrays.toString (arr)) }}}

Output result:

First round sorting: [30, 70, 50, 90, 60] second round sorting: [30, 50, 70, 90, 60] third round sorting: [30, 50, 60, 90, 70] fourth round sorting: [30, 50, 60, 70, 90] 3, insertion sorting

The basic idea is to insert a record into an ordered table, take the first element from the unordered table every time in the sorting process, compare it with the ordered table element in turn, and insert it into the appropriate position in the ordered table. make it a new ordered table. In the implementation process, a double-layer loop is used, the outer loop looks for all elements except the first element, and the inner loop looks up and moves the ordered table in front of the current element.

Public class M04_Insert {public static void main (String [] args) {int [] arr = {10 arr 40 arr 90 arr 20 int insertValue 80}; insertSort (arr);} public static void insertSort (int [] arr) {int insertValue = 0; int insertIndex = 0; for (int I = 1; I

< arr.length; i++) { // 待插入数的值和下标 insertValue = arr[i]; insertIndex = i - 1; // 写入位置 while (insertIndex >

= 0 & & insertValue

< arr[insertIndex]) { arr[insertIndex + 1] = arr[insertIndex]; insertIndex--; } if (insertIndex + 1 != i) { arr[insertIndex + 1] = insertValue; } System.out.println("第" + i + "轮插入排序:"+Arrays.toString(arr)); } }} 输出结果: 第1轮插入排序:[10, 40, 90, 20, 80]第2轮插入排序:[10, 40, 90, 20, 80]第3轮插入排序:[10, 20, 40, 90, 80]第4轮插入排序:[10, 20, 40, 80, 90]三、查找算法 查找算法是指在一组元素中寻找一个特定的信息元素,在计算机应用中,查找是常用的基本运算,例如编译程序中符号表的查找;常用的查找算法有:顺序查找,二分查找,插值查找,斐波那契查找。 1、顺序查找 顺序查找是按照序列原有顺序对一组元素进行遍历,并与要查找的元素逐个比较的基本查找算法。 public class M05_OrderFind { public static void main(String[] args) { String[] arr = {"first","second","third"}; System.out.println(seqSearch(arr,"second")); } public static int seqSearch(String[] arr, String value) { // 数组下标,-1代表没有 int findIndex = -1 ; // 遍历并逐个对比 for (int i = 0; i < arr.length; i++) { if(value.equals(arr[i])) { return i ; } } return findIndex ; }}2、二分查找 二分查找也称折半查找(Binary Search),它是一种效率较高的查找方法。但是,折半查找要求线性表必须采用顺序存储结构,而且表中元素按关键字有序排列。 public class M06_BinaryFind { public static void main(String[] args) { int arr[] = { 10, 20, 30, 40, 50 }; int index = binarySearch (arr, 0, arr.length - 1, 40); System.out.println("index="+index); } public static int binarySearch(int[] arr, int leftIndex, int rightIndex, int findValue) { // leftIndex >

RightIndex, no if (leftIndex > rightIndex) {return-1;} int midIndex = (leftIndex + rightIndex) / 2; int midValue = arr [midIndex]; / / left recursive if (findValue)

< midValue) { return binarySearch(arr, leftIndex, midIndex - 1, findValue); // 向右递归 } else if (findValue >

MidValue) {return binarySearch (arr, midIndex + 1, rightIndex, findValue); / / find} else {return midIndex;}} directly

If the elements to be queried are out of order, you can sort them first and then find them based on the sorting algorithm in module 2 above.

Thank you for reading this article carefully. I hope the article "how to implement sorting rules and search algorithms in java" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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