From dd53cefef94da871b152c4b9b2c0efb2690503fd Mon Sep 17 00:00:00 2001 From: Sparsh Rawat <92641998+Sparsh225@users.noreply.github.com> Date: Sat, 1 Oct 2022 08:59:32 +0530 Subject: [PATCH 01/23] An optimized version of Bubble Sort --- BubbleSort.java | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/BubbleSort.java b/BubbleSort.java index ae1ceaa..ff3784b 100644 --- a/BubbleSort.java +++ b/BubbleSort.java @@ -13,8 +13,31 @@ static void bubbleSort(int[] arr) { } } + - } + } + // An optimized version of Bubble Sort + 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 +55,4 @@ public static void main(String[] args) { } } -} \ No newline at end of file +} From c53a6ed94ca26786eb149db32abebeb7f9953eb5 Mon Sep 17 00:00:00 2001 From: Sparsh Rawat <92641998+Sparsh225@users.noreply.github.com> Date: Sat, 1 Oct 2022 09:01:26 +0530 Subject: [PATCH 02/23] adding time complexity --- BubbleSort.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/BubbleSort.java b/BubbleSort.java index ff3784b..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; @@ -17,6 +18,7 @@ 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; From e2e3111f46166b32ca6fe300706bbc14ca566e0e Mon Sep 17 00:00:00 2001 From: Sparsh Rawat <92641998+Sparsh225@users.noreply.github.com> Date: Sat, 1 Oct 2022 09:10:07 +0530 Subject: [PATCH 03/23] optimize sort --- bubblesortoptimize/Sparsh225.md | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 bubblesortoptimize/Sparsh225.md 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] + " "); + } + + } +} From 09a3d70634588c0b5eda1e6a9f8ed4eaed321e98 Mon Sep 17 00:00:00 2001 From: Vaghela Ajitkumar <91899491+ajitkumar1264@users.noreply.github.com> Date: Sat, 1 Oct 2022 10:02:57 +0530 Subject: [PATCH 04/23] shell_sort code --- Shell_Sort.java | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Shell_Sort.java 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 Date: Sat, 1 Oct 2022 10:33:25 +0530 Subject: [PATCH 05/23] Add files via upload --- subscribe.java | 204 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 subscribe.java 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 Date: Sat, 1 Oct 2022 10:46:31 +0530 Subject: [PATCH 06/23] Radix Sort --- RadixSort.java | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 RadixSort.java 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); + } +} From 7bb89b2469ba69a7d2341fa8b87294dbd2949070 Mon Sep 17 00:00:00 2001 From: MeetVaishnav23 <79635039+MeetVaishnav23@users.noreply.github.com> Date: Sat, 1 Oct 2022 11:32:29 +0530 Subject: [PATCH 07/23] Create Inheritance.java --- Inheritance.java | 103 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 Inheritance.java diff --git a/Inheritance.java b/Inheritance.java new file mode 100644 index 0000000..21b5e51 --- /dev/null +++ b/Inheritance.java @@ -0,0 +1,103 @@ +import java.util.Scanner; + +class Player +{ + static int age,total; + static String name; + static String type; + static String role; +} + +class CricketPlayer extends Player +{ + public int c = 0; + + void getData() + { + Scanner meet = new Scanner(System.in); + + System.out.print("Enter Player Name : "); + name = meet.nextLine(); + + System.out.print("Enter Player Age : "); + age = meet.nextInt(); + + System.out.print("Enter Player Type (Batsman or Bowler) : "); + type = meet.next(); + + if(type == "Bowler" || type == "bowler") + { + c++; + } + } + + void Bowler_Info() + { + if(c>0) + { + 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("------------------------------------------------"); + + } + +} From 1c6fcbc3df364ea63b694c66931386f320b49b8e Mon Sep 17 00:00:00 2001 From: MeetVaishnav23 <79635039+MeetVaishnav23@users.noreply.github.com> Date: Sat, 1 Oct 2022 11:37:15 +0530 Subject: [PATCH 08/23] Create FactorialProgram.java --- FactorialProgram.java | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 FactorialProgram.java 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); + + } +} From 893135d51c4bd1a569529ac5b65f3d74963bbfb5 Mon Sep 17 00:00:00 2001 From: MeetVaishnav23 <79635039+MeetVaishnav23@users.noreply.github.com> Date: Sat, 1 Oct 2022 11:51:33 +0530 Subject: [PATCH 09/23] Create Electricitybill.java --- Electricitybill.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Electricitybill.java 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); + } +} From aefb1b4c4ee524650c96dbb79955b2b8830f9892 Mon Sep 17 00:00:00 2001 From: vidio Date: Sat, 1 Oct 2022 13:30:41 +0700 Subject: [PATCH 10/23] Please add my code to your repository --- overidding_MR.java | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 overidding_MR.java diff --git a/overidding_MR.java b/overidding_MR.java new file mode 100644 index 0000000..2a6e0c9 --- /dev/null +++ b/overidding_MR.java @@ -0,0 +1,62 @@ + +//example of the overriding method +//return type and signature(parameter) same means the overriding method + +class a1 +{ + void hell() + { + System.out.println("hello i am from i1"); + } +} + +class b1 extends a1 +{ + void hell() + { + + System.out.println("hello i am from i1"); + } +} + +class c1 extends b1{ + void hell() + { + System.out.println("hello i am from i1"); + } +} + +public class polymorph { + + public static void main(String[] args) + { + //where c1 is called reference + // where c1() is called instance + + + //c1 obj1=new c1(); + // obj1.hell(); + + // now we take the reference of b1 and instance c1 as same + + // b1 obj1=new c1(); + // obj1.hell(); + + //it's means that if we change the instance than automatically + //sentence will be changed or method /function will be change + + //reference is a1 and instance c1 + //output is of c1 method is display + + // a1 obj1=new c1(); + //obj1.hell(); + + //reference is a1 and instance b1 + //output is of b1 method is display + + // a1 obj1=new b1(); + // obj1.hell(); + + + } +} From e610f6cddddcc7ed7660ed617cc0bdbaa9830a83 Mon Sep 17 00:00:00 2001 From: Saurav Sutaria Date: Sat, 1 Oct 2022 12:03:04 +0530 Subject: [PATCH 11/23] count sort program added --- countSort.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 countSort.java 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 Date: Sat, 1 Oct 2022 12:22:53 +0530 Subject: [PATCH 12/23] Java Substring --- javasubstring.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 javasubstring.java diff --git a/javasubstring.java b/javasubstring.java new file mode 100644 index 0000000..68bac97 --- /dev/null +++ b/javasubstring.java @@ -0,0 +1,17 @@ +package day3; + +import java.util.*; + +class javasubstring { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + System.out.println("Enter any String value = "); + String S = in.nextLine(); + System.out.println("Enter the start point = "); + int start = in.nextInt(); + System.out.println("Enter the end point = "); + int end = in.nextInt(); + System.out.println(S.substring(start, end)); + } +} From 6b9e11390cd709202f66c2ae0a2be03cd5debf5c Mon Sep 17 00:00:00 2001 From: Shweta <65610099+shwetacharde@users.noreply.github.com> Date: Sat, 1 Oct 2022 12:35:57 +0530 Subject: [PATCH 13/23] Reverse words in string code in java --- Reverse-words-in-string.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Reverse-words-in-string.java 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(); + } +} From fe7af61c35e2c490e0d6c52a9ec5c4a0001a6ac7 Mon Sep 17 00:00:00 2001 From: shubham singla <43694495+shubhamsingla807@users.noreply.github.com> Date: Sat, 1 Oct 2022 12:47:09 +0530 Subject: [PATCH 14/23] Bubblesort Algo --- bubblesort.java | 69 ++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/bubblesort.java b/bubblesort.java index 436662e..9b9425b 100644 --- a/bubblesort.java +++ b/bubblesort.java @@ -1,35 +1,34 @@ -public class BubbleSortExample { - static void bubbleSort(int[] arr) { - int n = arr.length; - int temp = 0; - for(int i=0; i < n; i++){ - for(int j=1; j < (n-i); j++){ - if(arr[j-1] > 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); + } +} From c4763de1760274291cf78ff19c38df74795b7fa3 Mon Sep 17 00:00:00 2001 From: Suvoji01 <47207810+Suvoji01@users.noreply.github.com> Date: Sat, 1 Oct 2022 12:53:07 +0530 Subject: [PATCH 15/23] Create Reverse a Number using a while loop.java --- Reverse a Number using a while loop.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Reverse a Number using a while loop.java 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); + } +} From a2b01055f100b16d97964ff772a913df1d3f86a8 Mon Sep 17 00:00:00 2001 From: Bhumi54321 <102283681+Bhumi54321@users.noreply.github.com> Date: Sat, 1 Oct 2022 13:07:50 +0530 Subject: [PATCH 16/23] GCD --- GCD.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 GCD.java diff --git a/GCD.java b/GCD.java new file mode 100644 index 0000000..284b664 --- /dev/null +++ b/GCD.java @@ -0,0 +1,29 @@ +/** + * Java program to find Greatest Common Divisor or GCD of + * two numbers using Euclid’s method.*/ + import java.util.*; +public class GCD{ + + public static void main(String args[]){ + + //Enter two number whose GCD needs to be calculated. + Scanner sc = new Scanner(System.in); + System.out.println("Please enter first number to find GCD"); + int num1 = sc.nextInt(); + System.out.println("Please enter second number to find GCD"); + int num2 = sc.nextInt(); + + System.out.println("GCD of two numbers " + num1 +" and " + + num2 +" is :" + GCD(num1,num2)); + + + } + private static int GCD(int num1, int num2) { + //base case + if(num2 == 0){ + return num1; + } + return GCD(num2, num1%num2); + } + +} \ No newline at end of file From e098222f604947764928899192d3e2df4187f2dc Mon Sep 17 00:00:00 2001 From: Dhiren Kokal <57043968+dhirenkokal@users.noreply.github.com> Date: Sat, 1 Oct 2022 13:16:50 +0530 Subject: [PATCH 17/23] Transpose_of_Matrix.java --- Transpose_of_Matrix.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Transpose_of_Matrix.java diff --git a/Transpose_of_Matrix.java b/Transpose_of_Matrix.java new file mode 100644 index 0000000..0cc2dcc --- /dev/null +++ b/Transpose_of_Matrix.java @@ -0,0 +1,30 @@ +public class MatrixTransposeExample{ +public static void main(String args[]){ +//creating a matrix +int original[][]={{1,3,4},{2,4,3},{3,4,5}}; + +//creating another matrix to store transpose of a matrix +int transpose[][]=new int[3][3]; //3 rows and 3 columns + +//Code to transpose a matrix +for(int i=0;i<3;i++){ +for(int j=0;j<3;j++){ +transpose[i][j]=original[j][i]; +} +} + +System.out.println("Printing Matrix without transpose:"); +for(int i=0;i<3;i++){ +for(int j=0;j<3;j++){ +System.out.print(original[i][j]+" "); +} +System.out.println();//new line +} +System.out.println("Printing Matrix After Transpose:"); +for(int i=0;i<3;i++){ +for(int j=0;j<3;j++){ +System.out.print(transpose[i][j]+" "); +} +System.out.println();//new line +} +}} From d34177b65f0d299c984631e670dfbffcfae354f5 Mon Sep 17 00:00:00 2001 From: Alia6922 <112715115+Alia6922@users.noreply.github.com> Date: Sat, 1 Oct 2022 13:22:48 +0530 Subject: [PATCH 18/23] Counting Sort Algo --- CountingSortAlgo.java | 55 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 CountingSortAlgo.java 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)); + } +} From b8ff6d364e7c5a71aaad178a391a653b541af945 Mon Sep 17 00:00:00 2001 From: Manish Kumar <47105557+manishsinghkuswaha@users.noreply.github.com> Date: Sat, 1 Oct 2022 13:31:49 +0530 Subject: [PATCH 19/23] hacktoberfest-2022 please add my file to your repository with label as hacktoberfest-2022 --- Spiral_Matrix.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Spiral_Matrix.cpp diff --git a/Spiral_Matrix.cpp b/Spiral_Matrix.cpp new file mode 100644 index 0000000..053dd25 --- /dev/null +++ b/Spiral_Matrix.cpp @@ -0,0 +1,37 @@ + vector 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 Date: Sat, 1 Oct 2022 14:02:30 +0530 Subject: [PATCH 20/23] Create sortingUsingsort.java The sort() method provided by the collections framework is used to sort elements. --- sortingUsingsort.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 sortingUsingsort.java diff --git a/sortingUsingsort.java b/sortingUsingsort.java new file mode 100644 index 0000000..0bfebd0 --- /dev/null +++ b/sortingUsingsort.java @@ -0,0 +1,21 @@ +import java.util.ArrayList; +import java.util.Collections; + +class Main { + public static void main(String[] args) { + + // Creating an array list + ArrayList 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); + + } +} From ee573bbb4da16ff8cd3b1c7473be8798800fd7b0 Mon Sep 17 00:00:00 2001 From: Dhiren Kokal <57043968+dhirenkokal@users.noreply.github.com> Date: Sat, 1 Oct 2022 14:19:26 +0530 Subject: [PATCH 21/23] Date_and_time --- Date_and_time.java | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Date_and_time.java 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); + } +} From 668300a9e2756d7f05be19247490cc371605522e Mon Sep 17 00:00:00 2001 From: Dhiren Kokal <57043968+dhirenkokal@users.noreply.github.com> Date: Sat, 1 Oct 2022 14:25:19 +0530 Subject: [PATCH 22/23] Determinant_of_Matrix --- Determinant_of_Matrix.java | 94 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Determinant_of_Matrix.java 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)); + } +} From 3a45dc5569b8b8ec551ff3e3e01e9e091361dee1 Mon Sep 17 00:00:00 2001 From: vmeet95 <91734696+vmeet95@users.noreply.github.com> Date: Sat, 1 Oct 2022 14:29:34 +0530 Subject: [PATCH 23/23] Create FibonacciSeries.java --- FibonacciSeries.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 FibonacciSeries.java 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;i