Posts

Print Mirror Upper Star Triangle Pattern using java

  Divide this into two parts Upper part Lower part Both parts are a mirror reflection of each other. And each part comprises 2 triangles. Therefore, In total, we have to print 4 triangles to get the desired pattern. Example 1:  Upper Part // Importing input output classes import java.io.*; // Main Class class GFG { // Method 1 // To print upper part of the pattern private static void displayUpperPart(int size) { // Declaring variables for rows and columns // respectively int m, n; // Outer loop for rows for (m = size - 1; m >= 0; m--) { // Inner loop 1 // to print triangle 1 for (n = 0; n < m; n++) { // Printing whitespace System.out.print(" "); } // Inner loop 2 // to print triangle 2 for (n = m; n <= size - 1; n++) { // Printing star with whitespace System.out.print("*" + " "); } // By now done with one rpw so next line System.out.println(); } } //...

Program for Priority CPU Scheduling

 #include<stdio.h>  int main() {     int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;     printf("Enter Total Number of Process:");     scanf("%d",&n);      printf("\nEnter Burst Time and Priority\n");     for(i=0;i<n;i++)     {         printf("\nP[%d]\n",i+1);         printf("Burst Time:");         scanf("%d",&bt[i]);         printf("Priority:");         scanf("%d",&pr[i]);         p[i]=i+1;           //contains process number     }      //sorting burst time, priority and process number in ascending order using selection sort     for(i=0;i<n;i++)     {         pos=i;         for(j=i+1;j<n;j++)         { ...

First Come First Serve Scheduling In C

 #include<stdio.h>   int main()  {     int n,bt[20],wt[20],tat[20],avwt=0,avtat=0,i,j;     printf("Enter total number of processes(maximum 20):");     scanf("%d",&n);      printf("nEnter Process Burst Timen");     for(i=0;i<n;i++)     {         printf("P[%d]:",i+1);         scanf("%d",&bt[i]);     }      wt[0]=0;         for(i=1;i<n;i++)     {         wt[i]=0;         for(j=0;j<i;j++)             wt[i]+=bt[j];     }      printf("nProcessttBurst TimetWaiting TimetTurnaround Time");      for(i=0;i<n;i++)     {         tat[i]=bt[i]+wt[i];         avwt+=wt[i];         avtat+=tat[i];         p...

Shortest Job First Scheduling Algorithm - C code

  #include<stdio.h>  int main() {     int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;     float avg_wt,avg_tat;     printf("Enter number of process:");     scanf("%d",&n);       printf("nEnter Burst Time:n");     for(i=0;i<n;i++)     {         printf("p%d:",i+1);         scanf("%d",&bt[i]);         p[i]=i+1;              }      //sorting of burst times     for(i=0;i<n;i++)     {         pos=i;         for(j=i+1;j<n;j++)         {             if(bt[j]<bt[pos])                 pos=j;         }           temp=bt[i];         bt[i]=bt[pos];     ...

Binary Search Program in Java

Binary Search is a searching algorithm for finding an element's position in a sorted array. In this approach, the element is always searched in the middle of a portion of an array. Binary search can be implemented only on a sorted list of items. If the elements are not sorted already, we need to sort them first. Iteration Method class BinarySearch {   int binarySearch(int array[], int x, int low, int high) {     // Repeat until the pointers low and high meet each other     while (low <= high) {       int mid = low + (high - low) / 2;       if (array[mid] == x)         return mid;       if (array[mid] < x)         low = mid + 1;       else         high = mid - 1;     }     return -1;   }   public static void main(String args[]) {     BinarySearch ob = new BinarySearch();     int array[] =...

Bucket Sort Program in Java

  Bucket Sort is a sorting algorithm that divides the unsorted array elements into several groups called buckets. Each bucket is then sorted by using any of the suitable  sorting algorithms  or recursively applying the same bucket algorithm. import java.util.ArrayList; import java.util.Collections; public class BucketSort {   public void bucketSort(float[] arr, int n) {     if (n <= 0)       return;     @SuppressWarnings("unchecked")     ArrayList<Float>[] bucket = new ArrayList[n];     // Create empty buckets     for (int i = 0; i < n; i++)       bucket[i] = new ArrayList<Float>();     // Add elements into the buckets     for (int i = 0; i < n; i++) {       int bucketIndex = (int) arr[i] * n;       bucket[bucketIndex].add(arr[i]);     }     // Sort the elements of each bucket     for...

Selection sort Program in Java

  Selection sort is  a sorting algorithm  that selects the smallest element from an unsorted list in each iteration and places that element at the beginning of the unsorted list. import java.util.Arrays; class SelectionSort {   void selectionSort(int array[]) {     int size = array.length;     for (int step = 0; step < size - 1; step++) {       int min_idx = step;       for (int i = step + 1; i < size; i++) {         // To sort in descending order, change > to < in this line.         // Select the minimum element in each loop.         if (array[i] < array[min_idx]) {           min_idx = i;         }       }       // put min at the correct position       int temp = array[step];       array[step] = array[min_idx];       array[mi...