Recursive binary search MERGE

Recursive binary search MERGE - merge(arr,start,middle,end...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Recursive binary search Int brSearch(const int arr[], const int start, const int end, const int key) { int result=-1; if(start<=end) { int middle=(start+end)/2; if(arr[middle]==key) result=middle; else if(arr[middle]>key) result=brSearch(arr,start,middle-1,key); else result=brSearch(arr,middle+1,end,key); } return result; } int binaryRSearch(const int arr[], const int size, const int key) { return brSearch(arr,0,size-1,key); } //”helper function” – “wrapper” functions
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Merge Sort Void mergeSort(const int arr[],const int start, const int end) { if(end>start) { int middle =(start+end)/2; mergeSort(arr,start,middle); mergeSort(arr,middle+1,end);
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: merge(arr,start,middle,end); } } void merge(int arr, int start, int middle, int end) { int *temp=new int[end-start+1]; //dynamic array int tempIndex=0; int s1=start; //start indeces for the two sorted subarrays int s2=middle+1; while(s1<=middle && s2<=end){ if(arr[s1]<arr[s2]) temp[tempIndex++]=arr[s1++]; else temp[tempIndex++]=arr[s2++]; } while(s1<=middle) temp[tempIndex++]=arr[s1++]; while(s2<=end) temp[tempIndex++]=arr[s2++]; for(int I=0;I<end-start+1;I++) arr[start+I]=temp[I]; delete temp; } void dpMergeSort(int arr, int size) { mergeSort(arr,0,size-1); }...
View Full Document

{[ snackBarMessage ]}

Page1 / 2

Recursive binary search MERGE - merge(arr,start,middle,end...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online