### I'm having trouble with my Merge Sort - tried it a couple different ways and this small bug wont go away - WHY???

I have created a program that first sorts a series of numbers that are input dynamically then an option is given to either use a sequential search or a Binary search. my sequential search works fine but the merge sort coupled with the binary search has a small bug that I just can't seem to figure how to eliminate. I first used my own merge sort but it was really in efficient so a I took a more effient example and incorporated it in my program but I cant seem to get rid of this bug I'm dealing with. and it seems to be causing a futher problem with the Binary seach. I have entered the ful code (sorry about the length) and I'm hoping anybody can lead me in the right direction towards fixing this.

here's my code

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126`` ``````#include #include using namespace std; const int N = 10; // A simple print function void print(int *A) { for ( int i = 0; i < N; i++ ) cout << A[i] << " "; cout << endl; } void merge(int* A, int p, int r) { int mid = floor((p + r) / 2); int i1 = 0; int i2 = p; int i3 = mid + 1; // Temp array //////I tried this two different ways but nothing seems to work //int temp [r-p+1]; int temp = new int[r-p+1]; // Merge in sorted form the 2 arrays while ( i2 <= mid && i3 <= r ) if ( A[i2] < A[i3] ) temp[i1++] = A[i2++]; else temp[i1++] = A[i3++]; // Merge the remaining elements in left array while ( i2 <= mid ) temp[i1++] = A[i2++]; // Merge the remaining elements in right array while ( i3 <= r ) temp[i1++] = A[i3++]; // Move from temp array to master array for ( int i = p; i <= r; i++ ) A[i] = temp[i-p]; } void merge_sort(int* A, int p, int r) { if ( p < r ) { int mid = floor((p + r) / 2); merge_sort(A, p, mid); merge_sort(A, mid + 1, r); merge(A, p, r); } } int sSearch(int A[], int N, int V) { for(int i=1;i<=N; i=i+1) { if (V==A[i]){ cout<<"Data found at position: "<>N; int* A ; A= new int[N]; cout<<"Please enter each numerical value "<>A[i]; } merge_sort(A, 0, 9); cout<>option; cout<>V; cout<>V; cout<

Can anybody please tell me what I'm pulling out the little hair I got over
Thank You!!!
Last edited on
Topic archived. No new replies allowed.