C Program to implement bucket sort

Write C program to implement bucket sort.
The idea of Bucket Sort is to divide the interval [0, 1] into n equal-sized sub intervals, or buckets, and then distribute the n input numbers into the buckets. To produce the output, we simply sort the numbers in each bucket and then go through the buckets in order, listing elements in each.
Bucket sort runs in linear time when the input is drawn from a uniform distribution. Read more about C Programming Language .
/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
void Bucket_Sort(int array[], int n)
{   
 int i, j;   
 int count[n];  
 for(i=0; i < n; i++)
 {   
  count[i] = 0;   
 }     
 for(i=0; i < n; i++)
 {    
  (count[array[i]])++; 
 }     
 for(i=0,j=0; i < n; i++)
 {   
  for(; count[i]>0;(count[i])--) 
  {       
   array[j++] = i; 
  }  
 }   
}    
int main() 
{ 
 int array[100];   
 int num;   
 int i;  
 printf("Enter How many Numbers : ");    
 scanf("%d",&num);    
 printf("Enter the %d elements to be sorted:n",num);  
 for(i = 0; i < num; i++ )
 {   
  scanf("%d",&array[i]);  
 }   
 printf("nThe array of elements before sorting : n"); 
 for (i = 0;i < num;i++) 
 {    
  printf("%d ", array[i]);   
 }    
 printf("nThe array of elements after sorting : n");  
 Bucket_Sort(array, num);  
 for (i = 0;i < n;i++) 
 {     
  printf("%d ", array[i]);  
 }   
 printf("n");      
 return 0; 
} 

Read more Similar C Programs

Data Structures


C Sorting

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!
To browse more C Programs visit this link
Or if you can’t find the program you are looking for, you can contact [email protected]. Thank you for visiting.

(c) www.c-program-example.com

C program to print given numbers as a pyramid

Write a C program to print given numbers as a pyramid.
In this program we use the simple for statements to produce the patterns.
This program prints the following output,
        1
      232
    34543
  4567654
567898765
 Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

–>

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>


void display(int start, char *str){

int i,j;


for(i=start; i<=(start+start); i++)

printf("%c",str[i]);

for(j=i-2;j>=start;j--)

printf("%c",str[j]);

}


main(){

char str[]="123456789";

int i, j;

for(i=0;i<5;i++){

display(i, str);

printf("n");

}

}
Read more Similar C Programs
 
Graphics in C

C Strings 
 
C Aptitude 
 

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

–> To browse more C Programs visit this link
(c) www.c-program-example.com

C program to print all the possible permutations of given digits

Write a C program to print all the possible permutations of given digits.
Permutations means possible way of rearranging in the group or set in the particular order.
Example:
Input:1, 2, 3
Output:1 2 3, 1 3 2, 2 1 3, 3 1 2, 2 3 1, 3 2 1
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int lev=-1,n,val[50],a[50];
void main()
{
int i,j;
clrscr();
printf("Enter how many numbers?n");
scanf("%d",&n);
printf("nEnter %d numbers:nn",n);
for(i=0;i<n;i++)
{
val[i]=0;
j=i+1;
scanf("%dnn",&a[j]);
}
visit(0);
getch();
}
visit(int k)
{
int i;
val[k]=++lev;
if(lev==n)
{
for(i=0;i<n;i++)
printf("%2d",a[val[i]]);
printf(" ");
}
for(i=0;i<n;i++)
if(val[i]==0)
visit(i);
lev--;
val[k]=0;

}
Read more Similar C Programs
Learn C Programming
Recursion
Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C program to implement SJF algorithm.

Write a C program to implement SJF algorithm.
Shortest Job First(SJF) is the CPU scheduling algorithm. In SJF, if the CPU is available it allocates the process which has smallest burst time, if the two process are same burst time it uses FCFS algorithm.Read more about C Programming Language . and read the C Programming Language (2nd Edition) by K and R.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>

void main()
{
int i,j,n,brust_time[10],start_time[10],end_time[10],wait_time[10],temp,tot;
float avg;
clrscr();
printf("Enter the No. of jobs:nn");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("n n Enter %d process burst time:n",i);
scanf("%d",&brust_time[i]);
}

for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(brust_time[i]>brust_time[j])
{
temp=brust_time[i];
brust_time[i]=brust_time[j];
brust_time[j]=temp;
}
}

if(i==1)
{
start_time[1]=0;
end_time[1]=brust_time[1];
wait_time[1]=0;
}

else
{
start_time[i]=end_time[i-1];
end_time[i]=start_time[i]+brust_time[i];
wait_time[i]=start_time[i];
}
}
printf("nn BURST TIME t STARTING TIME t END TIME t WAIT TIMEn");
printf("n ********************************************************n");
for(i=1;i<=n;i++)
{
printf("n %5d %15d %15d %15d",brust_time[i],start_time[i],end_time[i],wait_time[i]);
}
printf("n ********************************************************n");
for(i=1,tot=0;i<=n;i++)
tot+=wait_time[i];
avg=(float)tot/n;
printf("nnn AVERAGE WAITING TIME=%f",avg);
for(i=1,tot=0;i<=n;i++)
tot+=end_time[i];
avg=(float)tot/n;
printf("nn AVERAGE TURNAROUND TIME=%f",avg);
for(i=1,tot=0;i<=n;i++)
tot+=start_time[i];
avg=(float)tot/n;
printf("nn AVERAGE RESPONSE TIME=%fnn",avg);
getch();
}
Read more Similar C Programs
C Basic

Search Algorithms.

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C program to Encrypt and Decrypt a password

C Strings:
Write a C program to Encryption and Decryption of password.
In this program we encrypt the given string by subtracting the hex value from it. Password encryption is required for the security reason, You can use so many functions like hash or other keys to encrypt. Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>
#include <string.h>

void encrypt(char password[],int key)
{
unsigned int i;
for(i=0;i<strlen(password);++i)
{
password[i] = password[i] - key;
}
}

void decrypt(char password[],int key)
{
unsigned int i;
for(i=0;i<strlen(password);++i)
{
password[i] = password[i] + key;
}
}
int main()
{
char password[20] ;
printf("Enter the password: n ");
scanf("%s",password);
printf("Passwrod = %sn",password);
encrypt(password,0xFACA);
printf("Encrypted value = %sn",password);
decrypt(password,0xFACA);
printf("Decrypted value = %sn",password);
return 0;
}
Read more Similar C Programs
Learn C Programming
Recursion
Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C program to implement Round Robin CPU scheduling algorithm.

Write a C program to implement Round Robin CPU scheduling algorithm.
In Round Robin scheduling algorithm, a small time slice or quantum is defined, all the tasks are kept in queue. The CPU scheduler picks the first task from the queue ,sets a timer to interrupt after one quantum, and dispatches the process. If the process is still running at the end of the quantum, the CPU is preempted and the process is added to the tail of the queue. If the process finishes before the end of the quantum, the process itself releases the CPU voluntarily. In either case, the CPU scheduler assigns the CPU to the next process in the ready queue. Every time a process is granted the CPU, a context switch occurs, which adds overhead to the process execution time. Round Robin scheduler is mainly used for the time sharing systems. Read more about C Programming Language . and read the C Programming Language (2nd Edition) by K and R.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include<stdio.h>
#include<conio.h>
struct process
{
char na[20];
int at,bt,ft,tat,rem;
float ntat;
}Q[5],temp;
int rr[20],q,x,k;
main()
{
int f,r,n,i,j,tt=0,qt,t,flag,wt=0;
float awt=0,antat=0,atat=0;
clrscr();
printf("Enter the no. of jobs:");
scanf("%d",&n);
for(r=0;r<n;r++)
{
printf("Enter process name,arrival time and burst time:n");
scanf("%s%d%d",Q[r].na,&Q[r].at,&Q[r].bt);
}
printf("Enter quantum:n");
scanf("%d",&qt);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(Q[i].at>Q[j].at)
{
temp=Q[i];
Q[i]=Q[j];
Q[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
Q[i].rem=Q[i].bt;
Q[i].ft=0;
}
tt=0;
q=0;
rr[q]=0;
do
{
for(j=0;j<n;j++)
if(tt>=Q[j].at)
{
x=0;
for(k=0;k<=q;k++)
if(rr[k]==j)
x++;
if(x==0)
{
q++;
rr[q]=j;
}
}
if(q==0)
i=0;
if(Q[i].rem==0)
i++;
if(i>q)
i=(i-1)%q;
if(i<=q)
{
if(Q[i].rem>0)
{
if(Q[i].rem<qt)
{
tt+=Q[i].rem;
Q[i].rem=0;
}
else
{
tt+=qt;
Q[i].rem-=qt;
}
Q[i].ft=tt;
}
i++;
}
flag=0;
for(j=0;j<n;j++)
if(Q[j].rem>0)
flag++;
}while(flag!=0);
clrscr();
printf("nnttROUND ROBIN ALGORITHM");
printf("n***************************");
printf("nprocesses Arrival time burst time finish time tat wt ntat");
for(f=0;f<n;f++)
{
wt=Q[f].ft-Q[f].bt-Q[f].at;
Q[f].tat=Q[f].ft-Q[f].at;
Q[f].ntat=(float)Q[f].tat/Q[f].bt;
antat+=Q[f].ntat;
atat+=Q[f].tat;
awt+=wt;
printf("nt%st%dtt%dt%dt%dt%d %f",
Q[f].na,Q[f].at,Q[f].bt,Q[f].ft,Q[f].tat,wt,Q[f].ntat);
}
antat/=n;
atat/=n;
awt/=n;
printf("nAverage tat is %f",atat);
printf("nAverage normalised tat is %f",antat);
printf("n average waiting time is %f",awt);
getch(); }


Read more Similar C Programs
C Basic

Search Algorithms.

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to find all the permutations of a given string.

Write a c program to find all the permutations of a given string.
Example: If the given string is sam, output is,
sam
sma
msa
mas
asm
ams.
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

# include<stdio.h>
# include<conio.h>
#include<string.h>
void swap (char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}

void permute(char *a, int i, int n)
{
int j;
if (i == n)
printf("%sn", a);
else
{
for (j = i; j <= n; j++)
{
swap((a+i), (a+j));
permute(a, i+1, n);
swap((a+i), (a+j));
}
}
}


int main()
{
char str[80] ;
int len=0,i;
puts("Enter a string:nn");
gets(str);
for (i=0; str[i] != ''; i++)
{
len++;
}
permute(str, 0, len);
getchar();
return 0;
}
Read more Similar C Programs
Searching in C

C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to solve Dining philosophers problem.

Write a C Program to solve Dining philosophers problem.
Dining philosophers problem is a classic synchronization problem.A problem introduced by Dijkstra concerning resource allocation between processes. Five silent philosophers sit  around table with a bowl of spaghetti. A fork is placed between each pair of adjacent philosophers.

Each philosopher must alternately think and eat.
Eating is not limited by the amount of spaghetti left: assume an infinite supply.
However, a philosopher can only eat while holding both the fork to the left and the fork to the right
(an alternative problem formulation uses rice and chopsticks instead of spaghetti and forks).

Each philosopher can pick up an adjacent fork, when available, and put it down, when holding it.
These are separate actions: forks must be picked up and put down one by one.
The problem is how to design a discipline of behavior (a concurrent algorithm) such that each philosopher won’t starve, i.e. can forever continue to alternate between eating and thinking.
Read more about C Programming Language.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<semaphore.h>
#include<pthread.h>

#define N 5
#define THINKING 0
#define HUNGRY 1
#define EATING 2
#define LEFT (ph_num+4)%N
#define RIGHT (ph_num+1)%N

sem_t mutex;
sem_t S[N];

void * philospher(void *num);
void take_fork(int);
void put_fork(int);
void test(int);

int state[N];
int phil_num[N]={0,1,2,3,4};

int main()
{
int i;
pthread_t thread_id[N];
sem_init(&mutex,0,1);
for(i=0;i<N;i++)
sem_init(&S[i],0,0);
for(i=0;i<N;i++)
{
pthread_create(&thread_id[i],NULL,philospher,&phil_num[i]);
printf("Philosopher %d is thinkingn",i+1);
}
for(i=0;i<N;i++)
pthread_join(thread_id[i],NULL);
}

void *philospher(void *num)
{
while(1)
{
int *i = num;
sleep(1);
take_fork(*i);
sleep(0);
put_fork(*i);
}
}

void take_fork(int ph_num)
{
sem_wait(&mutex);
state[ph_num] = HUNGRY;
printf("Philosopher %d is Hungryn",ph_num+1);
test(ph_num);
sem_post(&mutex);
sem_wait(&S[ph_num]);
sleep(1);
}

void test(int ph_num)
{
if (state[ph_num] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING)
{
state[ph_num] = EATING;
sleep(2);
printf("Philosopher %d takes fork %d and %dn",ph_num+1,LEFT+1,ph_num+1);
printf("Philosopher %d is Eatingn",ph_num+1);
sem_post(&S[ph_num]);
}
}

void put_fork(int ph_num)
{
sem_wait(&mutex);
state[ph_num] = THINKING;
printf("Philosopher %d putting fork %d and %d downn",ph_num+1,LEFT+1,ph_num+1);
printf("Philosopher %d is thinkingn",ph_num+1);
test(LEFT);
test(RIGHT);
sem_post(&mutex);
}
Read more Similar C Programs
C Basic

Search Algorithms.

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to convert from infix expression into prefix expression

C Program to convert from infix expression into prefix expression.
We use the infix expressions for the mathematical expressions in a program, these expressions will converted into equivalent machine instructions by the compiler using stacks.
Using stacks we can efficiently convert the expressions from infix to postfix, infix to prefix, postfix to infix, and postfix to prefix.
Example: infix to prefix:
infix: x^y^z-m+n+p/q,
postfix: ++-^x^yzmn/pq. Read more about C Programming Language.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include<stdio.h>

#include<conio.h>

//Stack precedence function

int F(char symbol)

{

switch(symbol)

{

case ‘+’ :

case ‘-‘ :

return 1;

case ‘*’:

case ‘^’:

return 6:

case ‘)’:

return 0:;

case ‘#’:

return -1;

default:

return 8;

}

}

//Input precedence function

int G(char symbol)

{

switch(symbol)

{

case ‘+’ :

case ‘-‘ :

return 2;

case ‘*’:

return 4;

case ‘^’:

return 5:

case ‘(‘:

return 0;

case ‘)’:

return 9:;

case ‘#’:

return -1;

default:

return 7;

}

}

Void infix_prefix(char infix[], char prefix[])

{

int top, j, i;

char symbol, s[40];

top = -1;

s[++top] = ‘#’;

J = 0;

strrev(infix);

for(i = 0;i < strlen(infix); i++)

{

symbol= infix[i];

while(F(s[top]) > G(symbol))

{

prefix[j] = s[top--];

j++;

}

if(F(s[top]) != G(symbol))

s[++top] = symbol;

else

top--;

}

while(s[top != ‘#’)

{

prefix[j++] = s[top--];

}

prefix[j] = ‘’;

strrev(prefix);

}

void main()

{

char infix[20];

char prefix[20];

printf(“/nEnter a valid infix expressionn”);

scanf(“%s”,infix);

infix_prefix(infix, prefix);

printf(“nnThe prefix expression isn”);

printf(“%sn”,prefix);

}


Read more Similar C Programs
C Basic
C Data Structure
Search Algorithms.

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to demonstrate isalpha, isdigit, is space.

C Program to demonstrate the following functions:isalpha, isdigit, isspace.The same principles apply to isalnum, iscntrl, isgraph,islower, isprint, ispunct, isupper, isxdigit.
In the standard library ctype.h all the above functions are declared.
All the subroutines mentioned here, returns non zero(true), If the checked argument is true for the respective subroutines. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h> /* printf */
#include <ctype.h> /* isalpha isdigit isspace etc */

#define FALSE 0
#define TRUE 1

/* function declarations */
int char_type(char);

main()
{
char ch;
/* get a character from the keyboard */
printf(" Please enter a charcater => ");
ch = getc(stdin);

char_type(ch); /* Figure out the character type */


}

//char_type:decides the character type
int char_type(char ch)
{
/* returns non zero if A-Z or a-z */
if ( isalpha(ch) != FALSE)
printf("%c is an Alpha character.n",ch);

/* returns non zero if 0-9 */
if ( isdigit(ch) != FALSE)
printf("%c is a numeric character.n",ch);

/* returns non zero if a space, CR, Tab, NL FF */
if ( isspace(ch) != FALSE)
printf("%c is white spacen", ch);

}

Read more c programs 
C Basic
C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com