티스토리 뷰
Sequential Search Algorithm (Linear Search) 순차탐색
Less used than binary search and Hash Table. Big-O notation for Sequential Search is O(n) which is relatively slower than others.
public class Main {
public static void main(String[] args) {
int arr[] = {7, 4, 3, 1, 5};
int x = 3;
int answer = linearSearch(arr, x);
if(answer == -1){
System.out.println("No element matched");
}else{
System.out.println("Element is present at index " + answer); //2
}
}
private static int linearSearch(int arr[], int x) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == x) {
return i;
}
}
return -1;
}
}
|
Reference
'Algorithms' 카테고리의 다른 글
Comparison performing contains() for ArrayList and HashSet (0) | 2019.08.31 |
---|---|
Insertion Sort (0) | 2019.08.01 |
Selection Sort (0) | 2019.07.27 |
Bubble Sort (0) | 2019.07.25 |
Recursion in Java (0) | 2019.07.24 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- C++
- javascript
- hackerrank javascript solution
- hackerrank javascript
- 알고리즘
- string class in java
- hackerrank
- java
- compareTo()
- ... in Javascript
- repeat()
- math.abs
- easy javascript algorithm
- easy algorithm
- 프로그래머스 알고리즘문제
- Object type casting
- Javascript Algorithm
- equals()
- rest parameter
- HashMap
- spring boot application
- Collection Framework
- math.max
- algorithm
- HackerRank Algorithm
- 프로그래머스
- hackerrank solution
- 프로그래머스 알고리즘
- substring()
- code refactoring
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함