Last active
December 21, 2016 18:03
-
-
Save ZaeemSattar/c8d0824d740a890ac88aaed10a58f343 to your computer and use it in GitHub Desktop.
Simplest example of Linear Search in java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dorid; | |
import java.awt.LinearGradientPaint; | |
public class LinearSearchClass { | |
static int [] numAarray; | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
numAarray=new int[]{3,2,4,5,6,6,7,8,9,9,0,9}; | |
int result= LinearSearch(numAarray, 7); | |
if(result==-1) | |
{ | |
System.out.print("not in array"); | |
} | |
else | |
System.out.print("Number found at index "+result); | |
} | |
private static int LinearSearch(int [] array,int search) | |
{ | |
for(int i=0;i<array.length;i++) | |
{ | |
if(array[i]==search) | |
{ | |
return i; | |
} | |
} | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment