function - int binary_search int data int num int key int...

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 <= upper ) { mid = (lower + upper)/2; if ( key < data[mid]) upper = mid - 1; else if ( key > data[mid]) lower = mid + 1; else return mid; } return (-1); }...
View Full Document

This note was uploaded on 09/04/2011 for the course EE 140 taught by Professor Lanxiang during the Spring '11 term at Montgomery College.

Ask a homework question - tutors are online