Chapter8/8-02 · master · Youngju-Jang / Javacoursework · GitLab
GitLab.com
gitlab.com
public class BinarySearchProblem {
public static void main(String[] args) {
int[] numbers = {12, 25, 31, 48, 54, 66, 70, 83, 95, 108};
int target = 83;
int left = 0;
int right = numbers.length-1;
int mid = (left + right)/2;
int temp = numbers[mid];
boolean find = false;
while(left <= right) {
if(target == temp) { //수를 찾은 경우
find = true;
break;
}
else if(target < temp) { // 찾으려는 수가 더 작은 경우
right = mid-1;
}
else {
left = mid+1;
}
mid = (left + right)/2;
temp = numbers[mid];
}
if(find == true)
{
mid++;
System.out.println("찾는 수는 " + mid + "번째 있습니다.");
}
else System.out.println("찾는 수가 없습니다.");
}
}
Chapter8/8-03 · master · Youngju-Jang / Javacoursework · GitLab
GitLab.com
gitlab.com
public class HeapSort {
private int SIZE;
private int heapArr[];
public HeapSort()
{
SIZE = 0;
heapArr = new int [50];
}
public void insertHeap(int input)
{
int i = ++SIZE;
//while(( i != 1) && ( input > heapArr[i/2])){ //max heap
while(( i != 1) && ( input < heapArr[i/2])){ //min heap
heapArr[i] = heapArr[i/2];
i = i/2;
}
heapArr[i] = input;
}
public int getHeapSize()
{
return SIZE;
}
public int deleteHeap()
{
int parent, child;
int data, temp;
data = heapArr[1];
temp = heapArr[SIZE];
SIZE -= 1;
parent =1; child = 2;
while(child <= SIZE){
//if((child < HEAP_SIZE) && (heapArr[child] < heapArr[child+1])){ //max heap
if((child < SIZE) && (heapArr[child] > heapArr[child+1])){ //min heap
child++;
}
//if(temp >= heapArr[child]) break; //max heap
if(temp <= heapArr[child]) break; //min heap
heapArr[parent] = heapArr[child];
parent = child;
child *= 2;
}
heapArr[parent] = temp;
return data;
}
public void printHeap(){
//System.out.printf("\n Max Heap : ");
System.out.printf("\n Min Heap : ");
for(int i=1; i<=SIZE; i++)
System.out.printf("[%d] ", heapArr[i]);
}
public static void main(String[] args) {
HeapSort h = new HeapSort();
h.insertHeap(80);
h.insertHeap(50);
h.insertHeap(70);
h.insertHeap(10);
h.insertHeap(60);
h.insertHeap(20);
h.printHeap();
int n, data;
n = h.getHeapSize();
for(int i=1; i<=n; i++){
data = h.deleteHeap();
System.out.printf("\n 출력 : [%d]", data);
}
}
}
ch7~ 학점 산출 프로그램 (0) | 2022.07.19 |
---|---|
ch 6.23. wait()/notify() 에서드를 활용한 동기화 프로그래밍 (0) | 2022.07.18 |
ch 6.22 멀티 Thread 프로그래밍에서의 동기화 (0) | 2022.07.18 |
ch 6.21~ thread 메소드들 (0) | 2022.07.18 |
ch6.20~22 thread 스레드 (0) | 2022.07.15 |
댓글 영역