For some reason bubble sort is not working. Can someone help.
binary search bool search (int target, int array[],int length)
{
int end = length-1; int start = 0;
while (array[start] < array[end])
{
// 7 0 14
// 11 8 14
// 13 12 + 14
// 14 14 14
//
// 9 10 + 8 =
// 8 + 8
int middle = (start + end)/2;
if (array[middle] == target)
{
return true;
} //13.5 7
// 13.5 11
//13.5 13
if ( target > array[middle] )
{ // 8 7+1
// 12 11+1
// 14 13+1
start = middle+1;
// 8 13.5
// 12 13.5
if (array[start] == target)
{
return true;
}
}
else if(target < array[middle])
{
end = middle - 1;
if (array[end] == target)
{
return true;
}
}
}
return false;
}
I realize bubble sort is really inefficient code. The code won't work . The parts I can identity is printf( "%d" , array[total]); and break; Will break work the way I set it up? Here is a few steps.
I just realized I don't even need this part. //reverses left and right back to correct order so right is not before left // 1 = 2 left = right; right = left + 1;
r
l
// ex array[total] = {2,5,3,1,4}
l
r
// ex array[total] = {2,1,3,5,4}
The next time I run the loop array[right] and array[left] will array[total] eventually change to {1 2 3 4 5}
//bubble sort
void sort (int array[], int total ) { // ex array[total] = {2,5,3,1,4} int end = total - 1; int left = 0; int min = 0; int right
= left + 1;
while ((left <= total) || (min <= total))
{
if( array[left] > array[right] )
{//switches array[left] and array[right]
//5 3
array[left] = array[right];
// 5 [2]-[1]
array[right] = array[left]-1;
printf( "%d %d" ,array[left] ,array[right]);
//reverses left and right back to correct order so right is not before left
// 1 = 2
left = right;
right = left + 1;
}
// maybe top block
if (right == end)
{
min++;
left = 0;
right = left +1;
}
if (min == end)// does total need to be end
{
printf( "%d" , array[total]);
break;
}
left++;
right = left + 1;
}
}
Did I make any other mistakes in bubble sort?
:) helpers.c exists.
:) helpers.c compiles.
:( sorts {5,4,3,2,1}
expected "2", not "\n"
:( sorts {5,3,1,2,4,6}
expected "3", not "\n4\n6\n"
:( finds 28 in {28,29,30}
expected exit code 0, not 1
:( finds 28 in {27,28,29}
expected exit code 0, not 1
:( finds 28 in {26,27,28}
expected exit code 0, not 1
:( finds 28 in {27,28,29,30}
expected exit code 0, not 1
:( finds 28 in {26,27,28,29}
expected exit code 0, not 1
:( finds 28 in {25,26,27,28}
expected exit code 0, not 1
:) doesn't find 28 in {25,26,27}
:) doesn't find 28 in {25,26,27,29}
:) doesn't find 28 in {29,30,31,32}
:) doesn't find 28 in {29, 30, 31}
:( finds 28 in {30,27,28,26}
expected exit code 0, not 1