//Quick sort// #include <stdio.h> int main() { int n, i, j, pivot, temp; printf("Enter the number of elements: "); scanf("%d", &n); int arr[n]; printf("Enter %d integers: ", n); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } // Quicksort implementation (without functions) i = 0; // Lower index j = n - 1; // Upper index // Choose the last element as the pivot (can be modified) pivot = arr[j]; while (i <= j) { // Move elements less than the pivot to the left side while (arr[i] < pivot) { i++; } // Move elements greater than the pivot to the right side while (arr[j] > pivot) { j--; } // If elements on both sides are misplaced, swap them if (i <= j) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } printf("Sorted array: "); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf(" "); return 0; }
9. Write a program to convert decimal value into binary using stack. #include<stdio.h> #include<conio.h> struct Stack { int a[16]; int top; }S; void Push ( int d ) { S.top++; if (S.top > 15 ) { printf (" Stack is Full "); S.top = 15; } else { S.a[S.top] = d; } } int Pop () { int d ; if (S.top < 0 ) { printf (" Stack is Empty "); return -1; } else { d = S.a[S.top]; S.top--; return d; } } void main () { int n,r; clrscr(); S.top = -1; printf (" Enter Any Decimal Number "); scanf ("%d",&n); while ( n > 0 ) { r = n % 2; Push (r); n = n / 2; } while (S.top != -1 ) { printf ("%d",Pop()); } getch(); }
5. Write a program for binary search. #include<stdio.h> #include<conio.h> void main () { int a[5],i,l,h,n,m,flg; clrscr(); printf (" Enter Five Numbers "); for (i=0;i<5;i++) { scanf ("%d",&a[i]); } printf (" Enter Number to Search "); scanf ("%d",&n); flg=0; l=0; h=4; while (l<=h ) { m = (l+h) / 2; if (a[m] == n ) { flg = 1; break; } else if ( n > a[m] ) { l = m + 1; } else { h = m - 1; } } if ( flg == 0 ) { printf (" Number is Missing "); } else { printf (" Number is Present"); } getch(); }
4. Write a program for linear search. #include<stdio.h> #include<conio.h> void main () { int a[5],i,n,flg; clrscr(); printf (" Enter Five Numbers "); for (i=0;i<5;i++) { scanf ("%d",&a[i]); } printf (" Enter Number to Search "); scanf ("%d",&n); flg=0; for (i=0;i<5;i++) { if (a[i] == n ) { flg = 1; break; } } if ( flg == 0 ) { printf (" Number is Missing "); } else { printf (" Number is Present"); } getch(); }
3. Write a program for insertion sort. #include<stdio.h> #include<conio.h> void main () { int a[5]; int i,j,t; clrscr (); printf (" Enter Five Elements "); for (i=0;i<5;i++) { scanf ("%d",&a[i]); } printf (" Given Five Elements "); for (i=0;i<5;i++) { printf (" %d",a[i]); } for (i=1;i<5;i++) { t = a[i]; j=i-1; while ( j >= 0 && a[j] > t ) { a[j+1] = a[j]; j--; } a[j+1] = t; } printf (" After Sorting "); for (i=0;i<5;i++) { printf (" %d",a[i]); } getch(); }
2. Write a program for bubble sort #include<stdio.h> #include<conio.h> void main () { int a[5]; int i,j,t; clrscr (); printf (" Enter Five Elements "); for (i=0;i<5;i++) { scanf ("%d",&a[i]); } printf (" Given Five Elements "); for (i=0;i<5;i++) { printf (" %d",a[i]); } for (i=0;i<4;i++) { for (j=0;j<4-i;j++) { if (a[j] > a[j+1]) { t = a[j]; a[j] = a[j+1]; a[j+1] = t; } } } printf (" After Sorting "); for (i=0;i<5;i++) { printf (" %d",a[i]); } getch(); }
#include <stdio.h> int main() { int arr[100], n, i, x, pos; printf("Enter the number of elements in the array "); scanf("%d", &n); printf("Enter the elements "); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } printf("Input array elements are: "); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } printf(" Enter the new element to be inserted: "); scanf("%d", &x); printf("Enter the position where element is to be inserted: "); scanf("%d", &pos); // Check for valid position if (pos < 0 || pos > n) { printf("Invalid position "); return 1; } // Shift elements to make space for insertion for (i = n - 1; i >= pos - 1; i--) { arr[i + 1] = arr[i]; } // Insert the element at the specified position arr[pos - 1] = x; // Increase the size of the array (logical size, not actual size) n++; printf(" Array after insertion: "); for (i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Sir, just one question(maybe you’ve explain the reason in the video but I apologize that I know only english and chinese) why you lubricate on the outside of the chain instead of the inside where everyone on RU-vid or other website told us to lubricate the inside the chain? Just want to know the reason and learn something new, not trying to raise any argument😅