본문 바로가기

Programming/C

[알고리즘]Bubble Sort & Binary Search

Bubble Sort & Binary Search

#include 
#include 
#pragma warning(disable: 4996)
 
 
void bubble_sort(int _first[], int _length, int _find){
    int temp;
    int i = 0;
    for (i = 0; i < _length; i++){ //Bubble  Sort start
        for (int j = 1; j < _length; j++){
            if (_first[j - 1] > _first[j]){
                temp = _first[j - 1];
                _first[j - 1] = _first[j];
                _first[j] = temp;
            }
 
 
        }
    }
    for (i = 0; i < _length; i++){
        printf("_first[%d] => %d \n", i, _first[i]);
    }
    printf("\n");
    int left = 0;
    int right = _length - 1;
    while (left<=right){ //Binary Search start
        int middle = (left + right) / 2; // (0+7)/2 = 3
        if (_first[middle] < _find){ // _first[3] < 7 true left= 4
            left = middle + 1;
        }
        else if (_first[middle] > _find){
            right = middle - 1;
        }else{
            printf("%d => %d 번째 배열\n", _find, middle);
            break;
        }
    }
}
int main(int _argc,char *_argv){
    int size[8] = {1,9,4,5,7,6,27,2};
    bubble_sort(size, 8, 1);
 
 
    return 0;
 
 
}