Search
 
SCRIPT & CODE EXAMPLE
 

C

search sorted array c

// Search sorted array of integers "arr" of lenght "arr_len" for value "key"
// Return index of "key" if "key" is found, else return -1

int search_sorted_array(int arr[], int arr_len, int key) {

    int low = 0, high = arr_len - 1;
    int middle;

    if (arr_len < 1) return -1; // If array is empty return -1

    while ( high >= low ) {

        middle = (low + high) / 2 ;	// Get the middle of the array

        if ( arr[middle] == key ) return middle; // Return index of found key

        else if ( arr[middle] > key ) high = middle - 1;

        else low = middle + 1;

    }
	
  	// Key not in array, return -1
    return -1;
}
Comment

PREVIOUS NEXT
Code Example
C :: leggere stringhe con spazio in mezzo c 
C :: powershell list big files 
C :: esp8266 wifi.config does not work 
C :: pointer arithmetic on Arrray in c 
C :: c str add int 
C :: user define function in c 
C :: how to arrange a 2d array based on string length in c 
C :: how to join an array of strings c 
C :: c size_t 
C :: while loop in c 
C :: c unused variable 
C :: size of operator in c language 
C :: what is the last character of a string in c 
C :: man strstr 
C :: ecto where is not nil 
C :: c atoi atof 
C :: entity framework core discard changes 
C :: como somar em C 
C :: ansi c write unsigned short to file 
C :: gdebi install with yes option 
C :: OpenDaylight maven settings 
C :: perl file handling 
C :: injection 
C :: uri/beecrowd problem no - 1133 solution in C 
C :: timespec c 
C :: deepak rake 
C :: captive portal esp8266 
C :: how to use arry 
C :: not repeated serial number in c 
C :: c variable 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =