Sie sind auf Seite 1von 5

1.

/*program-1
2. 3. Performing Binary Search on Java char Array Example This java example shows how to perform binary search for an element of java char array using Arrays class.

4.
5. */ 6.

7. import java.util.Arrays;
8.

9. public class BinarySearchCharArrayExample {


10.

11.
12.

public static void main(String[] args) { //create char array char charArray[] = {'a','b','d','e'}; /* To perform binary search on char array use int binarySearch(char[] b, char value) of Arrays class. This method searches the char array for specified char value using binary search algorithm. Please note that the char array MUST BE SORTED before it can be searched using binarySearch method. This method returns the index of the value to be searched, if found in the array. Otherwise it returns (- (X) - 1) where X is the index where the the search value would be inserted. i.e. index of first element that is grater than the search value or array.length, if all elements of an array are less than the search value. */ //sort char array using Arrays.sort method Arrays.sort(charArray); //value to search char searchValue = 'b'; //since 'b' is present in char array, index of it would be returned int intResult = Arrays.binarySearch(charArray,searchValue); System.out.println("Result of binary search of 'b' is : " + intResult);

13.
14. 15. 16.

17. 18.
19. 20.

21.
22. 23.

24.
25.

26.
27.

28. 29.
30. 31.

32. 33.
34. 35.

36.
37. 38.

39. 40.

41.

42. 43. 44. 45.


46. 47. 48.} 49. 50./* }

//lets search something which is not in char array ! searchValue = 'c'; intResult = Arrays.binarySearch(charArray,searchValue); System.out.println("Result of binary search of 'c' is : " + intResult);

51.Output would be 52.Result of binary search of 'b' is : 1 53.Result of binary search of 'c' is : -3 54.*/

Program-2 #include <stdio.h> #include <string.h> #include <ctype.h> int binarysearch (char [], int, char [], int); main () { char cum_nouns [700] [21]; int file_len=0; char noun [6] [21]; int count; location=binarysearch(cum_nouns [i] , file_len, noun[count], count); } int binarysearch (char data [], int n, char newitem [], int datapositionofnewitem) { int low, high, test, findnewitem; low=0; high=n-1; while (low<=high){ test=(low +high/2); findnewitem=strcmp (newitem[datapositionofnewitem], data[test]); if(findnewitem==0) return test; else if (findnewitem<0) high = test -1; else low=test+1; } return -1; }

Program-3 #include <stdio.h> #include <stdlib.h> #include <string.h>

char binarySearch(const char, const char*, const size_t); int main(int argc, char* argv[]) { const char* arrc = "ABCDEFGHIJKLMNOPSTUVZ"; char c = '\0'; char f; if(2 != argc) { fprintf(stderr,"USAGE: ./exec [character]\n"); exit(EXIT_FAILURE); } else { c = *(argv[1]); }

printf("Finding '%c' inside '%s'\n\n", c, arrc); f = binarySearch(c, arrc, strlen(arrc)); if(f) { printf("Element Found: %c\n", f); } else { printf("Nothing Found } return 0; } \n");

char binarySearch(const char ele, const char* str, const size_t sz) { char retchar = '\0'; size_t begin = 0; size_t end = sz; size_t mid; while(begin <= end) { char fnd; mid = (begin + end) / 2; fnd = *(str + mid); /* printf("*str = %c, *(str + %u) = %c\n", *str, mid, *(str +

mid)); */ printf("fnd = %c, ele = %c\n", fnd, ele); printf("begin = %u, end = %u, mid = %u\n", begin, end, mid); if(ele > fnd) { printf("'%c' > '%c'\n", ele, fnd); begin = mid + 1; } else if(ele < fnd) { printf("'%c' < '%c'\n", ele, fnd); end = mid - 1; } else { printf("'%c' == '%c'\n", ele, fnd); retchar = fnd; break; } printf("-----------------------------------------\n\n"); } return retchar; } ===================== OUTPUT ======================= [arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra binary-search.c [arnuld@dune programs]$ ./a.out USAGE: ./exec [character] [arnuld@dune programs]$ ./a.out e Finding 'e' inside 'ABCDEFGHIJKLMNOPSTUVZ' fnd = K, ele = e begin = 0, end = 21, mid = 10 'e' > 'K' ----------------------------------------fnd = S, ele = e begin = 11, end = 21, mid = 16 'e' > 'S' ----------------------------------------fnd = V, ele = e begin = 17, end = 21, mid = 19 'e' > 'V' ----------------------------------------fnd = Z, ele = e begin = 20, end = 21, mid = 20 'e' > 'Z' -----------------------------------------

fnd = , ele = e begin = 21, end = 21, mid = 21 'e' > '' -----------------------------------------

Das könnte Ihnen auch gefallen