function - int binary_search( int data[ ], int num, int key...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Bubble sort void bubble_sort( int x[ ] , int n ) { int i, j, temp; for ( i = 0; i < n-1 ; i++ ) { for ( j = i + 1; j < n ; j++ ) { if ( x[i] > x[j] ) { temp = x[i] ; x[i] = x[j] ; x[j] = temp ; } } } } Linear Search int linear_search ( int x[], int n, int key ) { int i; for (i=0; i<n; i++) { if (x[i]== key) return i; } return (-1); } Binary Search
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: int binary_search( int data[ ], int num, int key ) { int lower = 0, upper = num - 1, mid; while ( lower &lt;= upper ) { mid = (lower + upper)/2; if ( key &lt; data[mid]) upper = mid - 1; else if ( key &gt; data[mid]) lower = mid + 1; else return mid; } return (-1); }...
View Full Document

Ask a homework question - tutors are online