diff --git a/BubbleSort.java b/BubbleSort.java index ae1ceaa..81e3ef9 100644 --- a/BubbleSort.java +++ b/BubbleSort.java @@ -1,4 +1,5 @@ public class BubbleSortExample { + //worst case of this code is O(n2). static void bubbleSort(int[] arr) { int n = arr.length; int temp = 0; @@ -13,8 +14,32 @@ static void bubbleSort(int[] arr) { } } + - } + } + // An optimized version of Bubble Sort + //worst case of this code is O(n). + static void optimizedbubbleSort(int arr[], int n) + { + int i, j, temp; + boolean swapped; + for (i = 0; i < n - 1; i++) + { + swapped = false; + for (j = 0; j < n - i - 1; j++) + { + if (arr[j] > arr[j + 1]) + { + temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + swapped = true; + } + } + if (swapped == false) + break; + } + } public static void main(String[] args) { int arr[] ={3,60,35,2,45,320,5}; @@ -32,4 +57,4 @@ public static void main(String[] args) { } } -} \ No newline at end of file +} diff --git a/CountingSortAlgo.java b/CountingSortAlgo.java new file mode 100644 index 0000000..131d6f9 --- /dev/null +++ b/CountingSortAlgo.java @@ -0,0 +1,55 @@ +// Counting sort in Java programming + +//import file +import java.util.Arrays; + +class CountingSort { + void countSort(int array[], int size) { + int[] output = new int[size + 1]; + + // For Finding the largest element of the array + int max = array[0]; + for (int i = 1; i < size; i++) { + if (array[i] > max) + max = array[i]; + } + int[] count = new int[max + 1]; + + // Initialize count array with all zeros + for (int i = 0; i < max; ++i) { + count[i] = 0; + } + + // Store the count of each element + for (int i = 0; i < size; i++) { + count[array[i]]++; + } + + // Store the cummulative count of each array + for (int i = 1; i <= max; i++) { + count[i] += count[i - 1]; + } + + // Find the index of each element of the original array in count array, and + // place the elements in output array + for (int i = size - 1; i >= 0; i--) { + output[count[array[i]] - 1] = array[i]; + count[array[i]]--; + } + + // Copy the sorted elements into original array + for (int i = 0; i < size; i++) { + array[i] = output[i]; + } + } + + // Driver code for the above code + public static void main(String args[]) { + int[] data = { 4, 2, 2, 8, 3, 3, 1 }; + int size = data.length; + CountingSort cs = new CountingSort(); + cs.countSort(data, size); + System.out.println("Sorted Array in Ascending Order: "); + System.out.println(Arrays.toString(data)); + } +} diff --git a/Date_and_time.java b/Date_and_time.java new file mode 100644 index 0000000..e6ce5e0 --- /dev/null +++ b/Date_and_time.java @@ -0,0 +1,32 @@ +// Java program to convert 24 hour +// time format to 12 hour format + +// Importing specific date class libraries +import java.util.Date; +import java.text.SimpleDateFormat; + +public class GFG { + +// Main driver method + public static void main(String[] args) + { + // Getting the current current time + Date date = new Date(); + + + System.out.println("Current Time is : " + date); + + // set format in 12 hours + SimpleDateFormat formatTime = new SimpleDateFormat("hh.mm aa"); + // hh = hours in 12hr format + // mm = minutes + // aa = am/pm + + String time = formatTime.format( + date); // changing the format of 'date' + + // display time as per format + System.out.println( + "Current Time in AM/PM Format is : " + time); + } +} diff --git a/Determinant_of_Matrix.java b/Determinant_of_Matrix.java new file mode 100644 index 0000000..bf856b9 --- /dev/null +++ b/Determinant_of_Matrix.java @@ -0,0 +1,94 @@ +// Java program to find Determinant of a matrix +class GFG { + + // Dimension of input square matrix + static final int N = 4; + + // Function to get determinant of matrix + static int determinantOfMatrix(int mat[][], int n) + { + int num1, num2, det = 1, index, + total = 1; // Initialize result + + // temporary array for storing row + int[] temp = new int[n + 1]; + + // loop for traversing the diagonal elements + for (int i = 0; i < n; i++) { + index = i; // initialize the index + + // finding the index which has non zero value + while (mat[index][i] == 0 && index < n) { + index++; + } + if (index == n) // if there is non zero element + { + // the determinant of matrix as zero + continue; + } + if (index != i) { + // loop for swaping the diagonal element row + // and index row + for (int j = 0; j < n; j++) { + swap(mat, index, j, i, j); + } + // determinant sign changes when we shift + // rows go through determinant properties + det = (int)(det * Math.pow(-1, index - i)); + } + + // storing the values of diagonal row elements + for (int j = 0; j < n; j++) { + temp[j] = mat[i][j]; + } + + // traversing every row below the diagonal + // element + for (int j = i + 1; j < n; j++) { + num1 = temp[i]; // value of diagonal element + num2 = mat[j] + [i]; // value of next row element + + // traversing every column of row + // and multiplying to every row + for (int k = 0; k < n; k++) { + // multiplying to make the diagonal + // element and next row element equal + mat[j][k] = (num1 * mat[j][k]) + - (num2 * temp[k]); + } + total = total * num1; // Det(kA)=kDet(A); + } + } + + // multiplying the diagonal elements to get + // determinant + for (int i = 0; i < n; i++) { + det = det * mat[i][i]; + } + return (det / total); // Det(kA)/k=Det(A); + } + + static int[][] swap(int[][] arr, int i1, int j1, int i2, + int j2) + { + int temp = arr[i1][j1]; + arr[i1][j1] = arr[i2][j2]; + arr[i2][j2] = temp; + return arr; + } + + // Driver code + public static void main(String[] args) + { + int mat[][] = { { 1, 0, 2, -1 }, + { 3, 0, 0, 5 }, + { 2, 1, 4, -3 }, + { 1, 0, 5, 0 } }; + + // Function call + System.out.printf( + "Determinant of the matrix is : %d", + determinantOfMatrix(mat, N)); + } +} diff --git a/Electricitybill.java b/Electricitybill.java new file mode 100644 index 0000000..8792ab8 --- /dev/null +++ b/Electricitybill.java @@ -0,0 +1,26 @@ +class ElectricityBillExample1 +{ + // main() method start + public static void main(String args[]) + { + // declare and initialize variable units + int units = 380; + // variable to calculate electricity bill to pay + double billToPay = 0; + // check whether units are less than 100 + if(units < 100) + { + billToPay = units * 1.20; + } + // check whether the units are less than 300 + else if(units < 300){ + billToPay = 100 * 1.20 + (units - 100) * 2; + } + // check whether the units are greater than 300 + else if(units > 300) + { + billToPay = 100 * 1.20 + 200 * 2 + (units - 300) * 3; + } + System.out.println("The electricity bill for " +units+ " is : " + billToPay); + } +} diff --git a/FactorialProgram.java b/FactorialProgram.java new file mode 100644 index 0000000..de1ecbc --- /dev/null +++ b/FactorialProgram.java @@ -0,0 +1,31 @@ +import java.util.Scanner; + +class Abc +{ + public int f=1,i=0; + + public void fact(int n) + { + for(i=1; i<=n; i++) + { + f = f*i; + } + + System.out.println(n + " Factorial is : " + f); + } +} + +class factorial +{ + public static void main(String arr[]) + { + Scanner m = new Scanner(System.in); + + System.out.print("Enter Elemnt TO find Factorial : "); + int n = m.nextInt(); + + Abc a1 = new Abc(); + a1.fact(n); + + } +} diff --git a/FibonacciSeries.java b/FibonacciSeries.java new file mode 100644 index 0000000..4e6629e --- /dev/null +++ b/FibonacciSeries.java @@ -0,0 +1,15 @@ +class Fibonacci{ +public static void main(String args[]) +{ + int n1=0,n2=1,n3,i,count=10; + System.out.print(n1+" "+n2);//printing 0 and 1 + + for(i=2;i0) + { + Scanner meet = new Scanner(System.in); + + System.out.print("Enter Bowler Player Type (Fast / Spin / Medium) : "); + role = meet.next(); + } + + } +} + +class batsman extends CricketPlayer +{ + void getCar() + { + Scanner meet = new Scanner(System.in); + + System.out.print("Enter Player Carrer Total Match : "); + total = meet.nextInt(); + + } + + void print1() + { + System.out.println("Player Type : " + type); + System.out.println("Carrer Total Match : " + total); + } + +} + +class Bowler extends CricketPlayer +{ + + void print() + { + System.out.println("Player Name : " + name); + System.out.println("Player Age : " + age); + + if(c == 1) + { + System.out.println("Player Boling Type : " + role); + } + + } + +} + +class Inheri { + + public static void main(String arr[]) + { + Bowler b1 = new Bowler(); + batsman c1 = new batsman(); + + b1.getData(); + c1.getCar(); + b1.Bowler_Info(); + + System.out.print("\n------------------------------------------------\n"); + + b1.print(); + c1.print1(); + + System.out.println("------------------------------------------------"); + + } + +} diff --git a/RadixSort.java b/RadixSort.java new file mode 100644 index 0000000..7667727 --- /dev/null +++ b/RadixSort.java @@ -0,0 +1,80 @@ +// Radix sort Java implementation + +import java.io.*; +import java.util.*; + +class Radix { + + // A utility function to get maximum value in arr[] + static int getMax(int arr[], int n) + { + int mx = arr[0]; + for (int i = 1; i < n; i++) + if (arr[i] > mx) + mx = arr[i]; + return mx; + } + + // A function to do counting sort of arr[] according to + // the digit represented by exp. + static void countSort(int arr[], int n, int exp) + { + int output[] = new int[n]; // output array + int i; + int count[] = new int[10]; + Arrays.fill(count, 0); + + // Store count of occurrences in count[] + for (i = 0; i < n; i++) + count[(arr[i] / exp) % 10]++; + + // Change count[i] so that count[i] now contains + // actual position of this digit in output[] + for (i = 1; i < 10; i++) + count[i] += count[i - 1]; + + // Build the output array + for (i = n - 1; i >= 0; i--) { + output[count[(arr[i] / exp) % 10] - 1] = arr[i]; + count[(arr[i] / exp) % 10]--; + } + + // Copy the output array to arr[], so that arr[] now + // contains sorted numbers according to current + // digit + for (i = 0; i < n; i++) + arr[i] = output[i]; + } + + // The main function to that sorts arr[] of + // size n using Radix Sort + static void radixsort(int arr[], int n) + { + // Find the maximum number to know number of digits + int m = getMax(arr, n); + + // Do counting sort for every digit. Note that + // instead of passing digit number, exp is passed. + // exp is 10^i where i is current digit number + for (int exp = 1; m / exp > 0; exp *= 10) + countSort(arr, n, exp); + } + + // A utility function to print an array + static void print(int arr[], int n) + { + for (int i = 0; i < n; i++) + System.out.print(arr[i] + " "); + } + + // Main driver method + public static void main(String[] args) + { + int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 }; + int n = arr.length; + + // Function Call + radixsort(arr, n); + print(arr, n); + } +} diff --git a/Reverse a Number using a while loop.java b/Reverse a Number using a while loop.java new file mode 100644 index 0000000..a443512 --- /dev/null +++ b/Reverse a Number using a while loop.java @@ -0,0 +1,21 @@ +class Main { + public static void main(String[] args) { + + int num = 1234, reversed = 0; + + System.out.println("Original Number: " + num); + + // run loop until num becomes 0 + while(num != 0) { + + // get last digit from num + int digit = num % 10; + reversed = reversed * 10 + digit; + + // remove the last digit from num + num /= 10; + } + + System.out.println("Reversed Number: " + reversed); + } +} diff --git a/Reverse-words-in-string.java b/Reverse-words-in-string.java new file mode 100644 index 0000000..840625a --- /dev/null +++ b/Reverse-words-in-string.java @@ -0,0 +1,18 @@ +class Solution { + public String reverseWords(String s) { + String resp = ""; + List list = Arrays.asList(s.split(" ")); + for(String word : list){ + resp += reverseIt(word); + resp += " "; + } + return resp.trim(); + } + + String reverseIt(String word){ + StringBuilder input = new StringBuilder(); + input.append(word); + input.reverse(); + return input.toString(); + } +} diff --git a/Shell_Sort.java b/Shell_Sort.java new file mode 100644 index 0000000..15c1589 --- /dev/null +++ b/Shell_Sort.java @@ -0,0 +1,47 @@ +package Sorting; + +public class shell { + + public static void main(String args[]) + { + int[] a={9, 8, 3, 56, 5, 6, 4, 1}; + int length=a.length; + + + int gap=length/2; + // System.out.println(gap); + while(gap>0) + { + for(int i=gap;i=gap && a[j-gap]>temp) + { + // System.out.println(a[j]); + a[j]=a[j-gap]; + // System.out.println(a[j]); + j=j-gap; + + a[j]=temp; + // System.out.println(a[j]); + + } + + + } + + gap=gap/2; + + + } + for(int i=0;i spiralOrder(vector>& matrix) { + + int count=0; + vector ans; + int startingrow=0,startingcol=0,endingrow=matrix.size(),endingcol=matrix[0].size(); + int total=endingrow*endingcol; + endingrow=matrix.size()-1; + endingcol=endingcol-1; + + while(count=startingcol&&count=startingrow&&count arr[j]){ - //swap elements - temp = arr[j-1]; - arr[j-1] = arr[j]; - arr[j] = temp; - } - - } - } - - } - public static void main(String[] args) { - int arr[] ={3,60,35,2,45,320,5}; - - System.out.println("Array Before Bubble Sort"); - for(int i=0; i < arr.length; i++){ - System.out.print(arr[i] + " "); - } - System.out.println(); - - bubbleSort(arr);//sorting array elements using bubble sort - - System.out.println("Array After Bubble Sort"); - for(int i=0; i < arr.length; i++){ - System.out.print(arr[i] + " "); - } - - } -} +// Java program for implementation of Bubble Sort +class BubbleSort { + void bubbleSort(int arr[]) + { + int n = arr.length; + for (int i = 0; i < n - 1; i++) + for (int j = 0; j < n - i - 1; j++) + if (arr[j] > arr[j + 1]) { + // swap arr[j+1] and arr[j] + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + + /* Prints the array */ + void printArray(int arr[]) + { + int n = arr.length; + for (int i = 0; i < n; ++i) + System.out.print(arr[i] + " "); + System.out.println(); + } + + // Driver method to test above + public static void main(String args[]) + { + BubbleSort ob = new BubbleSort(); + int arr[] = { 64, 34, 25, 12, 22, 11, 90 }; + ob.bubbleSort(arr); + System.out.println("Sorted array"); + ob.printArray(arr); + } +} diff --git a/bubblesortoptimize/Sparsh225.md b/bubblesortoptimize/Sparsh225.md new file mode 100644 index 0000000..6cdf5a4 --- /dev/null +++ b/bubblesortoptimize/Sparsh225.md @@ -0,0 +1,43 @@ +public class BubbleSortExample { + + // An optimized version of Bubble Sort + //worst case of this code is O(n). + static void bubbleSort(int arr[], int n) + { + int i, j, temp; + boolean swapped; + for (i = 0; i < n - 1; i++) + { + swapped = false; + for (j = 0; j < n - i - 1; j++) + { + if (arr[j] > arr[j + 1]) + { + temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + swapped = true; + } + } + if (swapped == false) + break; + } + } + public static void main(String[] args) { + int arr[] ={3,60,35,2,45,320,5}; + + System.out.println("Array Before Bubble Sort"); + for(int i=0; i < arr.length; i++){ + System.out.print(arr[i] + " "); + } + System.out.println(); + + bubbleSort(arr);//sorting array elements using bubble sort + + System.out.println("Array After Bubble Sort"); + for(int i=0; i < arr.length; i++){ + System.out.print(arr[i] + " "); + } + + } +} diff --git a/countSort.java b/countSort.java new file mode 100644 index 0000000..332a4bb --- /dev/null +++ b/countSort.java @@ -0,0 +1,23 @@ +public class countSort { + public static void main(String[] args) { + int[] arr = {5,4,3,3,2,1}; //created array + int k = 5; + int n = arr.length; + int[] countArr = new int[k+1]; + for(int i=1;i<=k;i++) countArr[i] = 0; + + for(int i=0;i numbers = new ArrayList<>(); + + // Add elements + numbers.add(4); + numbers.add(2); + numbers.add(3); + System.out.println("Unsorted ArrayList: " + numbers); + + // Using the sort() method + Collections.sort(numbers); + System.out.println("Sorted ArrayList: " + numbers); + + } +} diff --git a/subscribe.java b/subscribe.java new file mode 100644 index 0000000..8484c95 --- /dev/null +++ b/subscribe.java @@ -0,0 +1,204 @@ +// import java.util.Scanner; +// public class subscribe{ +// public static void main(String[] args){ + +// int age; +// System.out.println("Enter your age"); +// Scanner sc =new Scanner(System.in); +// age = sc.nextInt(); +// // if(age>56){ +// // System.out.println("you are experienced"); +// // } +// // else if(age>46){ +// // System.out.println("You are semi experienced"); +// // } +// // else{ +// // System.out.println("you are not experienced"); +// // } +// switch(age){ +// case 18: +// System.out.println("you are going to be an adult"); +// break; +// case 23: +// System.out.println("you are going to be young"); +// break; +// case 60: +// System.out.println("you are going to be old"); +// break; +// default: +// System.out.println("enjoy life"); +// break; +// } +// } +// } +// import java.util.Scanner; +// public class subscribe{ +// public static void main(String[] args){ +// int x=2; +// int y; +// System.out.println("Enter value of y:"); +// Scanner sc = new Scanner(System.in); +// y = sc.nextInt(); +// switch(y){ +// case 0 : +// System.out.println('1'); +// break; +// case 1 : +// System.out.println(x); +// break; +// case 2 : +// System.out.println( x * x); +// break; +// case 3 : +// System.out.println( x * x * x ); +// break; +// case 4 : +// System.out.println( x * x * x * x); +// break; +// default : +// System.out.println("No match exists."); +// } +// } +// } +// import java.util.Scanner; +// public class subscribe{ +// char LetterGrade; +// public static void main(String[] args){ +// System.out.println("Enter lettergrade"); +// Scanner sc = new Scanner(System.in); +// char LetterGrade = sc.next().charAt(0); +// switch (LetterGrade) { +// case 'a' : +// case 'A' : System.out.print( "Excellent"); +// break; +// case 'b' : +// case 'B' : System.out.print("Superior"); +// break; +// case 'C' : +// case 'c' : System.out.print("Average"); +// break; case 'd' : +// case 'D' : System.out.print(" Poor"); +// break; +// case 'f' : +// case 'F' : System.out.print( " Try again"); +// break; +// default : System.out.print("This is not a recognized letter grade."); +// } +// } +// } +// import java.util.Scanner; +// public class subscribe{ +// public static void main(String[] args){ +// char What; +// System.out.println("Enter lettergrade"); +// Scanner sc = new Scanner(System.in); +// What = sc.next().charAt(0); +// switch (What) { +// case 'c' : +// case 'C' : System.out.print(" Bobo "); +// case 'y' : +// case 'P' : System.out.print(" Is a Dog "); +// break; +// case 'x' : +// case 'X' : System.out.print(" Try But "); +// case 'z' : +// case 'Z' : System.out.print(" Cannot"); +// default : System.out.print(" Help You."); +// } +// } +// } +// import java.util.Scanner; +// public class subscribe{ +// public static void main(String[] args){ +// int Year; +// System.out.println("Enter your age"); +// Scanner sc =new Scanner(System.in); +// Year = sc.nextInt(); +// // if (Year == 1) +// // System.out.print(" Freshman "); else if (Year == 2) +// // System.out.print(" Sophomore "); +// // else if (Year == 3) +// // System.out.print(" Junior "); +// // else if (Year == 4) +// // System.out.print(" Senior "); +// // else +// // System.out.print(" Graduate "); +// switch(Year){ +// case 1: +// System.out.println("Freshman"); +// break; +// case 2: +// System.out.println("Sophomore"); +// break; +// case 3: +// System.out.println("Junior"); +// break; +// case 4: +// System.out.println("Senior"); +// break; +// default: +// System.out.println("Graduate"); +// } +// } +// } +// import java.util.Scanner; +// public class subscribe{ +// static void gp(int a,int r,int n){ +// int current_term; +// for(int i=0;i