1. Write a c program for bubble sort.
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
69 comments:
Thanks, its working! But while executing the program, the 'Run' window is disappearing before showing the result. Although, I have fixed it. If anyone is having the same problem then see below:
In the main function, put "getch();" before the "return 0" statement.
To run the program on a fresh screen:
include this header file at the top--> #include
and inside main function type "clrscr();" just after the variable declaration is done.
Thanks...
Sorry, I havn't completed the header file name above..
To run on a new window include this header file>>
#include
and inside main function type "clrscr();" just after the variable declaration is done.
Hope it works!!
Thanks...
conio.h
debaan every1 can understand that much if he is trying this program
can u please explain how it works?
working well but difficult to understand. Can anyone send discription at lovababu.golthi@gmail.com
Thanks in advance.
if you want to show the result without getch()
go to window--->output
Add prototype "int quicksort(int,int,int);" after the header files also add "#include" too to use gtech();
/*Add prototype "int quicksort(int,int,int);" after the header files also add "#include" too to use gtech();*/
Add prototype "int quicksort(int,int,int);" after the header files also add "conio.h" header file too to use getch();
//easy to understand
#include
#include
void quicksort(int [],int,int);
int main()
{
int x[20],size,i;
printf("Enter size of the array [<20]: ");
cin>>size;
printf("Enter %d elements: ",size);
for(i=0;i>x[i];
}
quicksort(x,0,size-1);
cout<<"Sorted elements: ";
for(i=0;i<size;i++)
cout<<x[i]<<" ";
getch();
return 0;
}
void quicksort(int x[],int first,int last)
{
int pivot,j,temp,i;
pivot=last;
for(i=first;i<pivot;i++)
{
if(x[pivot]<x[i])
{
temp=x[i];
x[i]=x[pivot-1];
x[pivot-1]=x[pivot];
x[pivot]=temp;
}
quicksort(x,first,pivot-1);
quicksort(x,pivot+1,last);
}
}
if any one is looking to execute quick_sort using linked list , here's the code:
#include
#include
#include
struct linked_list
{
int data;
struct linked_list *link;
struct linked_list *prev;
};
typedef struct linked_list node;
void create(node *first);
void print(node *first);
node *quick_sort(node *first);
int count(node *first);
main()
{
node *first;
int x,n,i,j=0,m;
clrscr();
first=(node *)malloc(sizeof(node));
first->prev=0;
create(first);
printf("the numbers you have entered are :\n");
print(first);
n=count(first);
printf("\n the number of elements you have entered are: %d\n",n);
for(i=0;idata);
printf( " do you want to add more?(y=1/n=2)\n");
scanf("%d",&x);
if(x==2)
{
first->link=0;
}
else
{
first->link= (node *)malloc(sizeof(node));
first->link->prev=first;
create(first->link);
}
return;
}
void print(node *list)
{
if(list->link!=0)
{
printf("%d-> ",list->data);
if(list->link->link==0)
printf("%d",list->link->data);
print(list->link);
}
}
node *quick_sort(node *first)
{
node *save,*temp,*pivot,*p;
pivot=first;
temp=pivot->link;
while(temp!=0)
{
if(pivot->data > temp->data)
{
if(temp->link==0)
{
temp->prev->link=0;
temp->link=first;
first->prev=temp;
temp->prev=0;
first=temp;
break;
}
else
{
temp=temp->link;
temp->prev->link=first;
temp->prev->prev->link=temp;
first->prev=temp->prev;
temp->prev=temp->prev->prev;
first->prev->prev=0;
first=first->prev;
}
}
else
temp=temp->link;
}
pivot=pivot->link;
while(pivot->link!=0)
{
temp=pivot->link;
while(temp!=0)
{
if(pivot->data > temp->data)
{
if(temp->link==0)
{
temp->prev->link=0;
temp->link=pivot;
pivot->prev->link=temp;
temp->prev=pivot->prev;
break;
}
else
{
p=pivot->prev;
temp=temp->link;
temp->prev->link=pivot;
temp->prev->prev->link=temp;
pivot->prev=temp->prev;
temp->prev=temp->prev->prev;
pivot->prev->prev=p;
pivot->prev->prev->link=pivot->prev;
}
}
else
temp=temp->link;
}
pivot=pivot->link;
}
printf("\n first=%d\n",first->data);
return(first);
}
int count(node *first)
{
node *save=first;
int n=0;
while(save!=0)
{
n++;
save=save->link;
}
return(n);
}
i want to write a program to sort weather condition for past 3 years for 5 cities using multi dimenstional arrays......
any one can know the program ...you please explain that....
can u explain this program
easy to understand
it cant work....
#include
#include
void quick_sort(int*,int,int);
int partision(int*,int,int);
void main()
{
int i,n,j,a[50],lower=0,upper;
clrscr();
printf("Enter the size of an array=");
scanf("%d",&n);
upper=n-1;
printf("Enter the array elements=");
for(i=0;ilower)
{
i= partision(a,lower,upper);
quick_sort(a,lower,i-1);
quick_sort(a,i+1,upper);
}
}
int partision(int a[],int lower,int upper)
{
int i,p,q,temp;
p=lower+1;
q=upper;
i=a[lower];
while(q>=p)
{
while(a[p]i)
{
q--;
}
if(q>p)
{
temp=a[p];
a[p]=a[q];
a[q]=temp;
}
}
temp=a[lower];
a[lower]=a[q];
a[q]=temp;
return q;
}
void main()
{
char c=125;
pritnf("%d",c);
}
What is the time Complexity of this program???
please tell me program for quick sort in descending order.
u can press ctrl f5 or alt f5 to see the output in turboc without need of getch
what's the ending condition763
unable to open include file 'stdio.h' and 'conio.h'. what to do if this error comes?
this is not working.... wtf...!!!!
can you plss help me?.our final project is to make a program that implementing bubble, sort,insertion,quick and selection in one program.
buddy..msg me at anand.sinha85@gmail.com for any help on sorting(The guy who posted it as final year project).Although the project is one of the easiest you can get stil if you need any help kindly let me know.
is it working???
hey u forgot to include the exit condition for recursion so its going in an infinite loop
what is the space complexity of the main code(quick sort)....anybody pls help....
Hi, thanks for the code.
Is there a way to find the position of the minimum from this?
If yes, please post that.
Thank you.
this will work if the numbers are identical...
after sorting, first element of the array is minimum
try this :
#include
#include
void display(int*,int);
void swap(int *a, int *b)
{
*a = *a - *b;
*b = *b + *a;
*a = *b - *a;
}
int partition(int *arr, int start, int end) //Partition the array
{
int pivot = start;
int left = start , right = end;
int i , pivotVal = arr[pivot];
while(left < right)
{
while(arr[left] <= pivotVal && left <=end )
left++;
while(arr[right] > pivotVal && right >= 0)
right--;
if(left < right)
swap(&arr[left], &arr[right]);
}
arr[start] = arr[right];
arr[right] = pivotVal;
return right;
}
void quick(int *arr, int start, int end)
{
int m;
if(start < end)
{
m = partition(arr,start,end); //Pivot
quick(arr,start,m-1);
quick(arr,m+1,end);
}
}
void display(int *arr,int nmem)
{
int i;
for(i = 0; i < nmem;i++)
printf("%d ",arr[i]);
printf("\n");
}
int main()
{
int arr[]={12,32,2,56,34,23,67,122};
int nmem = sizeof(arr)/sizeof(int);
quick(arr, 0, nmem - 1);
display(arr,nmem); //Sorted array
}
add these headers"stdio.h" and "stdlib.h"
o(n)
no it isnt going as the recursive calls are included in if condition, examine the codwe carefully
That's the spirit kid, Bharat Patidar
Your code is in a straight vertical line, like a arrow in an eye.
Above code is well formated.
Bharat Patidar
Hi there,
Trying to be smart ha,
I don't think anybody is going to read that code
But well done. Keep it
Bharat Patidar
while(x[i]<=x[pivot]&&i<last)
here if we replace i<last with i<=last...will it make much of a difference.?
time complexity is O(1)
FUnny its called a project,we call it a single Lab work !
ha..ha..funny!
buddy..msg me at anand.sinha85@gmail.com for any help on sorting(The guy who posted it as final year project).Although the project is one of the easiest you can get stil if you need any help kindly let me know.
can anyone explain the quick sort program which is given above plzz
An arrow in the eye
how to run bubble sort program in c++? i need the exact code of the program.
This program works in recursive manner. At first you need to know how recursive function works
How to make source code of simple quick sort implementation using array descending order in c programming language?
I need your Help someone.
Obtain the passenger details for a railway reservation system. Sort the passenger records based on train number and destination city using quick sort.
#include
#include
void quicksort(int [],int,int);
int main()
{
int x[20],size,i;
printf("Enter size of the array [<20]: ");
scanf("%d",&size);
printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
{
scanf("%d",&x[i]);
}
quicksort(x,0,size-1);
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf("%d",x[i]);
return 0;
}
void quicksort(int x[],int first,int last)
{
int pivot,j,temp,i;
pivot=last;
for(i=first;i<pivot;i++)
{
if(x[pivot]<x[i])
{
temp=x[i];
x[i]=x[pivot-1];
x[pivot-1]=x[pivot];
x[pivot]=temp;
}
quicksort(x,first,pivot-1);
quicksort(x,pivot+1,last-1);
}
}
Go to option then directories and remove include and lib from last that's it
not understand
what other modification in the code would I need to make if i initialise 'pivot=a[first]' (as the element itself)
rather tha 'pivot=first'(which sets the index value to pivot)
plz tell me abt these two lines in brief...
quicksort(x,first,j-1);
quicksort(x,j+1,last);
why to use c
Please write finished work qsort..
//plz check my mistake in this quick sort program. i am not getting output
#include
int partition(int [],int ,int );
void qs(int [],int ,int );
int partition(int a[],int p,int r)
{
int i,j, t,x,temp;
x=a[r-1];
i=p-1;
printf("sid");
for(j=p;j<r-1;j++)
{
if(a[j]<=x)
{
i=i+1;
t=a[j];
a[j]=a[i];
a[i]=t;
}
}
temp=a[i+1];
a[i+1]=a[r];
a[r]=temp;
return i+1;
}
void qs(int a[],int p,int r)
{
int q;
if(p<r)
q=partition(a,p,r);
printf("siddh");
qs(a,p,q-1);
qs(a,q+1,r);
}
int main()
{
int n,i,a[20];
printf("enter the size of array");
scanf("%d",&n);
printf("array element is");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("after sorting");
qs(a,i,n);
for(i=0;i<n;i++)
{
printf(" %d",a[i]);
}
}
If you want to sort each city individually thanthan apply a sorting algorithm on each individual array. If you want to sort d full array than your better off copying the whole in another array, sorting it n copying d contents back.
//i can not find my mistake...pls any one help me...according to me there is no logical problem...bt something may me wrong in my program...pls pls help me//
#include
void quick(int [15],int,int);
void main()
{
int array[15],i,n;
printf("Enter no of elements to sort:\n");
scanf("%d",&n);
printf("Enter %d elements 1 by 1:\n",n);
for(i=0;ipivot)
{
c=array[i];
i=i+1;
}
if(c<pivot)
{
temp=array[index];
array[index]=c;
c=temp;
index++;
c=array[i];
i=i+1;
}
}
temp=array[index];
array[index]=c;
c=temp;
quick(array,left,index-1);
quick(array,index+1,right);
}
//i can not find my mistake...pls any one help me...according to me there is no logical problem...bt something may me wrong in my program...pls pls help me//
#include
void quick(int [15],int,int);
void main()
{
int array[15],i,n;
printf("Enter no of elements to sort:\n");
scanf("%d",&n);
printf("Enter %d elements 1 by 1:\n",n);
for(i=0;ipivot)
{
c=array[i];
i=i+1;
}
if(c<pivot)
{
temp=array[index];
array[index]=c;
c=temp;
index++;
c=array[i];
i=i+1;
}
}
temp=array[index];
array[index]=c;
c=temp;
quick(array,left,index-1);
quick(array,index+1,right);
}
Incorrect code ! Used cin function on c language
thank you dude, JesusQuick
i want the program code for quick sort without usinf function or recursion.i want function code of quick sort only in main function plzzzz any one can help me.
I have viewed following page on Wed 31-Jan-2024 IST
https://www.cquestions.com/2008/01/c-program-for-quick-sort.html
We can replace temporary variable using XOR operator to swap values:
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
with:
if ( j != pivot )
{
arr[pivot] ^= arr[j];
arr[j] = arr[pivot] ^ arr[j];
arr[pivot] ^= arr[j];
}
...
arr[i] ^= arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] ^= arr[j];
...
Sample using double My Profile:
// /usr/bin/gcc.exe -g -Wall Quick_Sorting.c -o ./a.out
double murugesan = 900000000.2004;
double openssl = 900000000.2018;
printf( "murugesan %012.04f openssl %012.04f\n", murugesan, openssl);
*(int*)&murugesan ^= *(int*)&openssl;
*(int*)&openssl ^= *(int*)&murugesan;
*(int*)&murugesan ^= *(int*)&openssl;
printf( "murugesan %012.04f openssl %012.04f\n", murugesan, openssl);
/*
$ ./a.out
murugesan 900000000.2004 openssl 900000000.2018
murugesan 900000000.2018 openssl 900000000.2004
*/
Post a Comment