Binary searching and Bubble Sorting

Keane 16

Skilled
This one I get -
Linear Searching :

float data;
float a[10];
int flag=0;
for(i=0; i<10; i++)
{
cin>>a;
}
cin>>data;

for(i=0; i<10; i++)
{
if(a=data)
flag=1;
}
if(flag==1)
cout<<"found";
else
cout<<"not found";


I just don't get these 2 programs - Can anyone explain em to me -

binary searching :
int n, a[10], data, m, l=0, u=m-1, flag=0;
while(flag==0!!u>l)
{
n=(l+u)/2;
if(a[n]==data)
flag=1;
else if(a[m]<data)
l=n+1;
else
u=n-1;
}

if(flag==1)
cout<<"found";
else
cout<<"not found";
bubble sorting :

int a[10];
cout<<"enter 10 nos - ";
for(int p=0; p<10, p++)
cin>>a[p];
for(int i=0; i<10; i++)
{
for(int j=o; j<10-1; j++)
{
if(a[j]>a[j+1])
{
int temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(int a=0, a<10, a++)
cout<<a[a];
 
Thanks for that greenhorn.

One question about bubble sorting :

function bubble_sort(list L, number listsize)

loop

has_swapped := 0 //reset flag

for number i from 1 to (listsize - 1)

if L > L[i + 1] //if they are in the wrong order

swap(L, L[i + 1]) //exchange them

has_swapped := 1 //we have swapped at least once, list may not be sorted yet

endif

endfor

//if no swaps were made during this pass, the list has been sorted

if has_swapped = 0

exit

endif

endloop

endfunction



the line in bold ... suppose i get use an array of 10 numbers as input ... i understand teh use of the line in italics ... but what does swap(l, l+1) do ? Will this work in TC ?
 
not sure :( even i havent seen that function :ashamed:
if you wanna make sure, swap the old fashioned way :p

btw, am also learning C right now:hap2: ( job training :mad:)

EDIT: hey thats psuedocode. it wont compile :p
cook up your own code !, or in this case, you already have it !
 
greenhorn said:
check the last two lines of the program you posted yourself
Code:
for(int a=0, a<10, a++)
cout<<a[a];
Temme if i got this right -

What the cout<<a[a]; thing does is it displayes the list of numbers after running all the loops ? and hence the right output ?
 
notice that there is no ";" after the for loop . in that case the for loop makes the next statement repeat.its just like saying
if (condition) cout <<a;

OR

if (condition){
cout<<a;
}

the only reason why the {} is avoided is because there is only one statement in the loop

also be aware that if you put a ; after the for statement, then only the loop finishes running , and ends at 10, and it will try to print a[10] (which doesnt exist :p)
 
URGENT

//Bubble sorting

#include<iostream.h>

#include<conio.h>

void main()

{

int a[10];

cout<<"enter 10 nos - ";

for(int p=0; p<10; p++)

cin>>a[p];

for(int i=0; i<10; i++)

{

for(int j=0; j<9; j++)

{

if(a[j]>a[j+1])

{

int temp;

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

}

}

cout<<"The numbers in ascending order are - "<<endl;

for(int k=0; k<10; k++)

cout<<a[k]<<endl;

getch();

}

I understand the whole program now except for the use of the line in bold ... and the damn prog doesnt work w/o it ..
 
^ So that we could run the second loop, pertaining to 'j' variable, 10 times again.

See, we should have 2 loops right!? One for the sorting through the array and the other for how many times the sorting is to be repeated.

Thus, the 'i'th variable gives the value of number of iterations.
 
:O K. Thanks. Never thought of/knew that.

Cheers

Are you sure ??? :O :S:S:S:S:S

Cause then it should work 10x9 = 90 times ? :S Comparing each value with the remaining 9 values .. 9x10 ..
 
Well, tis been eras since i dumped that yashwant kanitkar, his book that is, so havent galvanized my C programs since then. But i guess that the array should be of same length as the no. of iterations.(or vice versa!)

so if: i=0; i<9 .. then j=0;j<9

analogously: i=1;i<10 ..then.. oh well.. u got it! ;)
 
Back
Top