├── ATM.cpp ├── Ajmaincse111_disply.cpp ├── Ajmaincse12_area.cpp ├── Ajmaincse12_large number.cpp ├── Ajmaincse12_realnumber.cpp ├── Algo.cpp ├── Array.cpp ├── Array00.cpp ├── Basic C Program to Concatenate Strings using Pointer_arman105735.c ├── Binaryserch.cpp ├── C Program to Add two user input numbers using Pointers_arman105735.c ├── C Program to Find Roots of Quadratic Equation_arman105735.c ├── C Program to Simulate PRIORITY CPU Scheduling Algorithm.c ├── C Program to create a Menu Driven software using Switch Case_arman105735.c ├── C Program to find the largest Element in an Array using Recursion_arman105735.c ├── C program on the priority scheduling algorithm (Non preemptive) .c ├── CONTRIBUTING.md ├── Cycle_sort.cpp ├── Hipsort.cpp ├── Loop.cpp ├── MahfuzBankerAlgorithm.c ├── MahfuzDisk.c ├── MahfuzFCFS.cpp ├── MahfuzRoundRobin.c ├── Mahfuzpriority.c ├── Multi – Dimensional Array.cpp ├── MultiLevelQueueScheduling.cpp ├── Non-manipulating-algo.cpp ├── Priority Scheduling (Non_Preemptive).cpp ├── Priority Scheduling (Preemptive).cpp ├── Priority-scheduling.cpp ├── Program for FCFS CPU Scheduling.c ├── Program to check if input Number is int or float_arman105735.c ├── Program to check if input character is a vowel using Switch Case_arman105735.c ├── README.md ├── Rafi_Round_Robin_Preemptive.c ├── Rafi_SRTF(Preemptive).cpp ├── Round Robin Process Scheduling Algorithm Program in.cpp ├── Round-robin.cpp ├── Shortest remaining job first.cpp ├── String.cpp ├── String1.cpp ├── Sum of array elements.cpp ├── activityselection.cpp ├── almahmudjoy_Implementation of the above C++ algorithm.cpp ├── almahmudjoy_receives a number as input from user.cpp ├── almahmudjoy_robot or not.cpp ├── almahmudjoy_wrong password.cpp ├── bfs_rotten_oranges.cpp ├── bubblesort.cpp ├── bubt_university_contest_E_string.cpp ├── calc.cpp ├── calculation.c ├── catalan_number.cpp ├── fcfs.cpp ├── fcfs1.cpp ├── fcfs874386.c ├── fcfs911129.c ├── fcfs9121999.c ├── fcfs98872176.c ├── fcfs991.cpp ├── fcfs992.cpp ├── fcfs99541.cpp ├── fibonacci.cpp ├── file.c ├── index.c ├── insertionsort.cpp ├── interest.c ├── knapsack.cpp ├── kruskal_algo.cpp ├── lifo.c ├── linearSearch.cpp ├── mehedi0011.c++ ├── mehedihasa740_ArmstrongNumber.java ├── mehedihasan740_Binary_Search.java ├── mehedihasan740_Heap_Sort ├── mehedihasan740_Linear_search.java ├── mehedihasan740_Linked_List.java ├── mehedihasan740_Routine_Data_Manipulation.java ├── merge.cpp ├── mergesort.cpp ├── msakib1.cpp ├── msakib2.cpp ├── msakib_01_matrix.cpp ├── nasim1717_Program for Priority Scheduling Algorithm ├── palindrome_linked_list.cpp ├── pointer.cpp ├── primeNumber.c ├── program of SJF(Shortest job first ) Scheduling algorithm (Non -preemptive).c ├── quicksort.cpp ├── rakibutsho@Tokenization_using_file.cpp ├── rakibutsho@comment_detection_and_count.cpp ├── rakibutsho@fcfs.cpp ├── rakibutsho@rebon.cpp ├── random_ROUND ROBIN ├── reverse_list.cpp ├── sieve_of_eratosthenes.cpp ├── simplecalculator.cpp ├── sliding_window_max_of_all_subarrays.cpp ├── sum_of_odd_and_even.cpp ├── total_salary.cpp ├── transpose_of_a_matrix.cpp ├── value.c └── vertical-order-traversal.cpp /ATM.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | class Bank { 6 | 7 | // Private variables used inside class 8 | private: 9 | string name; 10 | long long accnumber; 11 | char type[10]; 12 | long long amount = 0; 13 | long long tot = 0; 14 | 15 | // Public variables 16 | public: 17 | // Function to set the person's data 18 | void setvalue() 19 | { 20 | cout << "Enter name\n"; 21 | cin.ignore(); 22 | 23 | // To use space in string 24 | getline(cin, name); 25 | 26 | cout << "Enter Account number\n"; 27 | cin >> accnumber; 28 | cout << "Enter Account type\n"; 29 | cin >> type; 30 | cout << "Enter Balance\n"; 31 | cin >> tot; 32 | } 33 | 34 | // Function to display the required data 35 | void showdata() 36 | { 37 | cout << "Name:" << name << endl; 38 | cout << "Account No:" << accnumber << endl; 39 | cout << "Account type:" << type << endl; 40 | cout << "Balance:" << tot << endl; 41 | } 42 | 43 | // Function to deposit the amount in ATM 44 | void deposit() 45 | { 46 | cout << "\nEnter amount to be Deposited\n"; 47 | cin >> amount; 48 | } 49 | 50 | // Function to show the balance amount 51 | void showbal() 52 | { 53 | tot = tot + amount; 54 | cout << "\nTotal balance is: " << tot; 55 | } 56 | 57 | // Function to withdraw the amount in ATM 58 | void withdrawl() 59 | { 60 | int a, avai_balance; 61 | cout << "Enter amount to withdraw\n"; 62 | cin >> a; 63 | avai_balance = tot - a; 64 | cout << "Available Balance is" << avai_balance; 65 | } 66 | }; 67 | 68 | // Driver Code 69 | int main() 70 | { 71 | // Object of class 72 | Bank b; 73 | 74 | int choice; 75 | 76 | // Infinite while loop to choose 77 | // options everytime 78 | while (1) { 79 | cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~" 80 | << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~" 81 | << "~~~WELCOME~~~~~~~~~~~~~~~~~~" 82 | << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~" 83 | << "~~~~~~~~~\n\n"; 84 | cout << "Enter Your Choice\n"; 85 | cout << "\t1. Enter name, Account " 86 | << "number, Account type\n"; 87 | cout << "\t2. Balance Enquiry\n"; 88 | cout << "\t3. Deposit Money\n"; 89 | cout << "\t4. Show Total balance\n"; 90 | cout << "\t5. Withdraw Money\n"; 91 | cout << "\t6. Cancel\n"; 92 | cin >> choice; 93 | 94 | // Choices to select from 95 | switch (choice) { 96 | case 1: 97 | b.setvalue(); 98 | break; 99 | case 2: 100 | b.showdata(); 101 | break; 102 | case 3: 103 | b.deposit(); 104 | break; 105 | case 4: 106 | b.showbal(); 107 | break; 108 | case 5: 109 | b.withdrawl(); 110 | break; 111 | case 6: 112 | exit(1); 113 | break; 114 | default: 115 | cout << "\nInvalid choice\n"; 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /Ajmaincse111_disply.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Bank 5 | { 6 | int i; 7 | public: 8 | Bank(): i(0) { } 9 | void operator ++() 10 | { ++i; } 11 | void operator --() 12 | { --i; } 13 | void Display() 14 | { cout << "Number of person inside of bank: " << i << endl; } 15 | }; 16 | 17 | int main() 18 | { 19 | Bank obj; 20 | 21 | obj.Display(); 22 | 23 | ++obj; 24 | ++obj; 25 | obj.Display(); 26 | j; 27 | obj.Display(); 28 | j; 29 | obj.Display(); 30 | j; 31 | obj.Display(); 32 | j; 33 | obj.Display(); 34 | j; 35 | obj.Display(); 36 | j; 37 | obj.Display(); 38 | j; 39 | obj.Display(); 40 | j; 41 | obj.Display(); 42 | j; 43 | obj.Display(); 44 | 45 | --obj; 46 | obj.Display(); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Ajmaincse12_area.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Area{ 5 | public: 6 | double heigth; 7 | double width; 8 | }; 9 | class Rectangle:public Area{ 10 | public: 11 | void setheigth(double l){ 12 | heigth=l; 13 | } 14 | void setwidth(double b){ 15 | width=b; 16 | } 17 | double area(){ 18 | return heigth*width; 19 | } 20 | }; 21 | 22 | int main() { 23 | Rectangle rec; 24 | rec.setheigth(30); 25 | rec.setwidth(15); 26 | cout<<"the area of Rectangle is : "< 2 | using namespace std; 3 | 4 | int main() { 5 | float n1, n2, n3; 6 | 7 | cout << "Enter three numbers: "; 8 | cin >> n1 >> n2 >> n3; 9 | 10 | if (n1 >= n2) { 11 | if (n1 >= n3) 12 | cout << "Largest number: " << n1; 13 | else 14 | cout << "Largest number: " << n3; 15 | } 16 | else { 17 | if (n2 >= n3) 18 | cout << "Largest number: " << n2; 19 | else 20 | cout << "Largest number: " << n3; 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Ajmaincse12_realnumber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class complex 4 | { 5 | int real,imag; 6 | public: 7 | void set() 8 | { 9 | cout<<"Enter real and imag part are :"; 10 | cin>>real>>imag; 11 | } 12 | friend complex sum(complex,complex); 13 | void display(); 14 | }; 15 | 16 | void complex::display() 17 | { 18 | 19 | cout<<"the sum of complex num "< 2 | void swap(int *p1, int *p2) 3 | { 4 | int temp = *p1; 5 | *p1 = *p2; 6 | *p2 = temp; 7 | } 8 | // This is an optimised code for the bubble sort 9 | void bSort(int arrnumbers[], int n) 10 | { 11 | int i, j; 12 | bool check; 13 | for (i = 0; i < n-1; i++) 14 | { 15 | check = false; 16 | for (j = 0; j < n-i-1; j++) 17 | { 18 | if (arrnumbers[j] > arrnumbers[j+1]) 19 | { 20 | swap(&arrnumbers[j], &arrnumbers[j+1]); 21 | check = true; 22 | } 23 | } 24 | // We are breaking from the loop in case two elements were not swapped by inner loop. 25 | if (check == false) 26 | break; 27 | } 28 | } 29 | //This function is to print the array sequence as final output after sorting 30 | void print(int arrnumbers[], int sizeofarray) 31 | { 32 | int i; 33 | for (i=0; i < sizeofarray; i++) 34 | printf("%d ", arrnumbers[i]); 35 | } 36 | // This the main program from where the execution will start 37 | int main() 38 | { 39 | int arrnumbers[] = {5, 6, 1, 0, 2, 9}; 40 | int n = sizeof(arrnumbers)/sizeof(arrnumbers[0]); 41 | bSort(arrnumbers, n); 42 | printf("Sorted array: \n"); 43 | print(arrnumbers, n); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Array.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int numbers[5] = {7, 5, 6, 12, 35}; 7 | 8 | cout << "The numbers are: "; 9 | 10 | // Printing array elements 11 | // using range based for loop 12 | for (const int &n : numbers) { 13 | cout << n << " "; 14 | } 15 | 16 | cout << "\nThe numbers are: "; 17 | 18 | // Printing array elements 19 | // using traditional for loop 20 | for (int i = 0; i < 5; ++i) { 21 | cout << numbers[i] << " "; 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Array00.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | int numbers[5] = {7, 5, 6, 12, 35}; 7 | 8 | cout << "The numbers are: "; 9 | 10 | // Printing array elements 11 | // using range based for loop 12 | for (const int &n : numbers) { 13 | cout << n << " "; 14 | } 15 | 16 | cout << "\nThe numbers are: "; 17 | 18 | // Printing array elements 19 | // using traditional for loop 20 | for (int i = 0; i < 5; ++i) { 21 | cout << numbers[i] << " "; 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Basic C Program to Concatenate Strings using Pointer_arman105735.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); 6 | char aa[100], bb[100]; 7 | 8 | printf("\nEnter the first string: "); 9 | gets(aa); // inputting first string 10 | 11 | printf("\nEnter the second string to be concatenated: "); 12 | gets(bb); // inputting second string 13 | 14 | char *a = aa; 15 | char *b = bb; 16 | 17 | // pointing to the end of the 1st string 18 | while(*a) // till it doesn't point to NULL-till string is not empty 19 | { 20 | a++; // point to the next letter of the string 21 | } 22 | while(*b) // till second string is not empty 23 | { 24 | *a = *b; 25 | b++; 26 | a++; 27 | } 28 | *a = '\0'; // string must end with '\0' 29 | printf("\n\n\nThe string after concatenation is: %s ", aa); 30 | printf("\n\n\t\t\tCoding is Fun !\n\n\n"); 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Binaryserch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | int binarysearch(int data[],int n,int item) 7 | { 8 | int left=0,right=n,flag=0; 9 | while(left<=right) 10 | { 11 | int mid=(right+left)/2; 12 | 13 | if(item>data[mid]) 14 | { 15 | left=mid+1; 16 | } 17 | else if(item>n; 43 | cout<<"Input: "; 44 | for(int i=0;i>data[i]; 47 | } 48 | cout<<"Item: "; 49 | cin>>item; 50 | 51 | binarysearch(data,n,item); 52 | 53 | 54 | } 55 | -------------------------------------------------------------------------------- /C Program to Add two user input numbers using Pointers_arman105735.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); 6 | int first, second, *p , *q, sum; 7 | printf("Enter two integers to add using pointers:\n"); 8 | scanf("%d%d", &first, &second); 9 | p = &first; 10 | q = &second; 11 | sum = *p + *q; 12 | printf("\n\nThe sum of the entered numbers is: %d", sum); 13 | printf("\n\n\t\t\tCoding is Fun !\n\n\n"); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /C Program to Find Roots of Quadratic Equation_arman105735.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include // This is needed to use sqrt() function 3 | 4 | int main() 5 | { 6 | printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); 7 | float a, b, c, determinant, r1, r2, real, imag; 8 | printf("\n\nEnter coefficients a, b and c: \n\n\n"); 9 | scanf("%f%f%f", &a, &b, &c); 10 | 11 | /* 12 | mathematical formula to know the 13 | nature of the roots 14 | */ 15 | determinant == b*b - 4*a*c; 16 | 17 | if(determinant > 0) // both roots are real 18 | { 19 | r1 = (-b + sqrt(determinant))/2*a; // Brackets are important 20 | r2 = (-b - sqrt(determinant))/2*a; 21 | printf("\n\n\nRoots are: %.2f and %.2f ", r1, r2); 22 | } 23 | else if(determinant == 0) // both roots are real and equal 24 | { 25 | r1 = r2 = -b/(2*a); // brackets are important 26 | printf("\n\n\nRoots are: %.2f and %.2f ", r1, r2); 27 | } 28 | /* 29 | Determinant < 0 - both roots are imaginary of the 30 | form real + i*imaginary 31 | */ 32 | else 33 | { 34 | real = -b/(2*a); 35 | imag = sqrt(-determinant)/(2*a); 36 | printf("\n\n\nRoots are %.2f + i%.2f and %.2f - i%.2f ", real, imag, real, imag); 37 | } 38 | printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n"); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /C Program to Simulate PRIORITY CPU Scheduling Algorithm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | void main() 5 | { 6 | int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10]; 7 | int totwt=0,totta=0; 8 | float awt,ata; 9 | char pn[10][10],t[10]; 10 | //clrscr(); 11 | printf("Enter the number of process:"); 12 | scanf("%d",&n); 13 | for(i=0; i 2 | 3 | int main() 4 | { 5 | printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); 6 | int choice, num, i; 7 | unsigned long int fact; 8 | 9 | while(1) 10 | { 11 | printf("1. Factorial \n"); 12 | printf("2. Prime\n"); 13 | printf("3. Odd\\Even\n"); 14 | printf("4. Exit\n\n\n"); 15 | printf("Enter your choice : "); 16 | scanf("%d",&choice); 17 | 18 | switch(choice) 19 | { 20 | case 1: 21 | printf("Enter number:\n"); 22 | scanf("%d", &num); 23 | fact = 1; 24 | for(i = 1; i <= num; i++) 25 | { 26 | fact = fact*i; 27 | } 28 | printf("\n\nFactorial value of %d is = %lu\n\n\n",num,fact); 29 | break; 30 | 31 | case 2: 32 | printf("Enter number:\n"); 33 | scanf("%d", &num); 34 | if(num == 1) 35 | printf("\n1 is neither prime nor composite\n\n"); 36 | for(i = 2; i < num; i++) 37 | { 38 | if(num%i == 0) 39 | { 40 | printf("\n%d is not a prime number\n\n", num); 41 | break; 42 | } 43 | 44 | } 45 | /* 46 | Not divisible by any number other 47 | than 1 and itself 48 | */ 49 | if(i == num) 50 | { 51 | printf("\n\n%d is a Prime number\n\n", num); 52 | break; 53 | } 54 | 55 | case 3: 56 | printf("Enter number:\n"); 57 | scanf("%d", &num); 58 | 59 | if(num%2 == 0) // 0 is considered to be an even number 60 | printf("\n\n%d is an Even number\n\n",num); 61 | else 62 | printf("\n\n%d is an Odd number\n\n",num); 63 | break; 64 | 65 | case 4: 66 | printf("\n\n\t\t\tCoding is Fun !\n\n\n"); 67 | exit(0); // terminates the complete program execution 68 | } 69 | } 70 | printf("\n\n\t\t\tCoding is Fun !\n\n\n"); 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /C Program to find the largest Element in an Array using Recursion_arman105735.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAX 100 4 | 5 | int getMaxElement(int []); // takes array of int as parameter 6 | int size; 7 | 8 | int main() 9 | { 10 | printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); 11 | int arr[MAX], max, i; 12 | printf("\n\nEnter the size of the array: "); 13 | scanf("%d", &size); 14 | printf("\n\nEnter %d elements\n\n", size); 15 | 16 | for(i = 0; i < size; i++) 17 | { 18 | scanf("%d", &arr[i]); 19 | } 20 | 21 | max = getMaxElement(arr); // passing the complete array as parameter 22 | printf("\n\nLargest element of the array is %d\n\n", max); 23 | printf("\n\n\t\t\tCoding is Fun !\n\n\n"); 24 | return 0; 25 | } 26 | 27 | int getMaxElement(int a[]) 28 | { 29 | static int i = 0, max =- 9999; // static int max=a[0] is invalid 30 | if(i < size) // till the last element 31 | { 32 | if(max < a[i]) 33 | max = a[i]; 34 | 35 | i++; // to check the next element in the next iteration 36 | getMaxElement(a); // recursive call 37 | } 38 | return max; 39 | } 40 | -------------------------------------------------------------------------------- /C program on the priority scheduling algorithm (Non preemptive) .c: -------------------------------------------------------------------------------- 1 | #include 2 | struct process 3 | { 4 | int id,WT,AT,BT,TAT,PR; 5 | }; 6 | struct process a[10]; 7 | 8 | // function for swapping 9 | void swap(int *b,int *c) 10 | { 11 | int tem; 12 | tem=*c; 13 | *c=*b; 14 | *b=tem; 15 | } 16 | 17 | //Driver function 18 | int main() 19 | { 20 | int n,check_ar=0; 21 | int Cmp_time=0; 22 | float Total_WT=0,Total_TAT=0,Avg_WT,Avg_TAT; 23 | printf("Enter the number of process \n"); 24 | scanf("%d",&n); 25 | printf("Enter the Arrival time , Burst time and priority of the process\n"); 26 | printf("AT BT PR\n"); 27 | for(int i=0;ia[j+1].AT) 49 | { 50 | swap(&a[j].id,&a[j+1].id); 51 | swap(&a[j].AT,&a[j+1].AT); 52 | swap(&a[j].BT,&a[j+1].BT); 53 | swap(&a[j].PR,&a[j+1].PR); 54 | } 55 | } 56 | } 57 | } 58 | 59 | // logic of Priority scheduling ( non preemptive) algo 60 | // if all the process are arrived at different time 61 | if(check_ar!=0) 62 | { 63 | a[0].WT=a[0].AT; 64 | a[0].TAT=a[0].BT-a[0].AT; 65 | // cmp_time for completion time 66 | Cmp_time=a[0].TAT; 67 | Total_WT=Total_WT+a[0].WT; 68 | Total_TAT=Total_TAT+a[0].TAT; 69 | for(int i=1;ia[j].PR && a[j].AT<=Cmp_time) 75 | { 76 | min=a[j].PR; 77 | swap(&a[i].id,&a[j].id); 78 | swap(&a[i].AT,&a[j].AT); 79 | swap(&a[i].BT,&a[j].BT); 80 | swap(&a[i].PR,&a[j].PR); 81 | 82 | } 83 | 84 | } 85 | a[i].WT=Cmp_time-a[i].AT; 86 | Total_WT=Total_WT+a[i].WT; 87 | // completion time of the process 88 | Cmp_time=Cmp_time+a[i].BT; 89 | 90 | // Turn Around Time of the process 91 | // compl-Arival 92 | a[i].TAT=Cmp_time-a[i].AT; 93 | Total_TAT=Total_TAT+a[i].TAT; 94 | 95 | } 96 | } 97 | 98 | // if all the process are arrived at same time 99 | else 100 | { 101 | for(int i=0;ia[j].PR && a[j].AT<=Cmp_time) 107 | { 108 | min=a[j].PR; 109 | swap(&a[i].id,&a[j].id); 110 | swap(&a[i].AT,&a[j].AT); 111 | swap(&a[i].BT,&a[j].BT); 112 | swap(&a[i].PR,&a[j].PR); 113 | } 114 | 115 | } 116 | a[i].WT=Cmp_time-a[i].AT; 117 | 118 | // completion time of the process 119 | Cmp_time=Cmp_time+a[i].BT; 120 | 121 | // Turn Around Time of the process 122 | // compl-Arrival 123 | a[i].TAT=Cmp_time-a[i].AT; 124 | Total_WT=Total_WT+a[i].WT; 125 | Total_TAT=Total_TAT+a[i].TAT; 126 | 127 | } 128 | 129 | } 130 | 131 | Avg_WT=Total_WT/n; 132 | Avg_TAT=Total_TAT/n; 133 | 134 | // Printing of the results 135 | printf("The process are\n"); 136 | printf("ID WT TAT\n"); 137 | for(int i=0;i2)|Θ(n2)|Ω(n)| 18 | 19 | |In-place?|Stable?| 20 | |---|---| 21 | |Yes|Yes| 22 | 23 | 24 | ## Selection Sort 25 | Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited. 26 | 27 | The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right. 28 | 29 | Time complexity analysis: 30 | 31 | |Worst Case|Average Case|Best Case| 32 | |---|---|---| 33 | |O(n2)|Θ(n2)|Ω(n2)| 34 | 35 | |In-place?|Stable?| 36 | |---|---| 37 | |Yes|No| 38 | 39 | 40 | ## Insertion Sort 41 | Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. 42 | Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain. 43 | 44 | Time complexity analysis: 45 | 46 | |Worst Case|Average Case|Best Case| 47 | |---|---|---| 48 | |O(n2)|Θ(n2)|Ω(n)| 49 | 50 | |In-place?|Stable?| 51 | |---|---| 52 | |Yes|Yes| 53 | 54 | 55 | ## Counting Sort 56 | Counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key value, and using arithmetic on those counts to determine the positions of each key value in the output sequence. Its running time is linear in the number of items and the difference between the maximum and minimum key values, so it is only suitable for direct use in situations where the variation in keys is not significantly greater than the number of items. However, it is often used as a subroutine in another sorting algorithm, radix sort, that can handle larger keys more efficiently. 57 | 58 | Time complexity analysis: 59 | 60 | |Worst Case|Average Case|Best Case| 61 | |---|---|---| 62 | |O(n+k)|Θ(n+k>)|Ω(n+k)| 63 | 64 | |In-place?|Stable?| 65 | |---|---| 66 | |No|Yes| 67 | 68 | 69 | ## Cycle Sort 70 | Cycle sort is an in-place, unstable sorting algorithm, a comparison sort that is theoretically optimal in terms of the total number of writes to the original array, unlike any other in-place sorting algorithm. It is based on the idea that the permutation to be sorted can be factored into cycles, which can individually be rotated to give a sorted result. 71 | 72 | Time complexity analysis: 73 | 74 | |Worst Case|Average Case|Best Case| 75 | |---|---|---| 76 | |O(n2)|Θ(n2)|Ω(n2)| 77 | 78 | |In-place?|Stable?| 79 | |---|---| 80 | |Yes|No| 81 | 82 | 83 | ## Heap Sort 84 | Heapsort is a comparison-based sorting algorithm. Heapsort can be thought of as an improved selection sort: like that algorithm, it divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. The improvement consists of the use of a heap data structure rather than a linear-time search to find the maximum. 85 | 86 | The heapsort algorithm involves preparing the list by first turning it into a max heap. The algorithm then repeatedly swaps the first value of the list with the last value, decreasing the range of values considered in the heap operation by one, and sifting the new first value into its position in the heap. This repeats until the range of considered values is one value in length. 87 | 88 | Time complexity analysis: 89 | 90 | |Worst Case|Average Case|Best Case| 91 | |---|---|---| 92 | |O(n log(n))|Θ(n log(n))|Ω(n) (bottom up)| 93 | 94 | |In-place?|Stable?| 95 | |---|---| 96 | |Yes|No| 97 | 98 | 99 | ## Merge Sort 100 | Merge sort is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output. Merge sort is a divide and conquer algorithm. 101 | Conceptually, a merge sort works as follows: 102 | 103 | 1. Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted). 104 | 2. Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list. 105 | 106 | Time complexity analysis: 107 | 108 | |Worst Case|Average Case|Best Case| 109 | |---|---|---| 110 | |O(n log(n))|Θ(n log(n))|Ω(n log(n))| 111 | 112 | |In-place?|Stable?| 113 | |---|---| 114 | |No|Yes| 115 | 116 | 117 | ## Quick Sort 118 | Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. When implemented well, it can be about two or three times faster than its main competitors, merge sort and heapsort. 119 | 120 | Quicksort is a comparison sort, meaning that it can sort items of any type for which a "less-than" relation (formally, a total order) is defined. In efficient implementations it is not a stable sort, meaning that the relative order of equal sort items is not preserved. Quicksort can operate in-place on an array, requiring small additional amounts of memory to perform the sorting. It is very similar to selection sort, except that it does not always choose worst-case partition. 121 | 122 | Time complexity analysis: 123 | 124 | |Worst Case|Average Case|Best Case| 125 | |---|---|---| 126 | |O(n2)|Θ(n log(n))|Ω(n log(n))| 127 | 128 | |In-place?|Stable?| 129 | |---|---| 130 | |Yes|No| 131 | 132 | 133 | ## Radix Sort 134 | Radix sort is a non-comparative integer sorting algorithm that sorts data with integer keys by grouping keys by the individual digits which share the same significant position and value. A positional notation is required, but because integers can represent strings of characters (e.g., names or dates) and specially formatted floating point numbers, radix sort is not limited to integers. 135 | 136 | Time complexity analysis: 137 | 138 | |Worst Case|Average Case|Best Case| 139 | |---|---|---| 140 | |O(nk)|Θ(nk)|Ω(nk)| 141 | 142 | |In-place?|Stable?| 143 | |---|---| 144 | |Depends on implementation|Depends on implementation| 145 | 146 | 147 | ## Cocktail Sort 148 | Cocktail Sort is a variation of Bubble sort. The Bubble sort algorithm always traverses elements from left and moves the largest element to its correct position in first iteration and second largest in second iteration and so on. Cocktail Sort traverses through a given array in both directions alternatively. 149 | 150 | Time complexity analysis: 151 | 152 | |Worst Case|Average Case|Best Case| 153 | |---|---|---| 154 | |O(n2)|Θ(n2)|Ω(n)| 155 | 156 | |In-place?|Stable?| 157 | |---|---| 158 | |Yes|Yes| 159 | 160 | 161 | ## Bogo Sort 162 | Also known as Permutation Sort. Stupid Sort, Slowsort, Shotgun Sort, or Monkey Sort, Bogosort randomly generates permutations of the input and checks if it is sorted until it generates one that happens to be sorted by chance. Not meant to be an efficient sort, Bogosort is largely used for educational purposes and for comparison to actual sorting algorithms for contrast. It has a best case of O(n) where the list is already sorted, an average performance of O((n+1)!) and an unbounded worst case performance. 163 | 164 | Time complexity analysis: 165 | 166 | |Worst Case|Average Case|Best Case| 167 | |---|---|---| 168 | |Unbounded|O((n+1)!)|Ω(n)| 169 | 170 | |In-place?|Stable?| 171 | |---|---| 172 | |Yes|No| 173 | 174 | 175 | 176 | ## Bozo Sort 177 | Bozosort is somewhat similar to Bogosort in its random behaviour. It works by choosing two random elements of the list and swapping them, until the list is actually sorted. 178 | The running time analysis of a bozosort is difficult, but some estimates are found in H. O(n!) is found to be the expected average case. Considering that every iteration calls a function that checks if the list is ordered or not, that makes the average case actually O((n+1)!) 179 | 180 | Time complexity analysis: 181 | 182 | |Worst Case|Average Case|Best Case| 183 | |---|---|---| 184 | |Unbounded|O((n+1)!) (estimated)|Ω(n)| 185 | 186 | |In-place?|Stable?| 187 | |---|---| 188 | |Yes|No| 189 | 190 | 191 | ## Shell Sort 192 | Shellsort, also known as Shell sort or Shell's method, is an in-place comparison sort. It can be seen as either a generalization of sorting by exchange (bubble sort) or sorting by insertion (insertion sort).The method starts by sorting pairs of elements far apart from each other, then progressively reducing the gap between elements to be compared. Starting with far apart elements, it can move some out-of-place elements into position faster than a simple nearest neighbor exchange. Donald Shell published the first version of this sort in 1959.The running time of Shellsort is heavily dependent on the gap sequence it uses. For many practical variants, determining their time complexity remains an open problem. 193 | 194 | Time complexity analysis: 195 | 196 | |Worst Case|Average Case|Best Case| 197 | |---|---|---| 198 | |O(n2)|Depends on Implementation|Ω(n log(n))| 199 | 200 | |In-place?|Stable?| 201 | |---|---| 202 | |Yes|No| 203 | 204 | 205 | ## IntroSort 206 | Introsort or introspective sort is a hybrid sorting algorithm that provides both fast average performance and (asymptotically) optimal worst-case performance. It begins with quicksort and switches to heapsort when the recursion depth exceeds a level based on (the logarithm of) the number of elements being sorted. This combines the good parts of both algorithms, with practical performance comparable to quicksort on typical data sets and worst-case O(n log n) runtime due to the heap sort. Since both algorithms it uses are comparison sorts, it too is a comparison sort. 207 | 208 | Time complexity analysis: 209 | 210 | |Worst Case|Average Case|Best Case| 211 | |---|---|---| 212 | |O(n log(n))|Θ(n log(n))|O(n log(n))| 213 | 214 | |In-place?|Stable?| 215 | |---|---| 216 | |Yes|No| 217 | 218 | 219 | ## Bucket Sort 220 | Bucket sort is a sorting algorithm that involves first putting elements that need to be sorted into "buckets", sorting these buckets and then merging the results to get the sorted list. It is best used on data that is uniformly distributed over a range and can perform quite well, O(n + m) (where n is the number of elements in the list and m is the number of buckets used). The buckets are typically stored as an array of linked lists so there is an additional space requirement for this sorting algorithm. 221 | For example, if there is a list of numbers in the range 1 to 1000 we can have 10 buckets. All numbers between 1 and 100 get added to the first bucket, between 101 and 200 get added to the second bucket, etc. After all numbers have been added to the appropriate bucket insertion sort is applied to all the buckets. This step is the bottleneck for the algorithm. If the data is not clustered tightly than the sorts can be completed in linear time despite the fact that insertion sort is O(n2). The algorithm degrades as certain buckets get more of the numbers, the worst case being when all the numbers are in a single bucket. The data in the input array must uniformly distributed across the range of bucket values to avoid the polynomial time. The size of each bucket should be equal to the number of buckets. As bucket sort is polynomial in the worse case Quicksort is a more optimal sorting algorithm. 222 | 223 | Time complexity analysis: 224 | 225 | |Worst Case|Average Case|Best Case| 226 | |---|---|---| 227 | |O(n2)|Θ(n+k)|Ω(n+k)| 228 | 229 | |In-place?|Stable?| 230 | |---|---| 231 | |Yes|Yes| 232 | 233 | 234 | ## Gnome sort 235 | Gnome sort is a sorting algorithm which is similar to insertion sort, except that moving an element to its proper place is accomplished by a series of swaps, similar to a bubble sort. It is conceptually simple, requiring no nested loops. The average, or expected, running time is O(n2) but tends towards O(n) if the list is initially almost sorted. 236 | 237 | The algorithm always finds the first place where two adjacent elements are in the wrong order and swaps them. It takes advantage of the fact that performing a swap can introduce a new out-of-order adjacent pair next to the previously swapped elements. It does not assume that elements forward of the current position are sorted, so it only needs to check the position directly previous to the swapped elements. 238 | 239 | Time complexity analysis: 240 | 241 | |Worst Case|Average Case|Best Case| 242 | |---|---|---| 243 | |O(n2)|O(n2)|Ω(n2)| 244 | 245 | |In-place?|Stable?| 246 | |---|---| 247 | |Yes|Yes| 248 | 249 | 250 | ## Comb sort 251 | 252 | Comb sort improves on bubble sort. The basic idea is to eliminate turtles, or small values near the end of the list, since in a bubble sort these slow the sorting down tremendously. Rabbits, large values around the beginning of the list, do not pose a problem in bubble sort. 253 | 254 | In bubble sort, when any two elements are compared, they always have a gap (distance from each other) of 1. The basic idea of comb sort is that the gap can be much more than 1. The inner loop of bubble sort, which does the actual swap, is modified such that gap between swapped elements goes down (for each iteration of outer loop) in steps of a "shrink factor" k: \[ n/k, n/k2, n/k3, ..., 1 \]. 255 | 256 | The gap starts out as the length of the list n being sorted divided by the shrink factor k (generally 1.3; see below) and one pass of the aforementioned modified bubble sort is applied with that gap. Then the gap is divided by the shrink factor again, the list is sorted with this new gap, and the process repeats until the gap is 1. At this point, comb sort continues using a gap of 1 until the list is fully sorted. The final stage of the sort is thus equivalent to a bubble sort, but by this time most turtles have been dealt with, so a bubble sort will be efficient. 257 | 258 | The shrink factor has a great effect on the efficiency of comb sort. k = 1.3 has been suggested as an ideal shrink factor by the authors of the original article after empirical testing on over 200,000 random lists. A value too small slows the algorithm down by making unnecessarily many comparisons, whereas a value too large fails to effectively deal with turtles, making it require many passes with 1 gap size. 259 | 260 | The pattern of repeated sorting passes with decreasing gaps is similar to Shellsort, but in Shellsort the array is sorted completely each pass before going on to the next-smallest gap. Comb sort's passes do not completely sort the elements. This is the reason that Shellsort gap sequences have a larger optimal shrink factor of about 2.2. 261 | 262 | Time complexity analysis: 263 | 264 | |Worst Case|Average Case|Best Case| 265 | |---|---|---| 266 | |O(n2)|Θ(n2/2p),where p is the number of increments|Ω(n log(n))| 267 | 268 | |In-place?|Stable?| 269 | |---|---| 270 | |Yes|No| 271 | 272 | 273 | ## Tim sort 274 | 275 | Timsort is a hybrid stable sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data. It uses techniques from Peter McIlroy's "Optimistic Sorting and Information Theoretic Complexity", in Proceedings of the Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp. 467–474, January 1993. It was implemented by Tim Peters in 2002 for use in the Python programming language. The algorithm finds subsequences of the data that are already ordered, and uses that knowledge to sort the remainder more efficiently. This is done by merging an identified subsequence, called a run, with existing runs until certain criteria are fulfilled. Timsort has been Python's standard sorting algorithm since version 2.3. It is also used to sort arrays of non-primitive type in Java SE 7, on the Android platform, and in GNU Octave. 276 | 277 | Time complexity analysis: 278 | 279 | |Worst Case|Average Case|Best Case| 280 | |---|---|---| 281 | |O(n log(n))|O(n log(n)|O(n)| 282 | 283 | |In-place?|Stable?| 284 | |---|---| 285 | |No|Yes| 286 | 287 | 288 | ## Tournament sort 289 | 290 | Tournament sort improves upon the naive selection sort by using a priority queue to find the next element in the sort. In the naive selection sort, it takes O(n) operations to select the next element of n elements; in a tournament sort, it takes O(log n) operations (after building the initial tournament in O(n)). Tournament sort is a variation of heapsort. 291 | 292 | Time complexity analysis: 293 | 294 | |Worst Case|Average Case|Best Case| 295 | |---|---|---| 296 | |O(n log(n))|O(n log(n)|O(n log(n))| 297 | 298 | |In-place?|Stable?| 299 | |---|---| 300 | |No|No| 301 | 302 | 303 | 304 | ### IN SHORT: 305 | Red and black is used for searching whereas for sorting it is better to use Heapsort(for local projects).The time complexity is for the worst case is "2nlogn" and best case is "n". But it is not stable, if you prefer the stable sort it is better to use the merge sort. O(n) = n log n. If the sorting is for priority of top 10 or least 10, then use priority Queue. (Max-PQ and Min-PQ). 306 | Source : (use this links for the code, it is an industrial code and the code is written in generics(can use any datatypes)) 307 | Heap: https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Heap.java.html 308 | Merge: https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Merge.java.html 309 | For other sorting and searching algorithms: https://algs4.cs.princeton.edu/code/ 310 | 311 | 312 | ## Bitonic Sort 313 | The Bitonic Sort is a parallel comparison-based sorting algorithm which does O(nlogn) comparisons. It is also called as the Bitonic Merge Sort. The Bitonic Sort is based on the concept of converting the given sequence into a Bitonic Sequence. A Bitonic Sequence is a sequence of numbers which is first strictly increasing then after a point strictly decreasing. Although, the number of comparisons are more than that in any other popular sorting algorithm, It performs better for the parallel implementation because elements are compared in predefined sequence which must not be depended upon the data being sorted. The predefined sequence is called Bitonic sequence. Therefore it is suitable for implementation in hardware and parallel processor array. 314 | 315 | Time complexity analysis: 316 | 317 | |Worst Case|Average Case|Best Case| 318 | |---|---|---| 319 | |O(log2n)|O(log2n)|O(log2n)| 320 | 321 | |In-place?|Stable?| 322 | |---|---| 323 | |Yes|No| 324 | 325 | 326 | ## Pancake Sort 327 | Pancake sorting is the colloquial term for the mathematical problem of sorting a disordered stack of pancakes in order of size when a spatula can be inserted at any point in the stack and used to flip all pancakes above it. A pancake number is the minimum number of flips required for a given number of pancakes. 328 | Unlike a traditional sorting algorithm, which attempts to sort with the fewest comparisons possible, the goal is to sort the sequence in as few reversals as possible. 329 | 330 | Time complexity analysis: 331 | 332 | |Worst Case|Average Case|Best Case| 333 | |---|---|---| 334 | |O(n^2)|O(n)|O(n)| 335 | 336 | |In-place?|Stable?| 337 | |---|---| 338 | |No|No| 339 | 340 | ## Stooge Sort 341 | 342 | The Stooge sort is a recursive sorting algorithm. It is defined for ascending order sorting.Stooge sort is a recursive sorting algorithm. It is notable for its exceptionally bad time complexity of O(n^(log 3 / log 1.5 ) = O(n2.7095...). The running time of the algorithm is thus slower compared to reasonable sorting algorithms, and is slower than Bubble sort, a canonical example of a fairly inefficient sort. It is however more efficient than Slowsort 343 | 344 | ## 3 Way Merge Sort 345 | Merge sort involves recursively splitting the array into 2 parts, sorting and finally merging them. A variant of merge sort is called 3-way merge sort where instead of splitting the array into 2 parts we split it into 3 parts. 346 | Merge sort recursively breaks down the arrays to subarrays of size half. Similarly, 3-way Merge sort breaks down the arrays to subarrays of size one third. 347 | 348 | ## Contributing 349 | To start contributing, check out [CONTRIBUTING.md](https://github.com/MdHRShohel/scheduling-algos-in-c-or-cpp/tree/main)). New contributors are always welcome to support this project. Check out issues labelled as `Hacktoberfest` if you are up for some grabs! :) 350 | -------------------------------------------------------------------------------- /Cycle_sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void cycleSort(int a[], int n) { 4 | int writes = 0; 5 | for (int c_start = 0; c_start <= n - 2; c_start++) { 6 | int item = a[c_start]; 7 | int pos = c_start; 8 | for (int i = c_start + 1; i < n; i++) 9 | if (a[i] < item) 10 | pos++; 11 | if (pos == c_start) 12 | continue; 13 | while (item == a[pos]) 14 | pos += 1; 15 | if (pos != c_start) { 16 | swap(item, a[pos]); 17 | writes++; 18 | } 19 | while (pos != c_start) { 20 | pos = c_start; 21 | for (int i = c_start + 1; i < n; i++) 22 | if (a[i] < item) 23 | pos += 1; 24 | while (item == a[pos]) 25 | pos += 1; 26 | if (item != a[pos]) { 27 | swap(item, a[pos]); 28 | writes++; 29 | } 30 | } 31 | } 32 | } 33 | int main() { 34 | int a[] ={7,4,3,5,2,1,6}; 35 | int n = 7; 36 | cycleSort(a, n); 37 | for (int i = 0; i < n; i++) 38 | cout << a[i] << " "; 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Hipsort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void heapify(int arr[], int n, int i) { 6 | int largest = i; 7 | int left = 2 * i + 1; 8 | int right = 2 * i + 2; 9 | 10 | if (left < n && arr[left] > arr[largest]) 11 | largest = left; 12 | 13 | if (right < n && arr[right] > arr[largest]) 14 | largest = right; 15 | 16 | if (largest != i) { 17 | swap(arr[i], arr[largest]); 18 | heapify(arr, n, largest); 19 | } 20 | } 21 | void heapSort(int arr[], int n) { 22 | for (int i = (n / 2) - 1; i >= 0; i--) 23 | { 24 | heapify(arr, n, i); 25 | } 26 | for (int i = n - 1; i >= 0; i--) { 27 | swap(arr[0], arr[i]); 28 | heapify(arr, i, 0); 29 | } 30 | } 31 | 32 | int main() { 33 | int size,index,insertion,arr[100]; 34 | cout<<"HOW MANY NUMBERS DO YOU WANT TO TAKE- "; 35 | cin>>size; 36 | cout<>arr[i]; 41 | } 42 | heapSort(arr, size); 43 | cout<<"SORT: "; 44 | for (int i = 0; i < size; i++) 45 | cout << arr[i] << " "; 46 | cout << endl; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /Loop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main () { 5 | for(int i=1;i<=3;i++){ 6 | for(int j=1;j<=3;j++){ 7 | cout< 5 | 6 | void main() 7 | { 8 | int process, resource, i, j, instance, k=0, count=0, temp=0; 9 | printf("\n Enter No. of Process : "); 10 | scanf("%d", &process); 11 | printf(" Enter No. of Resources : "); 12 | scanf("%d",&resource); 13 | int available[resource], max[process][resource], allocated[process][resource], need[process][resource], completed[process]; 14 | 15 | for(i=0; i P[%d] ", i); 70 | completed[i] = 1; 71 | for(j=0; j FCFS, SSTF, SCAN 3 | 4 | #include 5 | #include 6 | int seektime = 0; 7 | 8 | void fcfs(int sequence[], int head, int n) 9 | { 10 | int temp = head; 11 | seektime = 0; 12 | printf("\n The Disk sequence is : \n"); 13 | for(int i=0; i %d", sequence[i]); 16 | seektime += abs(sequence[i]-temp); 17 | temp = sequence[i]; 18 | } 19 | 20 | printf("\n\n Seek Time = %d", seektime); 21 | } 22 | 23 | 24 | void sstf(int sequence[], int head, int n) 25 | { 26 | seektime = 0; 27 | int arr[n], min, temp, i, j, pos; 28 | 29 | for(i=0; i abs(head - sequence[i])) 42 | { 43 | min = abs(head - sequence[i]); 44 | pos = i; 45 | } 46 | } 47 | } 48 | if(min == 999) 49 | break; 50 | arr[pos] = 1; 51 | seektime += min; 52 | head = sequence[pos]; 53 | printf(" > %d", sequence[pos]); 54 | } 55 | printf("\n\n Seek Time = %d", seektime); 56 | 57 | } 58 | 59 | void scan(int sequence[], int head, int n, int t) 60 | { 61 | int temp, i, j; 62 | seektime = 0; 63 | 64 | printf("\n The Disk sequence is : \n"); 65 | for(i=0;i sequence[j]) 70 | { 71 | temp = sequence[i]; 72 | sequence[i] = sequence[j]; 73 | sequence[j] = temp; 74 | } 75 | } 76 | 77 | } 78 | temp = head; 79 | for(i=0; i head) 82 | { 83 | printf(" > %d", sequence[i]); 84 | seektime += abs(temp - sequence[i]); 85 | temp = sequence[i]; 86 | } 87 | } 88 | 89 | 90 | seektime += abs(t-1 - sequence[i-1]); 91 | temp = t-1; 92 | for(i=n-1; i>=0; i--) 93 | { 94 | if(head > sequence[i]) 95 | { 96 | printf(" > %d", sequence[i]); 97 | seektime += abs(temp - sequence[i]); 98 | temp = sequence[i]; 99 | } 100 | } 101 | printf("\n\n Seek Time = %d", seektime); 102 | } 103 | 104 | 105 | void main() 106 | { 107 | int n, t, i, head, temp, choice; 108 | printf("\n Enter number of disk request in queue : "); 109 | scanf("%d", &n); 110 | printf(" Enter total number of tracks : "); 111 | scanf("%d", &t); 112 | int sequence[n]; 113 | printf("\n Enter the disk request sequence for a disk with %d tracks : ", t); 114 | for(i=0; i 4 | using namespace std; 5 | 6 | // Function to find the waiting time for all 7 | // processes 8 | void findWaitingTime(int processes[], int n, 9 | int bt[], int wt[]) 10 | { 11 | // waiting time for first process is 0 12 | wt[0] = 0; 13 | 14 | // calculating waiting time 15 | for (int i = 1; i < n ; i++ ) 16 | wt[i] = bt[i-1] + wt[i-1] ; 17 | } 18 | 19 | // Function to calculate turn around time 20 | void findTurnAroundTime( int processes[], int n, 21 | int bt[], int wt[], int tat[]) 22 | { 23 | // calculating turnaround time by adding 24 | // bt[i] + wt[i] 25 | for (int i = 0; i < n ; i++) 26 | tat[i] = bt[i] + wt[i]; 27 | } 28 | 29 | //Function to calculate average time 30 | void findavgTime( int processes[], int n, int bt[]) 31 | { 32 | int wt[n], tat[n], total_wt = 0, total_tat = 0; 33 | 34 | //Function to find waiting time of all processes 35 | findWaitingTime(processes, n, bt, wt); 36 | 37 | //Function to find turn around time for all processes 38 | findTurnAroundTime(processes, n, bt, wt, tat); 39 | 40 | //Display processes along with all details 41 | cout << "Processes "<< " Burst time " 42 | << " Waiting time " << " Turn around time\n"; 43 | 44 | // Calculate total waiting time and total turn 45 | // around time 46 | for (int i=0; i 0) 32 | { 33 | total = total + temp[i]; 34 | temp[i] = 0; 35 | counter = 1; 36 | } 37 | else if(temp[i] > 0) 38 | { 39 | temp[i] = temp[i] - time_quantum; 40 | total = total + time_quantum; 41 | } 42 | if(temp[i] == 0 && counter == 1) 43 | { 44 | x--; 45 | printf("nProcess[%d]tt%dtt %dttt %d", i + 1, burst_time[i], total - arrival_time[i], total - arrival_time[i] - burst_time[i]); 46 | wait_time = wait_time + total - arrival_time[i] - burst_time[i]; 47 | turnaround_time = turnaround_time + total - arrival_time[i]; 48 | counter = 0; 49 | } 50 | if(i == limit - 1) 51 | { 52 | i = 0; 53 | } 54 | else if(arrival_time[i + 1] <= total) 55 | { 56 | i++; 57 | } 58 | else 59 | { 60 | i = 0; 61 | } 62 | } 63 | 64 | average_wait_time = wait_time * 1.0 / limit; 65 | average_turnaround_time = turnaround_time * 1.0 / limit; 66 | printf("nnAverage Waiting Time:t%f", average_wait_time); 67 | printf("nAvg Turnaround Time:t%fn", average_turnaround_time); 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /Mahfuzpriority.c: -------------------------------------------------------------------------------- 1 | 2 | // CPU-Scheduling-Algorithm-In-C 3 | // Non Pre-emptive Priority Scheduling Algorithm 4 | 5 | #include 6 | #include 7 | 8 | void main() 9 | { 10 | int n, i, j, pos, temp, *bt, *wt, *tat, *p, *pt; 11 | float avgwt = 0, avgtat = 0; 12 | printf("\n Enter the number of processes : "); 13 | scanf("%d", &n); 14 | 15 | p = (int*)malloc(n*sizeof(int)); 16 | bt = (int*)malloc(n*sizeof(int)); 17 | wt = (int*)malloc(n*sizeof(int)); 18 | tat = (int*)malloc(n*sizeof(int)); 19 | 20 | printf("\n Enter the burst time and priority for each process "); 21 | for(i=0; i 2 | 3 | int main () { 4 | 5 | // an array with 5 rows and 2 columns. 6 | 7 | int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; 8 | 9 | 10 | 11 | // output each array element's value 12 | 13 | for ( int i = 0; i < 5; i++ ) 14 | 15 | for ( int j = 0; j < 2; j++ ) { 16 | 17 | 18 | 19 | cout << "a[" << i << "][" << j << "]: "; 20 | 21 | cout << a[i][j]<< endl; 22 | 23 | } 24 | -------------------------------------------------------------------------------- /MultiLevelQueueScheduling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int p[20],bt[20], su[20], wt[20],tat[20],i, k, n, temp; 5 | float wtavg, tatavg; 6 | printf("Enter the number of processes:"); 7 | scanf("%d",&n); 8 | for(i=0;i su[k]) 19 | { 20 | temp=p[i]; 21 | p[i]=p[k]; 22 | p[k]=temp; 23 | temp=bt[i]; 24 | bt[i]=bt[k]; 25 | bt[k]=temp; 26 | temp=su[i]; 27 | su[i]=su[k]; 28 | su[k]=temp; 29 | } 30 | wtavg = wt[0] = 0; 31 | tatavg = tat[0] = bt[0]; 32 | for(i=1;i 2 | #include 3 | #include 4 | #include //For accumulate operation 5 | 6 | using namespace std; 7 | 8 | 9 | int main() 10 | { 11 | 12 | // Initializing vector with array values 13 | 14 | int arr[] = {10, 20, 5, 23 ,42 , 15}; 15 | 16 | int n = sizeof(arr)/sizeof(arr[0]); 17 | 18 | vector vect(arr, arr+n); 19 | 20 | 21 | cout << "Vector is: "; 22 | 23 | for (int i=0; i()); 47 | 48 | 49 | 50 | cout << "\nVector after sorting in Descending order is: "; 51 | 52 | for (int i=0; i 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | /*Visit - www.programmingwithbasics.com*/ 7 | 8 | printf("====================================="); 9 | printf("\nVisit - www.programmingwithbasics.com"); 10 | printf("\n====================================="); 11 | 12 | /* 13 | Here Integer Variables for loop, swapping and assign value and float variable for waiting time, turn around time and Average waiting time and Average turn around time. 14 | */ 15 | 16 | int n,i,j,temp,min; 17 | float c1=0,c2=0,avwt,avtrt; 18 | 19 | int *cbt = new int[n]; 20 | int *p = new int[n]; 21 | int *s = new int[n]; 22 | int *wt = new int[n]; 23 | int *trt = new int[n]; 24 | 25 | /* 26 | Here asking for user Enter Number of Process 27 | */ 28 | 29 | cout<<"\nEnter The Number of Process: "; 30 | cin>>n; 31 | 32 | /* 33 | Storing the Burst time of each process in an array and process ID of the process. 34 | */ 35 | 36 | cout<<"\n========================================================"; 37 | cout<<"\nEnter The CBT(Burst Time) of Processes"; 38 | cout<<"\n========================================================\n"; 39 | 40 | for(i = 0; i< n; i++ ) 41 | { 42 | cin>>cbt[i]; 43 | s[i] = i+1; 44 | } 45 | 46 | /* 47 | Storing the Priority of the each process 48 | */ 49 | 50 | cout<<"\n========================================================"; 51 | cout<<"\nEnter The Priority of Process"; 52 | cout<<"\n========================================================\n"; 53 | for(i = 0; i< n; i++ ) 54 | { 55 | cin>>p[i]; 56 | } 57 | 58 | /* 59 | This is the main important part here we are sorting an array according to priority given and if 60 | we are sorting priority array than Burst Time array and process ID array also need to sort 61 | */ 62 | 63 | for(i = 0 ; i p[min] ) 69 | { 70 | min=j; 71 | } 72 | } 73 | 74 | temp = p[i]; 75 | p[i] = p[min]; 76 | p[min] = temp; 77 | 78 | temp = cbt[i]; 79 | cbt[i] = cbt[min]; 80 | cbt[min] = temp; 81 | 82 | temp = s[i]; 83 | s[i] = s[min]; 84 | s[min] = temp; 85 | } 86 | 87 | /* 88 | Starting waiting time of a process will be zero. 89 | */ 90 | 91 | wt[0] = 0; 92 | 93 | /* 94 | Here We are calculating waiting time, after calculating total waiting time we are calculating Average 95 | Waiting time divide. Average Waiting Time = Total Waiting Time / Number of Process. 96 | */ 97 | 98 | for(i=1;i 2 | #include 3 | using namespace std; 4 | 5 | struct node{ 6 | char pname; 7 | int btime; 8 | int atime; 9 | int priority; 10 | int restime=0; 11 | int ctime=0; 12 | int wtime=0; 13 | }a[1000],b[1000],c[1000]; 14 | 15 | void insert(int n){ 16 | int i; 17 | for(i=0;i>a[i].pname; 19 | cin>>a[i].priority; 20 | cin>>a[i].atime; 21 | cin>>a[i].btime; 22 | a[i].wtime=-a[i].atime+1; 23 | } 24 | } 25 | 26 | bool btimeSort(node a,node b){ 27 | return a.btime < b.btime; 28 | } 29 | 30 | bool atimeSort(node a,node b){ 31 | return a.atime < b.atime; 32 | } 33 | bool prioritySort(node a,node b){ 34 | return a.priority < b.priority; 35 | } 36 | int k=0,f=0,r=0; 37 | void disp(int nop,int qt){ 38 | int n=nop,q; 39 | sort(a,a+n,atimeSort); 40 | int ttime=0,i; 41 | int j,tArray[n]; 42 | int alltime=0; 43 | bool moveLast=false; 44 | for(i=0;iqt){ 73 | c[k]=b[j]; 74 | c[k].btime=qt; 75 | k++; 76 | b[j].btime=b[j].btime-qt; 77 | ttime+=qt; 78 | moveLast=true; 79 | for(q=0;q=n) 98 | break; 99 | } 100 | tArray[i]=ttime; 101 | ttime+=a[i].btime; 102 | for(i=0;i>nop; 177 | cout<<"Enter process, priority, AT, BT\n"; 178 | insert(nop); 179 | disp(nop,1); 180 | return 0; 181 | } 182 | -------------------------------------------------------------------------------- /Priority-scheduling.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | struct Process { 4 | int pid; // Process ID 5 | int bt; // CPU Burst time required 6 | int priority; // Priority of this process 7 | }; 8 | // sorting the Process acc. to the priority 9 | bool compare(Process a, Process b) { 10 | return (a.priority > b.priority); 11 | } 12 | void waitingtime(Process pro[], int n, int wt[]) { 13 | // Initial waiting time for a process is 0 14 | wt[0] = 0; 15 | // calculating waiting time 16 | for (int i = 1; i < n ; i++ ) 17 | wt[i] = pro[i-1].bt + wt[i-1] ; 18 | } 19 | // Function to calculate turn around time 20 | void turnarround( Process pro[], int n, int wt[], int tat[]) { 21 | // calculating turnaround time by adding 22 | // bt[i] + wt[i] 23 | for (int i = 0; i < n ; i++) 24 | tat[i] = pro[i].bt + wt[i]; 25 | } 26 | //Function to calculate average time 27 | void avgtime(Process pro[], int n) { 28 | int wt[n], tat[n], total_wt = 0, total_tat = 0; 29 | //Function to find waiting time of all processes 30 | waitingtime(pro, n, wt); 31 | //Function to find turn around time for all processes 32 | turnarround(pro, n, wt, tat); 33 | //Display processes along with all details 34 | cout << "\nProcesses "<< " Burst time " << " Waiting time " << " Turn around time\n"; 35 | // Calculate total waiting time and total turn 36 | // around time 37 | for (int i=0; i 4 | // Function to find the waiting time for all 5 | // processes 6 | void findWaitingTime(int processes[], int n, 7 | int bt[], int wt[]) 8 | { 9 | // waiting time for first process is 0 10 | wt[0] = 0; 11 | 12 | // calculating waiting time 13 | for (int i = 1; i < n ; i++ ) 14 | wt[i] = bt[i-1] + wt[i-1] ; 15 | } 16 | 17 | // Function to calculate turn around time 18 | void findTurnAroundTime( int processes[], int n, 19 | int bt[], int wt[], int tat[]) 20 | { 21 | // calculating turnaround time by adding 22 | // bt[i] + wt[i] 23 | for (int i = 0; i < n ; i++) 24 | tat[i] = bt[i] + wt[i]; 25 | } 26 | 27 | //Function to calculate average time 28 | void findavgTime( int processes[], int n, int bt[]) 29 | { 30 | int wt[n], tat[n], total_wt = 0, total_tat = 0; 31 | 32 | //Function to find waiting time of all processes 33 | findWaitingTime(processes, n, bt, wt); 34 | 35 | //Function to find turn around time for all processes 36 | findTurnAroundTime(processes, n, bt, wt, tat); 37 | 38 | //Display processes along with all details 39 | printf("Processes Burst time Waiting time Turn around time\n"); 40 | 41 | // Calculate total waiting time and total turn 42 | // around time 43 | for (int i=0; i 2 | 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); 9 | char number[10]; 10 | int flag = 0; 11 | int length, i = 0; 12 | 13 | printf("\n\nEnter a number: "); 14 | scanf("%s", number); 15 | 16 | length = strlen(number); 17 | 18 | // till string does not end 19 | while(number[i++] != '\0') // same as while(length-->0) 20 | { 21 | if(number[i] == '.') // decimal point is present 22 | { 23 | flag = 1; 24 | break; 25 | } 26 | } 27 | 28 | // if(0) is same as if(false) 29 | if(flag) 30 | printf("\n\n\n\tEntered Number is a Floating point Number\n\n"); 31 | else 32 | printf("\n\n\n\tEntered Number is a integer Number\n\n"); 33 | 34 | printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n"); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Program to check if input character is a vowel using Switch Case_arman105735.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); 6 | 7 | char ch; 8 | printf("Input a Character : "); 9 | scanf("%c", &ch); 10 | 11 | switch(ch) 12 | { 13 | case 'a': 14 | case 'A': 15 | case 'e': 16 | case 'E': 17 | case 'i': 18 | case 'I': 19 | case 'o': 20 | case 'O': 21 | case 'u': 22 | case 'U': 23 | printf("\n\n%c is a vowel.\n\n", ch); 24 | break; 25 | default: 26 | printf("%c is not a vowel.\n\n", ch); 27 | } 28 | printf("\n\n\t\t\tCoding is Fun !\n\n\n"); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Eeveryone Give a STAR on this Repo on the Right corner 2 | -------------------------------------------------------------------------------- /Rafi_Round_Robin_Preemptive.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void main() 5 | { 6 | // initlialize the variable name 7 | int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10]; 8 | float avg_wt, avg_tat; 9 | printf(" Total number of process in the system: "); 10 | scanf("%d", &NOP); 11 | y = NOP; // Assign the number of process to variable y 12 | 13 | // Use for loop to enter the details of the process like Arrival time and the Burst Time 14 | for(i=0; i 0) // define the conditions 31 | { 32 | sum = sum + temp[i]; 33 | temp[i] = 0; 34 | count=1; 35 | } 36 | else if(temp[i] > 0) 37 | { 38 | temp[i] = temp[i] - quant; 39 | sum = sum + quant; 40 | } 41 | if(temp[i]==0 && count==1) 42 | { 43 | y--; //decrement the process no. 44 | printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-at[i]-bt[i]); 45 | wt = wt+sum-at[i]-bt[i]; 46 | tat = tat+sum-at[i]; 47 | count =0; 48 | } 49 | if(i==NOP-1) 50 | { 51 | i=0; 52 | } 53 | else if(at[i+1]<=sum) 54 | { 55 | i++; 56 | } 57 | else 58 | { 59 | i=0; 60 | } 61 | } 62 | // represents the average waiting time and Turn Around time 63 | avg_wt = wt * 1.0/NOP; 64 | avg_tat = tat * 1.0/NOP; 65 | printf("\n Average Turn Around Time: \t%f", avg_wt); 66 | printf("\n Average Waiting Time: \t%f", avg_tat); 67 | getch(); 68 | } 69 | -------------------------------------------------------------------------------- /Rafi_SRTF(Preemptive).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int main() 5 | { 6 | int a[10],b[10],x[10]; 7 | int waiting[10],turnaround[10],completion[10]; 8 | int i,j,smallest,count=0,time,n; 9 | double avg=0,tt=0,end; 10 | 11 | cout<<"\nEnter the number of Processes: "; //input 12 | cin>>n; 13 | for(i=0; i>a[i]; 17 | } 18 | for(i=0; i>b[i]; 22 | } 23 | for(i=0; i0 ) 33 | smallest=i; 34 | } 35 | b[smallest]--; 36 | 37 | if(b[smallest]==0) 38 | { 39 | count++; 40 | end=time+1; 41 | completion[smallest] = end; 42 | waiting[smallest] = end - a[smallest] - x[smallest]; 43 | turnaround[smallest] = end - a[smallest]; 44 | } 45 | } 46 | cout<<"Process"<<"\t"<< "burst-time"<<"\t"<<"arrival-time" <<"\t"<<"waiting-time" <<"\t"<<"turnaround-time"<< "\t"<<"completion-time"<>nop; 149 | cout<<"Enter time quantum\n"; 150 | cin>>qt; 151 | insert(nop); 152 | disp(nop,qt); 153 | return 0; 154 | } 155 | -------------------------------------------------------------------------------- /Round-robin.cpp: -------------------------------------------------------------------------------- 1 | // C++ program for implementation of RR scheduling 2 | #include 3 | 4 | using namespace std; 5 | 6 | // Function to find the waiting time for all 7 | // processes 8 | 9 | void findWaitingTime(int processes[], int n, 10 | 11 | int bt[], int wt[], int quantum) 12 | { 13 | 14 | // Make a copy of burst times bt[] to store remaining 15 | 16 | // burst times. 17 | 18 | int rem_bt[n]; 19 | 20 | for (int i = 0 ; i < n ; i++) 21 | 22 | rem_bt[i] = bt[i]; 23 | 24 | 25 | int t = 0; // Current time 26 | 27 | 28 | // Keep traversing processes in round robin manner 29 | 30 | // until all of them are not done. 31 | 32 | while (1) 33 | 34 | { 35 | 36 | bool done = true; 37 | 38 | 39 | // Traverse all processes one by one repeatedly 40 | 41 | for (int i = 0 ; i < n; i++) 42 | 43 | { 44 | 45 | // If burst time of a process is greater than 0 46 | 47 | // then only need to process further 48 | 49 | if (rem_bt[i] > 0) 50 | 51 | { 52 | 53 | done = false; // There is a pending process 54 | 55 | 56 | if (rem_bt[i] > quantum) 57 | 58 | { 59 | 60 | // Increase the value of t i.e. shows 61 | 62 | // how much time a process has been processed 63 | 64 | t += quantum; 65 | 66 | 67 | // Decrease the burst_time of current process 68 | 69 | // by quantum 70 | 71 | rem_bt[i] -= quantum; 72 | 73 | } 74 | 75 | 76 | // If burst time is smaller than or equal to 77 | 78 | // quantum. Last cycle for this process 79 | 80 | else 81 | 82 | { 83 | 84 | // Increase the value of t i.e. shows 85 | 86 | // how much time a process has been processed 87 | 88 | t = t + rem_bt[i]; 89 | 90 | 91 | // Waiting time is current time minus time 92 | 93 | // used by this process 94 | 95 | wt[i] = t - bt[i]; 96 | 97 | 98 | // As the process gets fully executed 99 | 100 | // make its remaining burst time = 0 101 | 102 | rem_bt[i] = 0; 103 | 104 | } 105 | 106 | } 107 | 108 | } 109 | 110 | 111 | // If all processes are done 112 | 113 | if (done == true) 114 | 115 | break; 116 | 117 | } 118 | } 119 | 120 | // Function to calculate turn around time 121 | 122 | void findTurnAroundTime(int processes[], int n, 123 | 124 | int bt[], int wt[], int tat[]) 125 | { 126 | 127 | // calculating turnaround time by adding 128 | 129 | // bt[i] + wt[i] 130 | 131 | for (int i = 0; i < n ; i++) 132 | 133 | tat[i] = bt[i] + wt[i]; 134 | } 135 | 136 | // Function to calculate average time 137 | 138 | void findavgTime(int processes[], int n, int bt[], 139 | 140 | int quantum) 141 | { 142 | 143 | int wt[n], tat[n], total_wt = 0, total_tat = 0; 144 | 145 | 146 | // Function to find waiting time of all processes 147 | 148 | findWaitingTime(processes, n, bt, wt, quantum); 149 | 150 | 151 | // Function to find turn around time for all processes 152 | 153 | findTurnAroundTime(processes, n, bt, wt, tat); 154 | 155 | 156 | // Display processes along with all details 157 | 158 | cout << "PN\t "<< " \tBT " 159 | 160 | << " WT " << " \tTAT\n"; 161 | 162 | 163 | // Calculate total waiting time and total turn 164 | 165 | // around time 166 | 167 | for (int i=0; i> str; 10 | cout << "You entered: " << str << endl; 11 | 12 | cout << "\nEnter another string: "; 13 | cin >> str; 14 | cout << "You entered: "< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | // Declaring a string object 7 | string str; 8 | cout << "Enter a string: "; 9 | getline(cin, str); 10 | 11 | cout << "You entered: " << str << endl; 12 | return 0; 13 | } 14 | #include 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | // Declaring a string object 20 | string str; 21 | cout << "Enter a string: "; 22 | getline(cin, str); 23 | 24 | cout << "You entered: " << str << endl; 25 | return 0; 26 | } 27 | #include 28 | using namespace std; 29 | 30 | int main() 31 | { 32 | // Declaring a string object 33 | string str; 34 | cout << "Enter a string: "; 35 | getline(cin, str); 36 | 37 | cout << "You entered: " << str << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Sum of array elements.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main(){ 4 | int size; 5 | cin>>size; 6 | int arr[size]; 7 | for(int i=0;i>arr[i]; 9 | } 10 | int sum=0; 11 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | struct Activitiy 6 | { 7 | int start,end; 8 | }; 9 | bool comp(Activitiy arr1, Activitiy arr2) 10 | { 11 | return (arr1.end>size; 25 | cout<>arr[i].start; 31 | cout<<"End Time : "; 32 | cin>>arr[i].end; 33 | cout<= arr[i].end) { 44 | cout << "Activity: " << j << " Start: " < 2 | void swap(int *p1, int *p2) 3 | { 4 | int temp = *p1; 5 | *p1 = *p2; 6 | *p2 = temp; 7 | } 8 | // This is an optimised code for the bubble sort 9 | void bSort(int arrnumbers[], int n) 10 | { 11 | int i, j; 12 | bool check; 13 | for (i = 0; i < n-1; i++) 14 | { 15 | check = false; 16 | for (j = 0; j < n-i-1; j++) 17 | { 18 | if (arrnumbers[j] > arrnumbers[j+1]) 19 | { 20 | swap(&arrnumbers[j], &arrnumbers[j+1]); 21 | check = true; 22 | } 23 | } 24 | // We are breaking from the loop in case two elements were not swapped by inner loop. 25 | if (check == false) 26 | break; 27 | } 28 | } 29 | //This function is to print the array sequence as final output after sorting 30 | void print(int arrnumbers[], int sizeofarray) 31 | { 32 | int i; 33 | for (i=0; i < sizeofarray; i++) 34 | printf("%d ", arrnumbers[i]); 35 | } 36 | // This the main program from where the execution will start 37 | int main() 38 | { 39 | int arrnumbers[] = {5, 6, 1, 0, 2, 9}; 40 | int n = sizeof(arrnumbers)/sizeof(arrnumbers[0]); 41 | bSort(arrnumbers, n); 42 | printf("Sorted array: \n"); 43 | print(arrnumbers, n); 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /almahmudjoy_receives a number as input from user.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int num; 6 | cout<<"Gues a Number: "; 7 | cin>>num; 8 | if(num>10 && num<100) 9 | cout<<"\nWhat a True Guess!"; 10 | else 11 | cout<<"\nOpps!"; 12 | cout< 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | char robotChk[10]; 7 | int val; 8 | cout<<"Are You a Robot ? "; 9 | cin>>robotChk; 10 | val = strcmp("yes", robotChk); 11 | if(val==0) 12 | cout<<"\nYou can't Proceed!"; 13 | else 14 | cout<<"\nYou're Welcome!"; 15 | cout< 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | char pass[20], ePass[20]; 7 | int numOne, numTwo, sum; 8 | cout<<"Create a Password: "; 9 | cin>>pass; 10 | cout<<"\nEnter Two Numbers to Add: "; 11 | cin>>numOne>>numTwo; 12 | cout<<"\nEnter the Password to See the Result: "; 13 | cin>>ePass; 14 | if(!strcmp(pass, ePass)) 15 | { 16 | sum = numOne + numTwo; 17 | cout< 3 | #define R 3 4 | #define C 5 5 | using namespace std; 6 | 7 | bool isvalid(int i, int j) 8 | { 9 | return (i >= 0 && j >= 0 && i < R && j < C); 10 | } 11 | 12 | struct ele { 13 | int x, y; 14 | }; 15 | 16 | bool isdelim(ele temp) 17 | { 18 | return (temp.x == -1 && temp.y == -1); 19 | } 20 | 21 | 22 | bool checkall(int arr[][C]) 23 | { 24 | for (int i=0; i Q; 36 | ele temp; 37 | int ans = 0; 38 | 39 | for (int i=0; i " << ans << endl; 129 | return 0; 130 | } 131 | 132 | -------------------------------------------------------------------------------- /bubblesort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void bubbleSort(int arr[], int n) 5 | { 6 | int i, j; 7 | for (i = 0; i < n - 1; i++) 8 | 9 | for (j = 0; j < n - i - 1; j++) 10 | if (arr[j] > arr[j + 1]) 11 | swap(arr[j], arr[j + 1]); 12 | } 13 | 14 | void printArray(int arr[], int size) 15 | { 16 | int i; 17 | for (i = 0; i < size; i++) 18 | cout << arr[i] << " "; 19 | cout << endl; 20 | } 21 | 22 | int main() 23 | { 24 | int arr[] = { 5, 1, 4, 2, 8, 9, 3}; 25 | int N = sizeof(arr) / sizeof(arr[0]); 26 | bubbleSort(arr, N); 27 | cout << "Sorted array: \n"; 28 | printArray(arr, N); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /bubt_university_contest_E_string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespac std; 3 | 4 | void solve() 5 | { 6 | int n; 7 | string stn; 8 | cin>>stn; 9 | for(int i = 0; i >tc; 24 | while(tc--) 25 | { 26 | solve(); 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /calc.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | using namespace std; 3 | 4 | int main() { 5 | 6 | char op; 7 | float number1, number2; 8 | 9 | cout << "Enter operator: +, -, *, /: "; 10 | cin >> op; 11 | 12 | cout << "Enter two number to be calculated: "; 13 | cin >> number1 >> number2; 14 | 15 | switch(op) { 16 | 17 | case '+': 18 | cout << number1 << " + " << number2 << " = " << number1 + number2; 19 | break; 20 | 21 | case '-': 22 | cout << number1 << " - " << number2 << " = " << number1 - number2; 23 | break; 24 | 25 | case '*': 26 | cout << number1 << " * " << number2 << " = " << number1 * number2; 27 | break; 28 | 29 | case '/': 30 | cout << number1 << " / " << number2 << " = " << num1 / number2; 31 | break; 32 | 33 | default: 34 | 35 | cout << "Error! operator is not correct"; 36 | break; 37 | } 38 | 39 | return 0; 40 | } 41 | //by shimmer12 42 | -------------------------------------------------------------------------------- /calculation.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int x,y,sum,sub,mul,div,mod; 5 | printf("First number:",x); 6 | scanf("%d",&x); 7 | printf("Second number:",y); 8 | scanf("%d",&y); 9 | sum = x + y; 10 | printf("Sum = %d\n",sum); 11 | sub = x - y; 12 | printf("Sub = %d\n",sub); 13 | mul = x * y; 14 | printf("mul = %d\n",mul); 15 | div = x / y; 16 | printf("div = %d\n",div); 17 | mod = x % y; 18 | printf("mod = %d\n",mod); 19 | return 0; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /catalan_number.cpp: -------------------------------------------------------------------------------- 1 | 2 | // C++ program to find the nth Catalan Number 3 | #include 4 | using namespace std; 5 | 6 | // Returns value of Binomial Coefficient C(n, k) 7 | unsigned long int binomialCoeff(unsigned int n, 8 | unsigned int k) 9 | { 10 | unsigned long int res = 1; 11 | 12 | if (k > n - k) 13 | k = n - k; 14 | 15 | for (int i = 0; i < k; ++i) { 16 | res *= (n - i); 17 | res /= (i + 1); 18 | } 19 | 20 | return res; 21 | } 22 | 23 | // A Binomial coefficient based function to find nth catalan 24 | // number in O(n) time 25 | unsigned long int catalan(unsigned int n) 26 | { 27 | unsigned long int c = binomialCoeff(2 * n, n); 28 | 29 | return c / (n + 1); 30 | } 31 | 32 | // Driver code 33 | int main() 34 | { 35 | for (int i = 0; i < 10; i++) 36 | cout << catalan(i) << " "; 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /fcfs.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j; 5 | cout<<"Enter total number of processes(maximum 20):"; 6 | cin>>n; 7 | 8 | cout<<"\nEnter the process Burst Time aka DURATION \n"; 9 | for(i=0;i>bt[i]; 13 | } 14 | wt[0]=0; 15 | 16 | for(i=1;i 2 | using namespace std; 3 | int main() { 4 | int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j; 5 | cout<<"Enter total number of processes(maximum 20):"; 6 | cin>>n; 7 | cout<<"nEnter Process Burst Timen"; 8 | for (i=0;i>bt[i]; 11 | } 12 | wt[0]=0; 13 | //waiting time for first process is 0 14 | //calculating waiting time 15 | for (i=1;i 2 | #include 3 | int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1; 4 | void bfs(int v) { 5 | for (i=1;i<=n;i++) 6 | if(a[v][i] && !visited[i]) 7 | q[++r]=i; 8 | if(f<=r) { 9 | visited[q[f]]=1; 10 | bfs(q[f++]); 11 | } 12 | } 13 | void main() { 14 | int v; 15 | clrscr(); 16 | printf("\n Enter the number of vertices:"); 17 | scanf("%d",&n); 18 | for (i=1;i<=n;i++) { 19 | q[i]=0; 20 | visited[i]=0; 21 | } 22 | printf("\n Enter graph data in matrix form:\n"); 23 | for (i=1;i<=n;i++) 24 | for (j=1;j<=n;j++) 25 | scanf("%d",&a[i][j]); 26 | printf("\n Enter the starting vertex:"); 27 | scanf("%d",&v); 28 | bfs(v); 29 | printf("\n The node which are reachable are:\n"); 30 | for (i=1;i<=n;i++) 31 | if(visited[i]) 32 | printf("%d\t",i); else 33 | printf("\n Bfs is not possible"); 34 | getch(); 35 | } 36 | -------------------------------------------------------------------------------- /fcfs911129.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define MAX 500 5 | int t[MAX]; 6 | void shifttable(char p[]) { 7 | int i,j,m; 8 | m=strlen(p); 9 | for (i=0;i=0) 42 | printf("\n The desired pattern was found starting from position %d",pos+1); else 43 | printf("\n The pattern was not found in the given text\n"); 44 | getch(); 45 | } 46 | -------------------------------------------------------------------------------- /fcfs9121999.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #include 6 | 7 | #include 8 | 9 | #include 10 | 11 | long int p,q,n,t,flag,e[100],d[100],temp[100],j,m[100],en[100],i; 12 | 13 | char msg[100]; 14 | 15 | int prime(long int); 16 | 17 | void ce(); 18 | 19 | long int cd(long int); 20 | 21 | void encrypt(); 22 | 23 | void decrypt(); 24 | 25 | void main() { 26 | 27 | clrscr(); 28 | 29 | printf("\nENTER FIRST PRIME NUMBER\n"); 30 | 31 | scanf("%d",&p); 32 | 33 | flag=prime(p); 34 | 35 | if(flag==0) { 36 | 37 | printf("\nWRONG INPUT\n"); 38 | 39 | getch(); 40 | 41 | exit(1); 42 | 43 | } 44 | 45 | printf("\nENTER ANOTHER PRIME NUMBER\n"); 46 | 47 | scanf("%d",&q); 48 | 49 | flag=prime(q); 50 | 51 | if(flag==0||p==q) { 52 | 53 | printf("\nWRONG INPUT\n"); 54 | 55 | getch(); 56 | 57 | exit(1); 58 | 59 | } 60 | 61 | printf("\nENTER MESSAGE\n"); 62 | 63 | fflush(stdin); 64 | 65 | scanf("%s",msg); 66 | 67 | for (i=0;msg[i]!=NULL;i++) 68 | 69 | m[i]=msg[i]; 70 | 71 | n=p*q; 72 | 73 | t=(p-1)*(q-1); 74 | 75 | ce(); 76 | 77 | printf("\nPOSSIBLE VALUES OF e AND d ARE\n"); 78 | 79 | for (i=0;i0) { 130 | 131 | d[k]=flag; 132 | 133 | k++; 134 | 135 | } 136 | 137 | if(k==99) 138 | 139 | break; 140 | 141 | } 142 | 143 | } 144 | 145 | } 146 | 147 | long int cd(long int x) { 148 | 149 | long int k=1; 150 | 151 | while(1) { 152 | 153 | k=k+t; 154 | 155 | if(k%x==0) 156 | 157 | return(k/x); 158 | 159 | } 160 | 161 | } 162 | 163 | void encrypt() { 164 | 165 | long int pt,ct,key=e[0],k,len; 166 | 167 | i=0; 168 | 169 | len=strlen(msg); 170 | 171 | while(i!=len) { 172 | 173 | pt=m[i]; 174 | 175 | pt=pt-96; 176 | 177 | k=1; 178 | 179 | for (j=0;j 2 | 3 | int main() { 4 | 5 | char *message,*emessage,*dmessage; 6 | 7 | int i,j=0,k,key,temp; 8 | 9 | clrscr(); 10 | 11 | printf("\nEnter the key\n"); 12 | 13 | scanf("%d",&key); 14 | 15 | key=key%26; 16 | 17 | printf("\nEnter message\n"); 18 | 19 | fflush(stdin); 20 | 21 | gets(message); 22 | 23 | for (i=0;message[i]!=NULL;i++) 24 | 25 | message[i]=tolower(message[i]); 26 | 27 | for (i=0;message[i]!=NULL;i++) { 28 | 29 | //printf("%c ",message[i]); 30 | 31 | if(message[i]==' ') 32 | 33 | emessage[j++]=message[i]; else { 34 | 35 | if(message[i]>=48 && message[i]<=57) { 36 | 37 | temp=message[i]+key; 38 | 39 | if(temp>57) 40 | 41 | emessage[j++]=48+(temp-58); else 42 | 43 | emessage[j++]=temp; 44 | 45 | } else { 46 | 47 | if(message[i]>=97 && message[i]<=123) { 48 | 49 | temp=message[i]+key; 50 | 51 | if(temp>122) 52 | 53 | emessage[j++]=97+(temp-123); else 54 | 55 | emessage[j++]=temp; 56 | 57 | } else 58 | 59 | emessage[j++]=message[i]; 60 | 61 | } 62 | 63 | // printf("%c ",emessage[j]); 64 | 65 | } 66 | 67 | } 68 | 69 | emessage[j]='\0'; 70 | 71 | printf("\n\n\nEncrypted message is\n\n"); 72 | 73 | for (i=0;emessage[i]!=NULL;i++) 74 | 75 | printf("%c",emessage[i]); 76 | 77 | // printf("\n end"); 78 | 79 | for (i=0,j=0;emessage[i]!=NULL;i++) { 80 | 81 | if(emessage[i]==' ') 82 | 83 | dmessage[j++]=emessage[i]; else { 84 | 85 | if(emessage[i]>=48 && emessage[i]<=57) { 86 | 87 | temp=emessage[i]-key; 88 | 89 | if(temp<48) 90 | 91 | dmessage[j++]=58-(48-temp); else 92 | 93 | dmessage[j++]=temp; 94 | 95 | } else { 96 | 97 | if(emessage[i]>=97 && emessage[i]<=123) { 98 | 99 | temp=emessage[i]-key; 100 | 101 | if(temp<97) 102 | 103 | dmessage[j++]=123-(97-temp); else 104 | 105 | dmessage[j++]=temp; 106 | 107 | } else 108 | 109 | dmessage[j++]=emessage[i]; 110 | 111 | } 112 | 113 | } 114 | 115 | } 116 | 117 | dmessage[j]='\0'; 118 | 119 | printf("\n\n\nRetrieved message is\n\n"); 120 | 121 | for (i=0;dmessage[i]!=NULL;i++) 122 | 123 | printf("%c",dmessage[i]); 124 | 125 | getch(); 126 | 127 | return(0); 128 | 129 | } 130 | 131 | -------------------------------------------------------------------------------- /fcfs991.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j; 5 | cout<<"Enter total number of processes(maximum 20):"; 6 | cin>>n; 7 | cout<<"nEnter Process Burst Timen"; 8 | for (i=0;i>bt[i]; 11 | } 12 | wt[0]=0; 13 | //waiting time for first process is 0 14 | //calculating waiting time 15 | for (i=1;i 2 | #include 3 | using namespace std; 4 | 5 | class Hash 6 | { 7 | int BUCKET; // No. of buckets 8 | 9 | // Pointer to an array containing buckets 10 | list *table; 11 | public: 12 | Hash(int V); // Constructor 13 | 14 | // inserts a key into hash table 15 | void insertItem(int x); 16 | 17 | // deletes a key from hash table 18 | void deleteItem(int key); 19 | 20 | // hash function to map values to key 21 | int hashFunction(int x) { 22 | return (x % BUCKET); 23 | } 24 | 25 | void displayHash(); 26 | }; 27 | 28 | Hash::Hash(int b) 29 | { 30 | this->BUCKET = b; 31 | table = new list[BUCKET]; 32 | } 33 | 34 | void Hash::insertItem(int key) 35 | { 36 | int index = hashFunction(key); 37 | table[index].push_back(key); 38 | } 39 | 40 | void Hash::deleteItem(int key) 41 | { 42 | // get the hash index of key 43 | int index = hashFunction(key); 44 | 45 | // find the key in (inex)th list 46 | list :: iterator i; 47 | for (i = table[index].begin(); 48 | i != table[index].end(); i++) { 49 | if (*i == key) 50 | break; 51 | } 52 | 53 | // if key is found in hash table, remove it 54 | if (i != table[index].end()) 55 | table[index].erase(i); 56 | } 57 | 58 | // function to display hash table 59 | void Hash::displayHash() { 60 | for (int i = 0; i < BUCKET; i++) { 61 | cout << i; 62 | for (auto x : table[i]) 63 | cout << " --> " << x; 64 | cout << endl; 65 | } 66 | } 67 | 68 | // Driver program 69 | int main() 70 | { 71 | // array that contains keys to be mapped 72 | int a[] = {15, 11, 27, 8, 12}; 73 | int n = sizeof(a)/sizeof(a[0]); 74 | 75 | // insert the keys into the hash table 76 | Hash h(7); // 7 is count of buckets in 77 | // hash table 78 | for (int i = 0; i < n; i++) 79 | h.insertItem(a[i]); 80 | 81 | // delete 12 from hash table 82 | h.deleteItem(12); 83 | 84 | // display the Hash table 85 | h.displayHash(); 86 | 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /fcfs99541.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() { 4 | int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j; 5 | cout<<"Enter total number of processes(maximum 20):"; 6 | cin>>n; 7 | cout<<"nEnter Process Burst Timen"; 8 | for (i=0;i>bt[i]; 11 | } 12 | wt[0]=0; 13 | //waiting time for first process is 0 14 | //calculating waiting time 15 | for (i=1;i 2 | using namespace std; 3 | 4 | int main(int argc, char **argv) 5 | { 6 | int n,n1=0,n2=1,n3,i; 7 | cin >> n; 8 | 9 | for(i=0;i 2 | 3 | int main(){ 4 | FILE *ptr; 5 | int i; 6 | ptr = fopen("q5.txt", "r"); 7 | fscanf(ptr, "%d", &i); 8 | ptr = fopen("q5.txt", "w"); 9 | fprintf(ptr, "%d", i*2); 10 | fclose(ptr); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /index.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int a[]= {1,2,4,5,67,89,100}; 5 | int item = 5; 6 | int left,right,mid; 7 | left = 0; 8 | right = 6; 9 | while(left<=right) 10 | { 11 | mid = (left+right)/2; 12 | if(a[mid]==item) 13 | { 14 | printf("Item found at index:%d\n",mid); 15 | return 0; 16 | } 17 | else if(a[mid] 2 | using namespace std; 3 | void insertionsort(int *a, int n) 4 | { 5 | for (int i = 1; i < n; i++) 6 | { 7 | if (a[i] < a[i - 1]) 8 | { 9 | int k = a[i]; 10 | int l; 11 | for (l = i; l >= 0; l--) 12 | { 13 | if (k < a[l - 1] && l != 0) 14 | { 15 | a[l] = a[l - 1]; 16 | } 17 | else 18 | { 19 | a[l] = k; 20 | break; 21 | } 22 | } 23 | } 24 | } 25 | } 26 | int main() 27 | { 28 | int n; 29 | cout<<"enter no. of elements: "; 30 | cin >> n; 31 | cout<<"Enter the elements: "; 32 | int *a = new int[n]; 33 | for (int i = 0; i < n; i++) 34 | { 35 | cin >> a[i]; 36 | } 37 | cout<<"The result is: "< 2 | int main() 3 | { 4 | int p,t; 5 | float r,SI,add; 6 | scanf("%d %d",&p,&t); 7 | scanf("%f",&r); 8 | SI = (p*t*r)/100; 9 | printf("Simple Interest:%f\n",SI); 10 | add = p + SI; 11 | printf("Principle + Interest = %.1f\n",add); 12 | return 0; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /knapsack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | double a[100][100],m,p,c=0; 6 | int n,i=0,j=0,z,obj_; 7 | double ob[100][100],temp[100][100]; 8 | 9 | cout<<"HOW MANY OBJECTS TO CALCULATE:- "; 10 | cin>>n; 11 | cout<<"\nTOTAL WEIGHT- "; 12 | cin>>m; 13 | 14 | cout<<"PROFIT- "<>a[i][j]; 23 | } 24 | if(c==1) 25 | { 26 | cout<<"WEIGHT- "<0&& ob[i][1]<=m) 61 | { 62 | m=m-ob[i][1]; 63 | p=p+ob[i][2]; 64 | } 65 | else 66 | { 67 | z=i; 68 | break; 69 | } 70 | } 71 | if(m>0) 72 | { 73 | p=p+(ob[z][2]*(m/ob[z][1])); 74 | } 75 | 76 | cout<<"\nGREEDY METHOD PROFIT "< 2 | using namespace std; 3 | 4 | class Edge{ 5 | public: 6 | int source; 7 | int destination; 8 | int weight; 9 | }; 10 | 11 | 12 | Edge input[50]; 13 | 14 | bool sortByWeight(Edge e1,Edge e2){ 15 | return e1.weight < e2.weight; 16 | } 17 | 18 | int findParent(int v,int* parent){ 19 | if(parent[v] == v) 20 | return v; 21 | return findParent(parent[v],parent); 22 | } 23 | 24 | void kruskals(int E,int n){ 25 | // sort input array in asc order based on weights; 26 | sort(input,input+E,sortByWeight); 27 | 28 | Edge *output = new Edge[n-1]; 29 | int *parent = new int[n]; 30 | // parent array containin parent of every edge 31 | for(int i=0;i>n>>E; 63 | 64 | for(int i=0;i>s>>d>>w; 67 | input[i].source = s; 68 | input[i].destination = d; 69 | input[i].weight = w; 70 | } 71 | 72 | kruskals(E,n); 73 | } 74 | -------------------------------------------------------------------------------- /lifo.c: -------------------------------------------------------------------------------- 1 | #include 2 | int stack [ 100 ] , x , top = -1 , n , i ; 3 | void push() 4 | { 5 | if ( top >= n - 1 ) // Checking the overflow condition of the stack // 6 | { 7 | printf ( "Stack overflow! \n " ) ; 8 | } 9 | else 10 | { 11 | printf ( "Enter value to be pushed : " ) ; 12 | scanf ( "%d" , &x ) ; 13 | top = top + 1 ; 14 | stack [ top ] = x ; 15 | } 16 | } 17 | void pop() 18 | { 19 | if ( top == -1 ) 20 | { 21 | printf ( "Stack underflow! \n " ) ; 22 | } 23 | else 24 | { 25 | printf ( "The popped element is %d \n " , stack [ top ] ) ; 26 | top = top - 1 ; 27 | } 28 | } 29 | void traverse() 30 | { 31 | if ( top == -1 ) 32 | { 33 | printf ( "Stack empty!\n " ) ; 34 | } 35 | else 36 | { 37 | for ( i = top ; i >= 0 ; i-- ) 38 | { 39 | printf ( "%d \n " , stack [ i ] ) ; 40 | } 41 | } 42 | } 43 | int menu() 44 | { 45 | int ch ; 46 | printf ( " \t \t \t 1. PUSH OPERATION \n " ) ; 47 | printf ( " \t \t \t 2. POP OPERATION \n " ) ; 48 | printf ( " \t \t \t 3. TRAVERSE OPERATION \n " ) ; 49 | printf ( " \t \t \t 4. EXIT \n " ) ; 50 | printf ( "Enter your choice: " ) ; 51 | scanf ( "%d" , &ch ) ; 52 | return ch ; 53 | } 54 | void main() 55 | { 56 | printf ( "Enter size of stack : " ) ; 57 | scanf ( "%d" , &n ) ; 58 | while ( 1 ) 59 | { 60 | switch ( menu() ) 61 | { 62 | case 1 : 63 | push ( ) ; 64 | break ; 65 | case 2 : 66 | pop ( ) ; 67 | break ; 68 | case 3 : 69 | traverse ( ) ; 70 | break ; 71 | case 4 : 72 | exit( 0 ) ; 73 | break ; 74 | } } 75 | } 76 | -------------------------------------------------------------------------------- /linearSearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | int linear_search(int DATA[],int n,int item) 5 | { 6 | int loc=0; 7 | 8 | for(int k=1;k<=n;k++) 9 | { 10 | if(DATA[k]==item) 11 | { loc=k; 12 | break; 13 | } 14 | } 15 | if(loc==0) 16 | { 17 | cout<<"NOT FOUND"; 18 | } 19 | else 20 | { 21 | cout<<"FOUND DATA["<>n; 31 | cout<<"INPUT: "; 32 | for(int i=1;i<=n;i++) 33 | { 34 | cin>>DATA[i]; 35 | } 36 | cout<<"ITEM :"; 37 | cin>>item; 38 | linear_search(DATA,n,item); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /mehedi0011.c++: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n=0,a=0,r,i; 5 | scanf("%d %d",&n,&a); 6 | for(i=1; i<=a; i++) 7 | { 8 | r=n%10; 9 | if(r==0) 10 | { 11 | n/=10; 12 | } 13 | else 14 | n-=1; 15 | } 16 | printf("%d\n",n); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /mehedihasa740_ArmstrongNumber.java: -------------------------------------------------------------------------------- 1 | class ArmstrongNumberExample{ 2 | public static void main(String[] args) { 3 | int c = 0, a, temp; 4 | int n = 153; //It is the number to check armstrong 5 | temp = n; 6 | while(n > 0) 7 | { 8 | a = n % 10; 9 | n = n / 10; 10 | c = c +(a * a * a); 11 | } 12 | if(temp == c) 13 | System.out.println("armstrong number"); 14 | else 15 | System.out.println("Not armstrong number"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /mehedihasan740_Binary_Search.java: -------------------------------------------------------------------------------- 1 | // Java implementation of recursive 2 | // Binary Search 3 | 4 | class BinarySearch { 5 | 6 | // Function that returns index of 7 | // x if it is present in arr[l, r] 8 | int binarySearch(int arr[], int l, 9 | int r, int x) 10 | { 11 | if (r >= l) { 12 | int mid = l + (r - l) / 2; 13 | 14 | // If the element is present 15 | // at the middle itself 16 | if (arr[mid] == x) 17 | return mid; 18 | 19 | // If element is smaller than 20 | // mid, then it can only be 21 | // present in left subarray 22 | if (arr[mid] > x) 23 | return binarySearch(arr, l, 24 | mid - 1, x); 25 | 26 | // Else the element can only be 27 | // present in right subarray 28 | return binarySearch(arr, mid + 1, 29 | r, x); 30 | } 31 | 32 | // Reach here when element is 33 | // not present in array 34 | return -1; 35 | } 36 | 37 | // Driver Code 38 | public static void main(String args[]) 39 | { 40 | 41 | // Create object of this class 42 | BinarySearch ob = new BinarySearch(); 43 | 44 | // Given array arr[] 45 | int arr[] = { 2, 3, 4, 10, 40 }; 46 | int n = arr.length; 47 | int x = 10; 48 | 49 | // Function Call 50 | int result = ob.binarySearch(arr, 0, 51 | n - 1, x); 52 | 53 | if (result == -1) 54 | System.out.println("Element " 55 | + "not present"); 56 | else 57 | System.out.println("Element found" 58 | + " at index " 59 | + result); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /mehedihasan740_Heap_Sort: -------------------------------------------------------------------------------- 1 | class Sort { 2 | public void heapSort(int arr[]) 3 | { 4 | int temp; 5 | 6 | for (int i = arr.length / 2 - 1; i >= 0; i--) //build the heap 7 | { 8 | heapify(arr, arr.length, i); 9 | } 10 | 11 | for (int i = arr.length - 1; i > 0; i--) //extract elements from the heap 12 | { 13 | temp = arr[0]; //move current root to end (since it is the largest) 14 | arr[0] = arr[i]; 15 | arr[i] = temp; 16 | heapify(arr, i, 0); //recall heapify to rebuild heap for the remaining elements 17 | } 18 | } 19 | 20 | void heapify(int arr[], int n, int i) 21 | { 22 | int MAX = i; // Initialize largest as root 23 | int left = 2 * i + 1; //index of the left child of ith node = 2*i + 1 24 | int right = 2 * i + 2; //index of the right child of ith node = 2*i + 2 25 | int temp; 26 | 27 | if (left < n && arr[left] > arr[MAX]) //check if the left child of the root is larger than the root 28 | { 29 | MAX = left; 30 | } 31 | 32 | if (right < n && arr[right] > arr[MAX]) //check if the right child of the root is larger than the root 33 | { 34 | MAX = right; 35 | } 36 | 37 | if (MAX != i) 38 | { //repeat the procedure for finding the largest element in the heap 39 | temp = arr[i]; 40 | arr[i] = arr[MAX]; 41 | arr[MAX] = temp; 42 | heapify(arr, n, MAX); 43 | } 44 | } 45 | 46 | void display(int arr[]) //display the array 47 | { 48 | for (int i=0; i numbers = new ArrayList<>(); 8 | numbers.add(1); 9 | numbers.add(2); 10 | System.out.println("ArrayList1: " + numbers); 11 | 12 | // Using reverse() 13 | Collections.reverse(numbers); 14 | System.out.println("Reversed ArrayList1: " + numbers); 15 | 16 | // Using swap() 17 | Collections.swap(numbers, 0, 1); 18 | System.out.println("ArrayList1 using swap(): " + numbers); 19 | 20 | ArrayList newNumbers = new ArrayList<>(); 21 | 22 | // Using addAll 23 | newNumbers.addAll(numbers); 24 | System.out.println("ArrayList2 using addAll(): " + newNumbers); 25 | 26 | // Using fill() 27 | Collections.fill(numbers, 0); 28 | System.out.println("ArrayList1 using fill(): " + numbers); 29 | 30 | // Using copy() 31 | Collections.copy(newNumbers, numbers); 32 | System.out.println("ArrayList2 using copy(): " + newNumbers); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /merge.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | 6 | void merge(int arr[],int low,int high,int mid) 7 | { 8 | int i=low; 9 | int k=low; 10 | int j=mid+1; 11 | int temp[1000]; 12 | 13 | while(i<=mid && j<=high) 14 | { 15 | if(arr[i]>num; 74 | cout<<"INPUT: "; 75 | for (int i = 0; i < num; i++) 76 | { 77 | cin>>myarray[i]; 78 | } 79 | merge_sort(myarray, 0, num-1); 80 | cout<<"Sorted array: "; 81 | for (int i = 0; i < num; i++) 82 | { 83 | cout< 2 | using namespace std; 3 | 4 | void merge(int arr[], int p, int q, int r) { 5 | 6 | int n1 = q - p + 1; 7 | int n2 = r - q; 8 | 9 | int L[n1], M[n2]; 10 | 11 | for (int i = 0; i < n1; i++) 12 | L[i] = arr[p + i]; 13 | for (int j = 0; j < n2; j++) 14 | M[j] = arr[q + 1 + j]; 15 | int i, j, k; 16 | i = 0; 17 | j = 0; 18 | k = p; 19 | while (i < n1 && j < n2) { 20 | if (L[i] <= M[j]) { 21 | arr[k] = L[i]; 22 | i++; 23 | } else { 24 | arr[k] = M[j]; 25 | j++; 26 | } 27 | k++; 28 | } 29 | while (i < n1) { 30 | arr[k] = L[i]; 31 | i++; 32 | k++; 33 | } 34 | 35 | while (j < n2) { 36 | arr[k] = M[j]; 37 | j++; 38 | k++; 39 | } 40 | } 41 | 42 | void mergeSort(int arr[], int l, int r) { 43 | if (l < r) { 44 | int m = l + (r - l) / 2; 45 | 46 | mergeSort(arr, l, m); 47 | mergeSort(arr, m + 1, r); 48 | 49 | merge(arr, l, m, r); 50 | } 51 | } 52 | 53 | void printArray(int arr[], int size) { 54 | for (int i = 0; i < size; i++) 55 | cout << arr[i] << " "; 56 | cout << endl; 57 | } 58 | 59 | int main() { 60 | int arr[] = {6, 5, 12, 10, 9, 1, 2}; 61 | int size = sizeof(arr) / sizeof(arr[0]); 62 | 63 | mergeSort(arr, 0, size - 1); 64 | 65 | cout << "Sorted array: \n"; 66 | printArray(arr, size); 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /msakib1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | for(int i = 0; i <= 9; i++){ 6 | for(int j = 0; j <= i; j++){ 7 | cout<<"*"; 8 | } 9 | cout< 2 | using namespace std; 3 | #define ll long long 4 | const int base = 1000000000; 5 | const int base_digits = 9; 6 | 7 | struct bigint 8 | { 9 | vector a; 10 | int sign; 11 | 12 | bigint() : sign(1) 13 | { 14 | } 15 | 16 | bigint(long long v) 17 | { 18 | *this = v; 19 | } 20 | bigint(const string &s) 21 | { 22 | read(s); 23 | } 24 | 25 | void operator=(const bigint &v) 26 | { 27 | sign = v.sign; 28 | a = v.a; 29 | } 30 | 31 | void operator=(long long v) 32 | { 33 | sign = 1; 34 | if (v < 0) 35 | sign = -1, v = -v; 36 | for (; v > 0; v = v / base) 37 | a.push_back(v % base); 38 | } 39 | 40 | bigint operator+(const bigint &v) const 41 | { 42 | if (sign == v.sign) 43 | { 44 | bigint res = v; 45 | 46 | for (int i = 0, carry = 0; i < (int)max(a.size(), v.a.size()) || carry; ++i) 47 | { 48 | if (i == (int)res.a.size()) 49 | res.a.push_back(0); 50 | res.a[i] += carry + (i < (int)a.size() ? a[i] : 0); 51 | carry = res.a[i] >= base; 52 | if (carry) 53 | res.a[i] -= base; 54 | } 55 | return res; 56 | } 57 | return *this - (-v); 58 | } 59 | 60 | bigint operator-(const bigint &v) const 61 | { 62 | if (sign == v.sign) 63 | { 64 | if (abs() >= v.abs()) 65 | { 66 | bigint res = *this; 67 | for (int i = 0, carry = 0; i < (int)v.a.size() || carry; ++i) 68 | { 69 | res.a[i] -= carry + (i < (int)v.a.size() ? v.a[i] : 0); 70 | carry = res.a[i] < 0; 71 | if (carry) 72 | res.a[i] += base; 73 | } 74 | res.trim(); 75 | return res; 76 | } 77 | return -(v - *this); 78 | } 79 | return *this + (-v); 80 | } 81 | 82 | void operator*=(int v) 83 | { 84 | if (v < 0) 85 | sign = -sign, v = -v; 86 | for (int i = 0, carry = 0; i < (int)a.size() || carry; ++i) 87 | { 88 | if (i == (int)a.size()) 89 | a.push_back(0); 90 | long long cur = a[i] * (long long)v + carry; 91 | carry = (int)(cur / base); 92 | a[i] = (int)(cur % base); 93 | //asm("divl %%ecx" : "=a"(carry), "=d"(a[i]) : "A"(cur), "c"(base)); 94 | } 95 | trim(); 96 | } 97 | 98 | bigint operator*(int v) const 99 | { 100 | bigint res = *this; 101 | res *= v; 102 | return res; 103 | } 104 | 105 | friend pair divmod(const bigint &a1, const bigint &b1) 106 | { 107 | int norm = base / (b1.a.back() + 1); 108 | bigint a = a1.abs() * norm; 109 | bigint b = b1.abs() * norm; 110 | bigint q, r; 111 | q.a.resize(a.a.size()); 112 | 113 | for (int i = a.a.size() - 1; i >= 0; i--) 114 | { 115 | r *= base; 116 | r += a.a[i]; 117 | int s1 = r.a.size() <= b.a.size() ? 0 : r.a[b.a.size()]; 118 | int s2 = r.a.size() <= b.a.size() - 1 ? 0 : r.a[b.a.size() - 1]; 119 | int d = ((long long)base * s1 + s2) / b.a.back(); 120 | r -= b * d; 121 | while (r < 0) 122 | r += b, --d; 123 | q.a[i] = d; 124 | } 125 | 126 | q.sign = a1.sign * b1.sign; 127 | r.sign = a1.sign; 128 | q.trim(); 129 | r.trim(); 130 | return make_pair(q, r / norm); 131 | } 132 | 133 | bigint operator/(const bigint &v) const 134 | { 135 | return divmod(*this, v).first; 136 | } 137 | 138 | bigint operator%(const bigint &v) const 139 | { 140 | return divmod(*this, v).second; 141 | } 142 | 143 | void operator/=(int v) 144 | { 145 | if (v < 0) 146 | sign = -sign, v = -v; 147 | for (int i = (int)a.size() - 1, rem = 0; i >= 0; --i) 148 | { 149 | long long cur = a[i] + rem * (long long)base; 150 | a[i] = (int)(cur / v); 151 | rem = (int)(cur % v); 152 | } 153 | trim(); 154 | } 155 | 156 | bigint operator/(int v) const 157 | { 158 | bigint res = *this; 159 | res /= v; 160 | return res; 161 | } 162 | 163 | int operator%(int v) const 164 | { 165 | if (v < 0) 166 | v = -v; 167 | int m = 0; 168 | for (int i = a.size() - 1; i >= 0; --i) 169 | m = (a[i] + m * (long long)base) % v; 170 | return m * sign; 171 | } 172 | 173 | void operator+=(const bigint &v) 174 | { 175 | *this = *this + v; 176 | } 177 | void operator-=(const bigint &v) 178 | { 179 | *this = *this - v; 180 | } 181 | void operator*=(const bigint &v) 182 | { 183 | *this = *this * v; 184 | } 185 | void operator/=(const bigint &v) 186 | { 187 | *this = *this / v; 188 | } 189 | 190 | bool operator<(const bigint &v) const 191 | { 192 | if (sign != v.sign) 193 | return sign < v.sign; 194 | if (a.size() != v.a.size()) 195 | return a.size() * sign < v.a.size() * v.sign; 196 | for (int i = a.size() - 1; i >= 0; i--) 197 | if (a[i] != v.a[i]) 198 | return a[i] * sign < v.a[i] * sign; 199 | return false; 200 | } 201 | 202 | bool operator>(const bigint &v) const 203 | { 204 | return v < *this; 205 | } 206 | bool operator<=(const bigint &v) const 207 | { 208 | return !(v < *this); 209 | } 210 | bool operator>=(const bigint &v) const 211 | { 212 | return !(*this < v); 213 | } 214 | bool operator==(const bigint &v) const 215 | { 216 | return !(*this < v) && !(v < *this); 217 | } 218 | bool operator!=(const bigint &v) const 219 | { 220 | return *this < v || v < *this; 221 | } 222 | 223 | void trim() 224 | { 225 | while (!a.empty() && !a.back()) 226 | a.pop_back(); 227 | if (a.empty()) 228 | sign = 1; 229 | } 230 | 231 | bool isZero() const 232 | { 233 | return a.empty() || (a.size() == 1 && !a[0]); 234 | } 235 | 236 | bigint operator-() const 237 | { 238 | bigint res = *this; 239 | res.sign = -sign; 240 | return res; 241 | } 242 | 243 | bigint abs() const 244 | { 245 | bigint res = *this; 246 | res.sign *= res.sign; 247 | return res; 248 | } 249 | 250 | long long longValue() const 251 | { 252 | long long res = 0; 253 | for (int i = a.size() - 1; i >= 0; i--) 254 | res = res * base + a[i]; 255 | return res * sign; 256 | } 257 | 258 | friend bigint gcd(const bigint &a, const bigint &b) 259 | { 260 | return b.isZero() ? a : gcd(b, a % b); 261 | } 262 | friend bigint lcm(const bigint &a, const bigint &b) 263 | { 264 | return a / gcd(a, b) * b; 265 | } 266 | 267 | void read(const string &s) 268 | { 269 | sign = 1; 270 | a.clear(); 271 | int pos = 0; 272 | while (pos < (int)s.size() && (s[pos] == '-' || s[pos] == '+')) 273 | { 274 | if (s[pos] == '-') 275 | sign = -sign; 276 | ++pos; 277 | } 278 | for (int i = s.size() - 1; i >= pos; i -= base_digits) 279 | { 280 | int x = 0; 281 | for (int j = max(pos, i - base_digits + 1); j <= i; j++) 282 | x = x * 10 + s[j] - '0'; 283 | a.push_back(x); 284 | } 285 | trim(); 286 | } 287 | 288 | friend istream &operator>>(istream &stream, bigint &v) 289 | { 290 | string s; 291 | stream >> s; 292 | v.read(s); 293 | return stream; 294 | } 295 | 296 | friend ostream &operator<<(ostream &stream, const bigint &v) 297 | { 298 | if (v.sign == -1) 299 | stream << '-'; 300 | stream << (v.a.empty() ? 0 : v.a.back()); 301 | for (int i = (int)v.a.size() - 2; i >= 0; --i) 302 | stream << setw(base_digits) << setfill('0') << v.a[i]; 303 | return stream; 304 | } 305 | 306 | static vector convert_base(const vector &a, int old_digits, int new_digits) 307 | { 308 | vector p(max(old_digits, new_digits) + 1); 309 | p[0] = 1; 310 | for (int i = 1; i < (int)p.size(); i++) 311 | p[i] = p[i - 1] * 10; 312 | vector res; 313 | long long cur = 0; 314 | int cur_digits = 0; 315 | for (int i = 0; i < (int)a.size(); i++) 316 | { 317 | cur += a[i] * p[cur_digits]; 318 | cur_digits += old_digits; 319 | while (cur_digits >= new_digits) 320 | { 321 | res.push_back(int(cur % p[new_digits])); 322 | cur /= p[new_digits]; 323 | cur_digits -= new_digits; 324 | } 325 | } 326 | res.push_back((int)cur); 327 | while (!res.empty() && !res.back()) 328 | res.pop_back(); 329 | return res; 330 | } 331 | 332 | typedef vector vll; 333 | 334 | static vll karatsubaMultiply(const vll &a, const vll &b) 335 | { 336 | int n = a.size(); 337 | vll res(n + n); 338 | if (n <= 32) 339 | { 340 | for (int i = 0; i < n; i++) 341 | for (int j = 0; j < n; j++) 342 | res[i + j] += a[i] * b[j]; 343 | return res; 344 | } 345 | 346 | int k = n >> 1; 347 | vll a1(a.begin(), a.begin() + k); 348 | vll a2(a.begin() + k, a.end()); 349 | vll b1(b.begin(), b.begin() + k); 350 | vll b2(b.begin() + k, b.end()); 351 | 352 | vll a1b1 = karatsubaMultiply(a1, b1); 353 | vll a2b2 = karatsubaMultiply(a2, b2); 354 | 355 | for (int i = 0; i < k; i++) 356 | a2[i] += a1[i]; 357 | for (int i = 0; i < k; i++) 358 | b2[i] += b1[i]; 359 | 360 | vll r = karatsubaMultiply(a2, b2); 361 | for (int i = 0; i < (int)a1b1.size(); i++) 362 | r[i] -= a1b1[i]; 363 | for (int i = 0; i < (int)a2b2.size(); i++) 364 | r[i] -= a2b2[i]; 365 | 366 | for (int i = 0; i < (int)r.size(); i++) 367 | res[i + k] += r[i]; 368 | for (int i = 0; i < (int)a1b1.size(); i++) 369 | res[i] += a1b1[i]; 370 | for (int i = 0; i < (int)a2b2.size(); i++) 371 | res[i + n] += a2b2[i]; 372 | return res; 373 | } 374 | 375 | bigint operator*(const bigint &v) const 376 | { 377 | vector a6 = convert_base(this->a, base_digits, 6); 378 | vector b6 = convert_base(v.a, base_digits, 6); 379 | vll a(a6.begin(), a6.end()); 380 | vll b(b6.begin(), b6.end()); 381 | while (a.size() < b.size()) 382 | a.push_back(0); 383 | while (b.size() < a.size()) 384 | b.push_back(0); 385 | while (a.size() & (a.size() - 1)) 386 | a.push_back(0), b.push_back(0); 387 | vll c = karatsubaMultiply(a, b); 388 | bigint res; 389 | res.sign = sign * v.sign; 390 | for (int i = 0, carry = 0; i < (int)c.size(); i++) 391 | { 392 | long long cur = c[i] + carry; 393 | res.a.push_back((int)(cur % 1000000)); 394 | carry = (int)(cur / 1000000); 395 | } 396 | res.a = convert_base(res.a, 6, base_digits); 397 | res.trim(); 398 | return res; 399 | } 400 | }; 401 | int main() 402 | { 403 | ios_base::sync_with_stdio(false); 404 | cin.tie(0); 405 | cout.tie(0); 406 | 407 | } 408 | -------------------------------------------------------------------------------- /msakib_01_matrix.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define R 5 4 | #define C 8 5 | 6 | int maxHits(int row[]) 7 | { 8 | stackreslt; 9 | int top_val; 10 | int mx_area = 0; 11 | int area = 0; 12 | int i = 0; 13 | 14 | while(i 2 | 3 | int main() 4 | { 5 | int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat; 6 | printf("Enter Total Number of Process:"); 7 | scanf("%d",&n); 8 | 9 | printf("\nEnter Burst Time and Priority\n"); 10 | for(i=0;inext) { 26 | slow = slow->next; 27 | fast = fast->next->next; 28 | } 29 | 30 | if (fast) 31 | slow = slow->next; 32 | slow = reverseList(slow); 33 | 34 | while (slow) { 35 | if (slow->val != head->val) 36 | return false; 37 | slow = slow->next; 38 | head = head->next; 39 | } 40 | 41 | return true; 42 | } 43 | 44 | private: 45 | ListNode* reverseList(ListNode* head) { 46 | ListNode* prev = nullptr; 47 | 48 | while (head) { 49 | ListNode* next = head->next; 50 | head->next = prev; 51 | prev = head; 52 | head = next; 53 | } 54 | 55 | return prev; 56 | } 57 | }; 58 | -------------------------------------------------------------------------------- /pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | void gS(unsigned long *par); //function declaration 6 | 7 | int main () { 8 | 9 | unsigned long sec; 10 | 11 | gS( &sec ); 12 | 13 | // print the actual value 14 | 15 | cout << "Number of seconds :" << sec << endl; 16 | 17 | return 0; 18 | 19 | } 20 | 21 | void gS(unsigned long *par) { //function definition 22 | 23 | // get the current number of seconds 24 | 25 | *par = time( NULL ); 26 | 27 | } 28 | -------------------------------------------------------------------------------- /primeNumber.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int n = 925, isPrime=1; 5 | for (int i = 2; i < n; i++) 6 | { 7 | if(n%i == 0){ 8 | isPrime = 0; 9 | break; 10 | } 11 | } 12 | if(isPrime){ 13 | printf("The number is prime\n"); 14 | } 15 | else{ 16 | printf("The number is not prime\n"); 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /program of SJF(Shortest job first ) Scheduling algorithm (Non -preemptive).c: -------------------------------------------------------------------------------- 1 | #include 2 | struct process 3 | { 4 | int id,WT,AT,BT,TAT; 5 | }; 6 | struct process a[10]; 7 | 8 | // function for swapping 9 | void swap(int *b,int *c) 10 | { 11 | int tem; 12 | tem=*c; 13 | *c=*b; 14 | *b=tem; 15 | } 16 | 17 | //Driver function 18 | int main() 19 | { 20 | int n,check_ar=0; 21 | int Cmp_time=0; 22 | float Total_WT=0,Total_TAT=0,Avg_WT,Avg_TAT; 23 | printf("Enter the number of process \n"); 24 | scanf("%d",&n); 25 | printf("Enter the Arrival time and Burst time of the process\n"); 26 | printf("AT BT\n"); 27 | for(int i=0;ia[j+1].AT) 49 | { 50 | swap(&a[j].id,&a[j+1].id); 51 | swap(&a[j].AT,&a[j+1].AT); 52 | swap(&a[j].BT,&a[j+1].BT); 53 | } 54 | } 55 | } 56 | } 57 | 58 | // logic of SJF non preemptive algo 59 | // if all the process are arrived at different time 60 | if(check_ar!=0) 61 | { 62 | a[0].WT=a[0].AT; 63 | a[0].TAT=a[0].BT-a[0].AT; 64 | Cmp_time=a[0].TAT; 65 | Total_WT=Total_WT+a[0].WT; 66 | Total_TAT=Total_TAT+a[0].TAT; 67 | for(int i=1;ia[j].BT && a[j].AT<=Cmp_time) 73 | { 74 | min=a[j].BT; 75 | swap(&a[i].id,&a[j].id); 76 | swap(&a[i].AT,&a[j].AT); 77 | swap(&a[i].BT,&a[j].BT); 78 | } 79 | 80 | } 81 | a[i].WT=Cmp_time-a[i].AT; 82 | Total_WT=Total_WT+a[i].WT; 83 | // completion time of the process 84 | Cmp_time=Cmp_time+a[i].BT; 85 | 86 | // Turn Around Time of the process 87 | // compl-Arival 88 | a[i].TAT=Cmp_time-a[i].AT; 89 | Total_TAT=Total_TAT+a[i].TAT; 90 | 91 | } 92 | } 93 | 94 | // if all the process are arrived at same time 95 | else 96 | { 97 | for(int i=0;ia[j].BT && a[j].AT<=Cmp_time) 103 | { 104 | min=a[j].BT; 105 | swap(&a[i].id,&a[j].id); 106 | swap(&a[i].AT,&a[j].AT); 107 | swap(&a[i].BT,&a[j].BT); 108 | } 109 | 110 | } 111 | a[i].WT=Cmp_time-a[i].AT; 112 | 113 | // completion time of the process 114 | Cmp_time=Cmp_time+a[i].BT; 115 | 116 | // Turn Around Time of the process 117 | // compl-Arrival 118 | a[i].TAT=Cmp_time-a[i].AT; 119 | Total_WT=Total_WT+a[i].WT; 120 | Total_TAT=Total_TAT+a[i].TAT; 121 | 122 | } 123 | 124 | } 125 | 126 | Avg_WT=Total_WT/n; 127 | Avg_TAT=Total_TAT/n; 128 | 129 | // Printing of the results 130 | printf("The process are\n"); 131 | printf("ID WT TAT\n"); 132 | for(int i=0;i 2 | 3 | using namespace std; 4 | int partition (int a[], int start, int end) 5 | { 6 | int pivot = a[end]; 7 | int i = (start - 1); 8 | 9 | for (int j = start; j <= end - 1; j++) 10 | { 11 | if (a[j] < pivot) 12 | { 13 | i++; 14 | int t = a[i]; 15 | a[i] = a[j]; 16 | a[j] = t; 17 | } 18 | } 19 | int t = a[i+1]; 20 | a[i+1] = a[end]; 21 | a[end] = t; 22 | return (i + 1); 23 | } 24 | 25 | void quick(int a[], int start, int end) 26 | { 27 | if (start < end) 28 | { 29 | int p = partition(a, start, end); 30 | quick(a, start, p - 1); 31 | quick(a, p + 1, end); 32 | } 33 | } 34 | 35 | void printArr(int a[], int n) 36 | { 37 | int i; 38 | for (i = 0; i < n; i++) 39 | cout< 2 | #include 3 | using namespace std; 4 | FILE *tp1,*tp2; 5 | int main () 6 | { 7 | char c; 8 | ofstream myfile; 9 | myfile.open ("result2.txt"); 10 | tp1=fopen("input1.txt","r"); 11 | char input[1000]; 12 | int i=0; 13 | while((c=fgetc(tp1))!=EOF) 14 | { 15 | input[i]=c; 16 | i++; 17 | } 18 | fclose(tp1); 19 | char* token; 20 | token=strtok(input,",.*!'"); 21 | while(token!=NULL) 22 | { 23 | myfile < 2 | #include 3 | using namespace std; 4 | FILE *tp,*tp1; 5 | int main () 6 | { 7 | char c; 8 | ofstream myfile; 9 | myfile.open ("result3.txt"); 10 | tp1=fopen("input2.txt","r"); 11 | char a[1000]; 12 | int i=0; 13 | while((c=fgetc(tp1))!=EOF) 14 | { 15 | a[i]=c; 16 | i++; 17 | }fclose(tp1); 18 | string s; 19 | int count1=0,count2=0; 20 | for(int i=0; i<100; i++) 21 | { 22 | if(a[i]=='/'&&a[i+1]=='/'&&a[i+3]!='*' 23 | ) 24 | { 25 | count1++; 26 | myfile << "Single line comment\n"; 27 | } 28 | if(a[i]=='/'&&a[i+1]=='*') 29 | { 30 | count2++; 31 | myfile<<"Multi line comment\n"; 32 | } } 33 | myfile<<"Total Single line comment is: "< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j; 8 | cout<<"Enter total number of processes(maximum 20):"; 9 | cin>>n; 10 | 11 | cout<<"\nEnter Process Burst Time\n"; 12 | for(i=0;i>bt[i]; 16 | } 17 | 18 | wt[0]=0; //waiting time for first process is 0 19 | 20 | //calculating waiting time 21 | for(i=1;i 2 | using namespace std; 3 | // Function to find the waiting time for all 4 | // processes 5 | void findWaitingTime(int processes[], int n, 6 | int bt[], int wt[], int quantum) 7 | { 8 | // Make a copy of burst times bt[] to store remaining 9 | // burst times. 10 | int rem_bt[n]; 11 | for (int i = 0 ; i < n ; i++) 12 | rem_bt[i] = bt[i]; 13 | int t = 0; // Current time 14 | // Keep traversing processes in round robin manner 15 | // until all of them are not done. 16 | while (1) 17 | { 18 | bool done = true; 19 | // Traverse all processes one by one repeatedly 20 | for (int i = 0 ; i < n; i++) 21 | { 22 | // If burst time of a process is greater than 0 23 | // then only need to process further 24 | if (rem_bt[i] > 0) 25 | { 26 | done = false; // There is a pending process 27 | if (rem_bt[i] > quantum) 28 | { 29 | // Increase the value of t i.e. shows 30 | // how much time a process has been processed 31 | t += quantum; 32 | // Decrease the burst_time of current process 33 | // by quantum 34 | rem_bt[i] -= quantum; 35 | } 36 | // If burst time is smaller than or equal to 37 | // quantum. Last cycle for this process 38 | else 39 | { 40 | // Increase the value of t i.e. shows 41 | // how much time a process has been processed 42 | t = t + rem_bt[i]; 43 | // Waiting time is current time minus time 44 | // used by this process 45 | wt[i] = t - bt[i]; 46 | // As the process gets fully executed 47 | // make its remaining burst time = 0 48 | rem_bt[i] = 0; 49 | } 50 | } 51 | } 52 | // If all processes are done 53 | if (done == true) 54 | break; 55 | } 56 | } 57 | // Function to calculate turn around time 58 | void findTurnAroundTime(int processes[], int n, 59 | int bt[], int wt[], int tat[]) 60 | { 61 | // calculating turnaround time by adding 62 | // bt[i] + wt[i] 63 | for (int i = 0; i < n ; i++) 64 | tat[i] = bt[i] + wt[i]; 65 | } 66 | // Function to calculate average time 67 | void findavgTime(int processes[], int n, int bt[], 68 | int quantum) 69 | { 70 | int wt[n], tat[n], total_wt = 0, total_tat = 0; 71 | // Function to find waiting time of all processes 72 | findWaitingTime(processes, n, bt, wt, quantum); 73 | // Function to find turn around time for all processes 74 | findTurnAroundTime(processes, n, bt, wt, tat); 75 | // Display processes along with all details 76 | cout << "Processes "<< " Burst time " 77 | << " Waiting time " << " Turn around time\n"; 78 | // Calculate total waiting time and total turn 79 | // around time 80 | for (int i=0; i 0) 32 | { 33 | total = total + temp[i]; 34 | temp[i] = 0; 35 | counter = 1; 36 | } 37 | else if(temp[i] > 0) 38 | { 39 | temp[i] = temp[i] - time_quantum; 40 | total = total + time_quantum; 41 | } 42 | if(temp[i] == 0 && counter == 1) 43 | { 44 | x--; 45 | printf("nProcess[%d]tt%dtt %dttt %d", i + 1, burst_time[i], total - arrival_time[i], total - arrival_time[i] - burst_time[i]); 46 | wait_time = wait_time + total - arrival_time[i] - burst_time[i]; 47 | turnaround_time = turnaround_time + total - arrival_time[i]; 48 | counter = 0; 49 | } 50 | if(i == limit - 1) 51 | { 52 | i = 0; 53 | } 54 | else if(arrival_time[i + 1] <= total) 55 | { 56 | i++; 57 | } 58 | else 59 | { 60 | i = 0; 61 | } 62 | } 63 | 64 | average_wait_time = wait_time * 1.0 / limit; 65 | average_turnaround_time = turnaround_time * 1.0 / limit; 66 | printf("nnAverage Waiting Time:t%f", average_wait_time); 67 | printf("nAvg Turnaround Time:t%fn", average_turnaround_time); 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /reverse_list.cpp: -------------------------------------------------------------------------------- 1 | // Reverse Linked List 2 | /** 3 | Given the head of a singly linked list, reverse the list, and return the reversed list. 4 | */ 5 | 6 | class Solution { 7 | public: 8 | ListNode* reverseList(ListNode* head) { 9 | ListNode* cur = NULL; 10 | while (head) { 11 | ListNode* next = head -> next; 12 | head -> next = cur; 13 | cur = head; 14 | head = next; 15 | } 16 | return cur; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /sieve_of_eratosthenes.cpp: -------------------------------------------------------------------------------- 1 | / C++ program to print all primes 2 | // smaller than or equal to 3 | // n using Sieve of Eratosthenes 4 | #include 5 | using namespace std; 6 | 7 | void SieveOfEratosthenes(int n) 8 | { 9 | 10 | bool prime[n + 1]; 11 | memset(prime, true, sizeof(prime)); 12 | 13 | for (int p = 2; p * p <= n; p++) 14 | { 15 | // If prime[p] is not changed, 16 | // then it is a prime 17 | if (prime[p] == true) 18 | { 19 | for (int i = p * p; i <= n; i += p) 20 | prime[i] = false; 21 | } 22 | } 23 | 24 | for (int p = 2; p <= n; p++) 25 | if (prime[p]) 26 | cout << p << " "; 27 | } 28 | 29 | // Driver Code 30 | int main() 31 | { 32 | int n = 30; 33 | cout << "Following are the prime numbers smaller " 34 | << " than or equal to " << n << endl; 35 | SieveOfEratosthenes(n); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /simplecalculator.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | main() 6 | { 7 | char op; 8 | float num1, num2; 9 | 10 | // It allows user to enter operator i.e. +, -, *, / 11 | cin >> op; 12 | 13 | // It allow user to enter the operands 14 | cin >> num1 >> num2; 15 | 16 | // Switch statement begins 17 | switch (op) { 18 | 19 | // If user enter + 20 | case '+': 21 | cout << num1 + num2; 22 | break; 23 | 24 | // If user enter - 25 | case '-': 26 | cout << num1 - num2; 27 | break; 28 | 29 | // If user enter * 30 | case '*': 31 | cout << num1 * num2; 32 | break; 33 | 34 | // If user enter / 35 | case '/': 36 | cout << num1 / num2; 37 | break; 38 | 39 | // If the operator is other than +, -, * or /, 40 | // error message will display 41 | default: 42 | cout << "Error! operator is not correct"; 43 | break; 44 | } // switch statement ends 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /sliding_window_max_of_all_subarrays.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | //Main Function Block 7 | void printKMax(int arr[], int n, int k) 8 | { 9 | deque dq(k); 10 | for (int i = 0; i < k; ++i) 11 | { 12 | while ((!dq.empty()) && arr[i] >= arr[dq.back()]) 13 | dq.pop_back(); 14 | 15 | dq.push_back(i); 16 | } 17 | 18 | for (; i < n; ++i) 19 | { 20 | cout << arr[dq.front()] << " "; 21 | 22 | while ((!dq.empty()) && dq.front() <= i - k) 23 | 24 | dq.pop_front(); 25 | 26 | while ((!dq.empty()) && arr[i] >= arr[dq.back()]) 27 | dq.pop_back(); 28 | 29 | dq.push_back(i); 30 | } 31 | 32 | cout << arr[dq.front()]; 33 | } 34 | 35 | //Testing code 36 | int main() 37 | { 38 | int arr[] = { 12, 156, 73, 93, 59, 83, 51, 73, 101, 456}; 39 | int n = sizeof(arr) / sizeof(arr[0]); 40 | int k = 3; 41 | printKMax(arr, n, k); 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /sum_of_odd_and_even.cpp: -------------------------------------------------------------------------------- 1 | // digit like 12345678 2 | // even=20 odd= 16 3 | 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | int a,even=0,n,odd=0; 9 | cin>>n; /*input digit*/ 10 | while(n>0){ 11 | a=n%10; 12 | n=n/10; 13 | if(a%2==0){ 14 | even=even+a; 15 | } 16 | else{ 17 | odd=odd+a; 18 | } 19 | }cout<<"even="< 2 | int main() 3 | { 4 | int a[]= {8,4,1,3,2}; 5 | int i,value,hole; 6 | for(i=1; i<5; i++) 7 | { 8 | value=a[i]; 9 | hole=i; 10 | while(hole>0&&a[hole-1]>value) 11 | { 12 | a[hole]=a[hole-1]; 13 | hole--; 14 | } 15 | a[hole]=value; 16 | int a[]= {8,4,1,3,2}; 17 | int i,value,hole; 18 | for(i=1; i<5; i++) 19 | { 20 | value=a[i]; 21 | hole=i; 22 | while(hole>0&&a[hole-1]>value) 23 | { 24 | a[hole]=a[hole-1]; 25 | hole--; 26 | } 27 | a[hole]=value; 28 | 29 | } 30 | printf("Sorted Array:\n\n"); 31 | for(i=0; i<5; i++) 32 | { 33 | printf("%d",a[i]); 34 | } 35 | printf("\n\n"); 36 | 37 | } 38 | printf("Sorted Array:\n\n"); 39 | for(i=0; i<5; i++) 40 | { 41 | printf("%d",a[i]); 42 | } 43 | printf("\n\n"); 44 | return 0; 45 | 46 | } 47 | -------------------------------------------------------------------------------- /vertical-order-traversal.cpp: -------------------------------------------------------------------------------- 1 | // Vertical order traversal of a binary tree 2 | 3 | // Time: O(n) 4 | // Space: O(n) 5 | 6 | /** 7 | * Definition for a binary tree node. 8 | * struct TreeNode { 9 | * int val; 10 | * TreeNode *left; 11 | * TreeNode *right; 12 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 13 | * }; 14 | */ 15 | class Solution { 16 | public: 17 | vector> verticalOrder(TreeNode* root) { 18 | unordered_map> cols; 19 | vector> queue{{root, 0}}; 20 | for (int i = 0; i < queue.size(); ++i) { 21 | TreeNode *node; 22 | int j; 23 | tie(node, j) = queue[i]; 24 | if (node) { 25 | cols[j].emplace_back(node->val); 26 | queue.push_back({node->left, j - 1}); 27 | queue.push_back({node->right, j + 1}); 28 | } 29 | } 30 | int min_idx = numeric_limits::max(), 31 | max_idx = numeric_limits::min(); 32 | for (const auto& kvp : cols) { 33 | min_idx = min(min_idx, kvp.first); 34 | max_idx = max(max_idx, kvp.first); 35 | } 36 | vector> res; 37 | for (int i = min_idx; !cols.empty() && i <= max_idx; ++i) { 38 | res.emplace_back(move(cols[i])); 39 | } 40 | return res; 41 | } 42 | }; 43 | --------------------------------------------------------------------------------