1. 선형 테이블 검색 :
1.1. 이진 검색 방법을 만듭니다 ::
public static ArrayList<Integer> binarySeach(int[] arr, int left, int right, int findVal){
if(left > right){
return new ArrayList<Integer>();
}
int mid = (left+right)/2;
int minVal = arr[mid];
if(findVal > minVal){
return binarySeach(arr, mid+1, right, findVal);
}else if(findVal < minVal){
return binarySeach(arr, left, mid-1, findVal);
}else{
ArrayList<Integer> resIndexList = new ArrayList<Integer>();
int temp = mid-1;
while (true){
if ((temp < 0) || (arr[temp] != findVal)){
break;
}
resIndexList.add(temp);
temp -= 1;
}
resIndexList.add(mid);
temp = mid + 1;
while (true){
if ((temp > arr.length-1) || (arr[temp] != findVal)){
break;
}
resIndexList.add(temp);
temp += 1;
}
return resIndexList;
}
}
1.2. 주요 방법 :
public static void main(String[] args) {
int arr[] = {
0, 1, 8, 10, 89, 100, 100, 100, 123};
List<Integer> resIndexList = binarySeach(arr, 0, arr.length-1, 100);
System.out.println("resIndexList: "+ resIndexList);
}
• 2020 년 9 월 14 일 ChiKong_Tam 작성