Created
September 28, 2013 14:02
-
-
Save MaratB/6742362 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ctime> | |
#include <stdio.h> | |
#include <conio.h> | |
#include <stdlib.h> | |
const int N = 10; | |
main() { | |
int i,j, A[N],nmin,c,m,L=0,R=N-1,x,flag; | |
srand(time(NULL)); | |
for ( i = 0; i < N; i ++ ) | |
A[i] = rand()%(21)-10; | |
printf("\n Исходный массив:\n"); | |
for ( i = 0; i < N-1; i ++ ) | |
printf("%d ", A[i]); | |
for ( i=0; i<N-1; i++) { | |
nmin=i; | |
for (j=i+1; j<N; j++) | |
if (A[j]<A[nmin]) | |
nmin=j; | |
if (nmin!=i) { | |
c=A[i]; | |
A[i]=A[nmin]; | |
A[nmin]=c; | |
} | |
} | |
printf("\nОтсортированный массив: \n"); | |
for ( i = 0; i < N-1; i ++ ) | |
printf("%d ", A[i]); | |
printf("\nВведите искомый элемент: \n"); | |
scanf("%d", &x); | |
while (L<=R){ | |
m = (L+R)/2; | |
if (A[m]==x){ | |
flag=1; | |
break; | |
} | |
if (x<A[m]) R = m-1; | |
else L = m+1; | |
} | |
if (flag) | |
printf("Нашли: A[%d]=%d", m, A[m]); | |
else printf( "Такого эдемента нет" ); | |
clock(); | |
getch(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment