Skip to content

Instantly share code, notes, and snippets.

@hanjae-jea
Created March 24, 2013 12:17
Show Gist options
  • Save hanjae-jea/5231675 to your computer and use it in GitHub Desktop.
Save hanjae-jea/5231675 to your computer and use it in GitHub Desktop.
[NHN NEXT] 자료구조와 알고리즘 Permutation 쉬운 버젼. & selection sort
#include <stdio.h>
#include <conio.h>
int n;
int check[10];
char list[10];
void perm(int);
void print(int);
int main(void)
{
n = 4;
perm(0);
return 0;
}
void perm(int level)
{
int i;
getch();
print(level);
if( level == n ){
return;
}
for( i = 0 ; i < n ; i ++ ){
if( check[i] == 0 ){
list[level] = 'A' + i;
check[i] = 1;
perm( level + 1 );
check[i] = 0;
list[level] = 0;
}
}
}
void print(int n)
{
int i;
if( n == 0 ) return;
printf("{'%c'", list[0]);
for( i = 1 ; i < n ; i ++ ){
printf(", '%c'", list[i]);
}
printf("}\n");
}
#include <stdio.h>
int main(void)
{
int arr[] = {1, 3, 8, 10, 12, 14, 15, 19, 25, 30};
int length = sizeof(arr)/sizeof(int); // 원소의 개수
int left = 0, right = length - 1;
int mid, key;
printf("input key = ");
scanf("%d",&key);
while( left <= right ){ // left ~ right 검색하고자 하는 범위
mid = (left + right) / 2; // auto-casting condition, 그러나 이게 가장 범용적인 코드
if( key == arr[mid] ){
printf("찾았다!");
break;
}
else if( key < arr[mid] ){
right = mid - 1;
}
else{
left = mid + 1;
}
}
if( key != arr[mid] ){
printf("못찾았다...");
}
return 0;
}
@hanjae-jea
Copy link
Author

헉. selection_sort라고 이름 붙여놓고 binary_search 코드를 올려놨어요...

천천히 고쳐볼께요 ㅠㅠ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment