From 62c818cb75d7f227bf6a85392f63c2ea23478224 Mon Sep 17 00:00:00 2001 From: Rishav Ranjan Date: Thu, 5 Oct 2023 00:51:39 +0530 Subject: [PATCH 1/3] Adding swap function --- Sortings/bubblesort.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Sortings/bubblesort.c b/Sortings/bubblesort.c index 35e187f..1a619d2 100644 --- a/Sortings/bubblesort.c +++ b/Sortings/bubblesort.c @@ -1,4 +1,9 @@ #include +void swap(int *a,int *b){ + int t=*a; + *a=*b; + *b=t; +} int main() { int data[100],i,n,step,temp; @@ -9,17 +14,16 @@ int main() printf("%d. Enter element: ",i+1); scanf("%d",&data[i]); } - for(step=0;stepdata[i+1]) /* To sort in descending order, change > to < in this line. */ { - temp=data[i]; - data[i]=data[i+1]; - data[i+1]=temp; + swap(&data[i],&data[i+1]); } } + } printf("In ascending order: "); for(i=0;i Date: Thu, 5 Oct 2023 00:56:22 +0530 Subject: [PATCH 2/3] best case time O(n) --- Sortings/bubblesort.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sortings/bubblesort.c b/Sortings/bubblesort.c index 1a619d2..d02eb91 100644 --- a/Sortings/bubblesort.c +++ b/Sortings/bubblesort.c @@ -14,15 +14,21 @@ int main() printf("%d. Enter element: ",i+1); scanf("%d",&data[i]); } + int flag; for(step=0;stepdata[i+1]) /* To sort in descending order, change > to < in this line. */ { swap(&data[i],&data[i+1]); + flag=1; } } + if(flag==0){ + break; + } } printf("In ascending order: "); for(i=0;i Date: Thu, 5 Oct 2023 01:06:29 +0530 Subject: [PATCH 3/3] infix to postfix prefix --- InfixPrefixPostfix/infixtoboth.c | 208 ++++++++++++------------------- 1 file changed, 82 insertions(+), 126 deletions(-) diff --git a/InfixPrefixPostfix/infixtoboth.c b/InfixPrefixPostfix/infixtoboth.c index 187941f..dac9e95 100644 --- a/InfixPrefixPostfix/infixtoboth.c +++ b/InfixPrefixPostfix/infixtoboth.c @@ -1,130 +1,86 @@ #include #include +#include -int main() -{ - char stack[1000]; - char fin[10000]; - int final=0; - char A[1000]; - int head=-1; - int a,b,c,i,j,k,n,m,count=0; - while((c=getchar())!='\n') - { - A[count]=c; - count++; - } - //printf("%s", A); - for(i=0;i=0;i--) - { - if(A[i]==')') - { - head++; - stack[head]=')'; - } - else if(A[i]=='(') - { - while(head!=-1 && stack[head]!=')') - { - fin[final]=stack[head]; - final++; - head--; - } - if(head!=-1) - head--; - } - else if(A[i]=='*' || A[i]=='/') - { - while(head!=-1 && stack[head]!=')' && (stack[head]=='*' || stack[head]=='/')) - { - fin[final]=stack[head]; - final++; - head--; - } - head++; - stack[head]=A[i]; - } - else if(A[i]=='+' || A[i]=='-') - { - while(head!=-1 && stack[head]!=')') - { - fin[final]=stack[head]; - final++; - head--; - } - head++; - stack[head]=A[i]; - } - else - { - fin[final]=A[i]; - final++; - } - } - while(head!=-1) - { - fin[final]=stack[head]; - final++; - head--; - } - for(i=final-1;i>=0;i--) - printf("%c", fin[i]); - printf("\n"); - return 0; +#define MAX_SIZE 1000 + +char stack[MAX_SIZE]; +char fin[MAX_SIZE]; +int top = -1; // Stack's top pointer + +// Function declarations +void push(char ch); +char pop(); +bool isOperator(char ch); +int precedence(char ch); + +int main() { + char A[MAX_SIZE]; + int count = 0, final = 0, c; + + while ((c = getchar()) != '\n') { + A[count++] = c; + } + A[count] = '\0'; // Null terminate the string for good measure + + // Postfix conversion + for (int i = 0; i < count; i++) { + if (A[i] == '(') { + push(A[i]); + } else if (A[i] == ')') { + while (top != -1 && stack[top] != '(') { + fin[final++] = pop(); + } + pop(); // Remove '(' from stack + } else if (isOperator(A[i])) { + while (top != -1 && precedence(stack[top]) >= precedence(A[i])) { + fin[final++] = pop(); + } + push(A[i]); + } else { + fin[final++] = A[i]; + } + } + + while (top != -1) { + fin[final++] = pop(); + } + + fin[final] = '\0'; + printf("%s\n", fin); + + return 0; +} + +void push(char ch) { + if (top >= MAX_SIZE - 1) { + printf("Stack overflow!\n"); + exit(1); + } + stack[++top] = ch; +} + +char pop() { + if (top < 0) { + printf("Stack underflow!\n"); + exit(1); + } + return stack[top--]; +} + +bool isOperator(char ch) { + return ch == '+' || ch == '-' || ch == '*' || ch == '/'; +} + +int precedence(char ch) { + switch(ch) { + case '+': + case '-': + return 1; + case '*': + case '/': + return 2; + default: + return 0; + } }