Help with bubble sort & binary search program

I've looked up the algorithms and what I have should work if im reading it right but i am getting some weird numbers in my output and it is not always be sorted.

Example of error:

Enter the number of naturals: 4
Enter the natural numbers to sort: 45 2 43 7
Numbers sorted in ascending order:
32767 2 45 7 (error here, it doesn't sort and gives a weird number)

Enter the number to search: 2
Number 2 is found in position 1

Last edited on
closed account (D80DSL3A)
I see a clear error in bubblesort. Lines 45, 46
1
2
array[i] = array[size+1];
array[size+1] = temp;

You should be swapping elements i with i+1. Note size+1 is outside the array bounds. This would explain the odd very large number. Try this instead:
1
2
array[i] = array[i+1];
array[i+1] = temp;

That might solve the problem.
Wow i cant believe ive been trying that for like 3 hours. It works but it adds an extra 0 at the beggining which replaces the biggest number. Any idea how to solve that?
Topic archived. No new replies allowed.