Knowee
Questions
Features
Study Tools

Java Searching and Sorting Programs

Question

Java Searching and Sorting Programs

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, I can help with that. Here are some basic examples of searching and sorting programs in Java:

  1. Linear Search:
public class Main {
    public static int linearSearch(int[] array, int key) {    
        for(int i=0;i<array.length;i++){    
            if(array[i] == key){    
                return i;    
            }    
        }    
        return -1;    
    }    
    public static void main(String a[]){    
        int[] a1= {10,20,30,50,70,90};    
        int key = 50;    
        System.out.println(key+" is found at index: "+linearSearch(a1, key));    
    }    
} 
  1. Binary Search:
public class Main {
    int binarySearch(int array[], int x, int low, int high) {
        if (high >= low) {
            int mid = low + (high - low) / 2;
            if (array[mid] == x)
                return mid;

This problem has been solved

Similar Questions

Which sorting algorithm(s) Java library uses?

sort a numeric array and a string arrayWrite a Java program to sort a numeric array and a string array.

Write a java program to implement interfaces sort the set of numbers and a set of strings in a given array

Which Java data structure is used to implement the sorting algorithm without extra space?

answer this question in simple code for beginners using intellJ: 4. Write a Java program to sort an array of given integers using the Merge Sort Algorithm.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.