Loading...
More Java Posts
import java.util.Arrays;public class QuickSortMain {private static int array[];public static void sort(int[] arr) {if (arr == null || arr.length == 0) {return;}array = arr;quickSort(0, array.length-1);}private static void quickSort(int left, int right) {int i = left;int j = right;// find pivot number, we will take it as midint pivot = array[left+(right-left)/2];while (i <= j) {/*** In each iteration, we will increment left until we find element greater than pivot* We will decrement right until we find element less than pivot*/while (array[i] < pivot) { i++; } while (array[j] > pivot) {j--;}if (i <= j) {exchange(i, j);//move index to next position on both sidesi++;j--;}}// call quickSort() method recursivelyif (left < j)quickSort(left, j);if (i < right)quickSort(i, right);}private static void exchange(int i, int j) {int temp = array[i];array[i] = array[j];array[j] = temp;}public static void main(String a[]){int[] input = {33,21,45,64,55,34,11,8,3,5,1};System.out.println("Before Sorting : ");System.out.println(Arrays.toString(input));sort(input);System.out.println("==================");System.out.println("After Sorting : ");System.out.println(Arrays.toString(array));}}
import java.util.*;public class HeapSortMain {public static void buildheap(int []arr) {/** As last non leaf node will be at (arr.length-1)/2* so we will start from this location for heapifying the elements* */for(int i=(arr.length-1)/2; i>=0; i--){heapify(arr,i,arr.length-1);}}public static void heapify(int[] arr, int i,int size) {int left = 2*i+1;int right = 2*i+2;int max;if(left <= size && arr[left] > arr[i]){max=left;} else {max=i;}if(right <= size && arr[right] > arr[max]) {max=right;}// If max is not current node, exchange it with max of left and right childif(max!=i) {exchange(arr,i, max);heapify(arr, max,size);}}public static void exchange(int[] arr,int i, int j) {int t = arr[i];arr[i] = arr[j];arr[j] = t;}public static int[] heapSort(int[] arr) {buildheap(arr);int sizeOfHeap=arr.length-1;for(int i=sizeOfHeap; i>0; i--) {exchange(arr,0, i);sizeOfHeap=sizeOfHeap-1;heapify(arr, 0,sizeOfHeap);}return arr;}public static void main(String[] args) {int[] arr={1,10,16,19,3,5};System.out.println("Before Heap Sort : ");System.out.println(Arrays.toString(arr));arr=heapSort(arr);System.out.println("=====================");System.out.println("After Heap Sort : ");System.out.println(Arrays.toString(arr));}}
import java.util.Arrays;public class LongestIncreasingSubsequence {public int lengthOfLIS(int[] nums) {if (nums.length == 0) {return 0;}int[] dp = new int[nums.length];Arrays.fill(dp, 1); // Initialize all elements in dp array to 1, as each element itself is a valid subsequence of length 1.int maxLength = 1; // Initialize the maximum length to 1, considering a single element as the sequence.for (int i = 1; i < nums.length; i++) {for (int j = 0; j < i; j++) {if (nums[i] > nums[j]) {dp[i] = Math.max(dp[i], dp[j] + 1); // If nums[i] is greater than nums[j], update the longest subsequence length ending at i.}}maxLength = Math.max(maxLength, dp[i]); // Update the maximum length if needed.}return maxLength;}public static void main(String[] args) {LongestIncreasingSubsequence lis = new LongestIncreasingSubsequence();int[] nums = {10, 9, 2, 5, 3, 7, 101, 18};int result = lis.lengthOfLIS(nums);System.out.println("Length of the Longest Increasing Subsequence: " + result); // Output: 4}}
import java.util.Scanner;/* In Heaven, the seed is tall and the oak small / It's input vital* "The al-Madinatian," verse 236-37* tinyurl.com/17snqkfv* email answer to [email protected]**/public class Main{public static void main(String args[]){int x = 0;int a, c, m = 10000;Scanner in = new Scanner(System.in);System.out.println("Please enter a seed, maximum 3000000:");x = in.nextInt();System.out.println("Please input a number, maximum 29:");a = in.nextInt();System.out.println("Please input a number, maximum 999999:");c = in.nextInt();char menu = 'y';while(menu != 'n'){x = (a*x + c) % m;System.out.println("The next number is " + x + "\nWould you like another? (y/n)");menu = in.next().charAt(0);}}}//██╗ ░█████╗░███╗░░░███╗ ██████╗░░█████╗░███████╗██████╗░░█████╗░██╗░░░░░██╗░░░██╗░██████╗//██║ ██╔══██╗████╗░████║ ██╔══██╗██╔══██╗██╔════╝██╔══██╗██╔══██╗██║░░░░░██║░░░██║██╔════╝//██║ ███████║██╔████╔██║ ██║░░██║███████║█████╗░░██║░░██║███████║██║░░░░░██║░░░██║╚█████╗░//██║ ██╔══██║██║╚██╔╝██║ ██║░░██║██╔══██║██╔══╝░░██║░░██║██╔══██║██║░░░░░██║░░░██║░╚═══██╗//██║ ██║░░██║██║░╚═╝░██║ ██████╔╝██║░░██║███████╗██████╔╝██║░░██║███████╗╚██████╔╝██████╔╝//╚═╝ ╚═╝░░╚═╝╚═╝░░░░░╚═╝ ╚═════╝░╚═╝░░╚═╝╚══════╝╚═════╝░╚═╝░░╚═╝╚══════╝░╚═════╝░╚═════╝░
import javax.mail.*;import javax.mail.internet.*;import java.util.*;public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException{boolean debug = false;//Set the host smtp addressProperties props = new Properties();props.put("mail.smtp.host", "smtp.example.com");// create some properties and get the default SessionSession session = Session.getDefaultInstance(props, null);session.setDebug(debug);// create a messageMessage msg = new MimeMessage(session);// set the from and to addressInternetAddress addressFrom = new InternetAddress(from);msg.setFrom(addressFrom);InternetAddress[] addressTo = new InternetAddress[recipients.length];for (int i = 0; i < recipients.length; i++){addressTo[i] = new InternetAddress(recipients[i]);}msg.setRecipients(Message.RecipientType.TO, addressTo);// Optional : You can also set your custom headers in the Email if you Wantmsg.addHeader("MyHeaderName", "myHeaderValue");// Setting the Subject and Content Typemsg.setSubject(subject);msg.setContent(message, "text/plain");Transport.send(msg);}
public class Factorial{public static void main(String[] args){ final int NUM_FACTS = 100;for(int i = 0; i < NUM_FACTS; i++)System.out.println( i + "! is " + factorial(i));}public static int factorial(int n){ int result = 1;for(int i = 2; i <= n; i++)result *= i;return result;}}