Month: June 2016

c program for quick sorting

#include<stdio.h>
#include<conio.h>
void qsort(int arr[20], int fst, int last);
int main()
{
 int arr[30];
 int i,size;
 printf(“Enter total no. of the elements : “);
 scanf(“%d”,&size);
 printf(“Enter total %d elements : \n”,size);
 for(i=0; i<size; i++)
    scanf(“%d”,&arr[i]);
 qsort(arr,0,size-1);
 printf(“Quick sorted elements are as  : \n”);
 for(i=0; i<size; i++)
    printf(“%d\t”,arr[i]);
getch();
 return 0;
}
void qsort(int arr[20], int fst, int last)
{
 int i,j,pivot,tmp;
 if(fst<last)
 {
   pivot=fst;
   i=fst;
   j=last;
   while(i<j)
   {
     while(arr[i]<=arr[pivot] && i<last)
        i++;
     while(arr[j]>arr[pivot])
        j–;
     if(i<j)
     {
        tmp=arr[i];
        arr[i]=arr[j];
        arr[j]=tmp;
     }
   }
   tmp=arr[pivot];
   arr[pivot]=arr[j];
   arr[j]=tmp;
   qsort(arr,fst,j-1);
   qsort(arr,j+1,last);
 }
}

a program for stack sort (push and pop)

#include <stdio.h>
#define MAX 5
int top, status;
void push (int stack[], int item)
{ if (top == (MAX-1))
status = 0;
else
{ status = 1;
++top;
stack [top] = item;
}
}
int pop (int stack[])
{
int ret;
if (top == -1)
{
status = 0;
}
else
{
status = 1;
ret = stack [top];
–top;
}
return ret;
}
void display (int stack[])
{ int i;
printf (“\nThe Stack is: \n”);
if (top == -1)
printf (“empty”);
else
{
for (i=top; i>=0; –i)
printf (” \n%d “,stack[i]);
}
printf (“\n”);
}
int main()
{
int stack [MAX], item;
int ch;
top = -1;
do
{ do
{
printf (“\nNMAIN MENU:\n”);
printf (“\n 1.PUSH :”);
printf (“\n 2.POP :”);
printf (“\n 3.Exit :”);
printf (“\nEnter Your Choice:\n “);
scanf (“%d”, &ch);
if (ch<1 || ch>3)
printf (“\nInvalid Choice, Please try again:\n”);
} while (ch<1 || ch>3);
switch (ch)
{
case 1:
printf (“\nEnter the Element to be pushed : \n”);
scanf (“%d”, &item);
push (stack, item);
if (status)
{ printf (“\nAfter Pushing :\n”);
display (stack);
if (top == (MAX-1))
printf (“\nThe Stack is Full:\n”);
}
else
printf (“\nStack overflow on Push:\n”);
break;
case 2:
item = pop (stack);
if (status)
{ printf (“\nThe Popped item is %d \n After Popping: “,item);
display (stack);
}
else
printf (“\nStack underflow on Pop:\n”);
break;

}
}while (ch != 3);
}