Posts

Stack operation

#include<stdio.h> #include<process.h> #include<stdlib.h> #define MAX 5    //Maximum number of elements that can be stored int top=-1,stack[MAX]; void push(); void pop(); void display(); void main() {     int ch;         while(1)    //infinite loop, will end when choice will be 4     {         printf("\n*** Stack Menu ***");         printf("\n\n1.Push\n2.Pop\n3.Display\n4.Exit");         printf("\n\nEnter your choice(1-4):");         scanf("%d",&ch);                 switch(ch)         {             case 1: push();        ...

Merge sort

#include<stdio.h> void mergesort(int a[],int i,int j); void merge(int a[],int i1,int j1,int i2,int j2); int main() {     int a[30],n,i;     printf("Enter no of elements:");     scanf("%d",&n);     printf("Enter array elements:");         for(i=0;i<n;i++)         scanf("%d",&a[i]);             mergesort(a,0,n-1);         printf("\nSorted array is :");     for(i=0;i<n;i++)         printf("%d ",a[i]);             return 0; } void mergesort(int a[],int i,int j) {     int mid;             if(i<j)     {         mid=(i+j)/2; ...

Program for Different operations on C

#include<stdio.h> #include<conio.h> #include<stdlib.h> #define MAX 10 struct stack {     int items[MAX];     int top; }; typedef struct stack st; void createEmptyStack(st *s) {     s->top=-1; } int isfull(st *s) {     if (s->top==MAX-1)         return 1;     else         return 0; } int isempty(st *s) {     if (s->top==-1)         return 1;     else         return 0; } void push(st *s) {     int newitem;     printf("Enter item to be inserted: ");     scanf("%d",&newitem);     if (isfull(s))     {         printf("STACK FULL");     }     else ...

Bubble sort using function

Image
#include <stdio.h> void bubble_sort(long [], long); int main() {   long array[100], n, c, d, swap;   printf("Enter number of elements\n");   scanf("%ld", &n);   printf("Enter %ld integers\n", n);   for (c = 0; c < n; c++)     scanf("%ld", &array[c]);   bubble_sort(array, n);   printf("Sorted list in ascending order:\n");   for ( c = 0 ; c < n ; c++ )      printf("%ld\n", array[c]);   return 0; } void bubble_sort(long list[], long n) {   long c, d, t;   for (c = 0 ; c < ( n - 1 ); c++)   {     for (d = 0 ; d < n - c - 1; d++)     {       if (list[d] > list[d+1])       {         /* Swapping */         t         = list[d];  ...

Bubble sort

Image
/* Bubble sort code */ #include <stdio.h> int main() {   int array[100], n, c, d, swap;   printf("Enter number of elements\n");   scanf("%d", &n);   printf("Enter %d integers\n", n);   for (c = 0; c < n; c++)     scanf("%d", &array[c]);   for (c = 0 ; c < ( n - 1 ); c++)   {     for (d = 0 ; d < n - c - 1; d++)     {       if (array[d] > array[d+1]) /* For decreasing order use < */       {         swap       = array[d];         array[d]   = array[d+1];         array[d+1] = swap;       }     }   }   printf("Sorted list in ascending order:\n");   for ( c = 0 ; c < n ; c++ )    ...

Selection Sort Program

Image
#include <stdio.h> int main() {    int array[100], n, c, d, position, swap;    printf("Enter number of elements\n");    scanf("%d", &n);    printf("Enter %d integers\n", n);    for ( c = 0 ; c < n ; c++ )       scanf("%d", &array[c]);    for ( c = 0 ; c < ( n - 1 ) ; c++ )    {       position = c;       for ( d = c + 1 ; d < n ; d++ )       {          if ( array[position] > array[d] )             position = d;       }       if ( position != c )       {          swap = array[c];          array[c] = array[position]; ...