5. Write a c program for heap sort.
7. Write a c program for shell sort.
C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arrays structures functions recursion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c++ tutorials and pdf
24 comments:
in your insertion sort swapping of value is not possible
because
you assign value of a[j] to a[j+1]
but not alloting 0 anywhere in array
#include
int main()
{
int a[3],i,j,temp;
printf("Enter 5 Array Element:\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<5;i++)
{
temp=a[i+1];
j=i;
while(temp=0)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
printf("Sorted Array id: ");
for(i=0;i<3;i++)
{
printf("%d",a[i]);
}
return 0;
}
tu muy bien
hey this is a great program for insertion sort !!!!!!
amazing program . GREAT WORK GUYS !!!!
program changla ahe . akdum mast
I think The for loop is being started with i=1 instead of i=0. .dats y the program is ok!
good work!!!!!
actually the program has to be started for i=1 ...because in insertion sort we always compare with the element before it.in case of the 0th element there is nothing to compare with as there is nothing before it. so thats why the loop is ok
u have taken an arry of 3 but u want to take 5 element. how is it possible????
Well, if you want to use for loop instead of while, it goes something like this-
for(i=1;i0;j--)
{ if(a[j]<a[j-1])
{ temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
}
The rest is the same :)
0 is allotting in j=i-1 which then proceed.....
ur array will take only 3 elements and the remaining 2 elements wwill get stored in successive locations in memory following ur array!
I thnk its bst way to impliment dir sorting.
#include
#include
void main()
{
int a[20],k,n,i=0,j=0,t=0;
clrscr();
printf("no of of elemts");
scanf("%d",&n);
printf("enter %d values",n);
for(i=0;i=1;j--)
{
printf("i=%d j=%d %d %d",i,j,a[j],a[j-1]);
if(a[j]>a[j-1])
break;
else
{
t=a[j];
a[j]=a[j-1];
a[j-1]=t;
}
} printf("\n");
} printf("\n");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
getch();
}
THE ABOVE CODE REPRESENTS WHICH SORTING TECHNIQUE????
DOES IT REPRESENT INSERTION SORT????
cool..! and correct
bubble sort i guess
#include
#include
int main()
{
int n, array[1000], i, j, temp;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0 ; i < n; i++) {
scanf("%d", &array[i]);
}
for (i = 0 ; i < n-1; i++) {
for( j = i+1 ; j > 0; j--){
if(array[j] < array[j-1]) /* if(array[j] > array[j-1])*/
{
temp = array[j];
array[j] = array[j-1];
array[j-1] = temp;
}
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i < n; i++) {
printf("%d\n", array[i]);
}
getch();
return 0;
}
bubble sort
in while loop there must be one more statement
after
a[j+1]=a[j];
a[j]=temp;
the great code of sorting !
Very informative article.Thank you author for posting this kind of article .
http://www.wikitechy.com/view-article/insertion-sorting-
program-in-cpp-with-example-and-explanation
Both are really good,
Cheers,
Venkat
what kind of techinque was used?
Post a Comment