Skip to main content

longest sequence

0 likes • Oct 7, 2023 • 5 views
Java
Loading...

More Java Posts

Heap sort

0 likes • Nov 19, 2022 • 0 views
Java
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 child
if(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));
}
}

LeetCode #21: Merge Two Sorted Lists

0 likes • Oct 15, 2022 • 0 views
Java
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode handler = head;
while(l1 != null && l2 != null) {
if (l1.val <= l2.val) {
handler.next = l1;
l1 = l1.next;
} else {
handler.next = l2;
l2 = l2.next;
}
handler = handler.next;
}
if (l1 != null) {
handler.next = l1;
} else if (l2 != null) {
handler.next = l2;
}
return head.next;
}
}

merge sort

0 likes • Nov 18, 2022 • 0 views
Java
import static java.lang.System.*;
import java.util.Arrays;
public class MergeSort{
private static int passCount;
public static void mergeSort(int[] list)
{
passCount=0;
mergeSort(list, 0, list.length);
}
private static void mergeSort(int[] list, int front, int back) //O( Log N )
{
int mid = (front+back)/2;
if(mid==front) return;
mergeSort(list, front, mid);
mergeSort(list, mid, back);
merge(list, front, back);
}
private static void merge(int[] list, int front, int back) //O(N)
{
int dif = back-front;
int[] temp = new int[dif];
int beg = front, mid = (front+back)/2;
int saveMid = mid;
int spot = 0;
while(beg < saveMid && mid < back) {
if(list[beg] < list[mid]) {
temp[spot++] = list[beg++];
} else {
temp[spot++] = list[mid++];
}
}
while(beg < saveMid)
temp[spot++] = list[beg++];
while(mid < back)
temp[spot++] = list[mid++];
for(int i = 0; i < back-front; i++) {
list[front+i] = temp[i];
}
System.out.println("pass " + passCount++ + " " + Arrays.toString(list) + "\n");
}
public static void main(String args[])
{
mergeSort(new Comparable[]{ 9, 5, 3, 2 });
System.out.println("\n");
mergeSort(new Comparable[]{ 19, 52, 3, 2, 7, 21 });
System.out.println("\n");
mergeSort(new Comparable[]{ 68, 66, 11, 2, 42, 31});
System.out.println("\n");
}
}

Me

0 likes • Feb 6, 2021 • 0 views
Java
public class Daedalus extends Athenian
{
private Story story;
public ArrayList<Skill> skills = new ArrayList<Skill>();
public ArrayList<Athenian> children = new ArrayList<Athenian>();
public Daedalus(Story story, ArrayList<Skill> inherentSkills)
{
if(story != null)
System.out.println("ERROR No one is created with a story.");
skills.add(inherentSkills);
}
public Momento advanceStory(Scene s)
{
System.err.println("ERROR Don't know how to proceed...");
}
}
//██╗  ░█████╗░███╗░░░███╗  ██████╗░░█████╗░███████╗██████╗░░█████╗░██╗░░░░░██╗░░░██╗░██████╗
//██║  ██╔══██╗████╗░████║  ██╔══██╗██╔══██╗██╔════╝██╔══██╗██╔══██╗██║░░░░░██║░░░██║██╔════╝
//██║  ███████║██╔████╔██║  ██║░░██║███████║█████╗░░██║░░██║███████║██║░░░░░██║░░░██║╚█████╗░
//██║  ██╔══██║██║╚██╔╝██║  ██║░░██║██╔══██║██╔══╝░░██║░░██║██╔══██║██║░░░░░██║░░░██║░╚═══██╗
//██║  ██║░░██║██║░╚═╝░██║  ██████╔╝██║░░██║███████╗██████╔╝██║░░██║███████╗╚██████╔╝██████╔╝
//╚═╝  ╚═╝░░╚═╝╚═╝░░░░░╚═╝  ╚═════╝░╚═╝░░╚═╝╚══════╝╚═════╝░╚═╝░░╚═╝╚══════╝░╚═════╝░╚═════╝░

Add Binary Numbers

0 likes • Oct 15, 2022 • 0 views
Java
public static String addBinary(){
// The two input Strings, containing the binary representation of the two values:
String input0 = "1010";
String input1 = "10";
// Use as radix 2 because it's binary
int number0 = Integer.parseInt(input0, 2);
int number1 = Integer.parseInt(input1, 2);
int sum = number0 + number1;
return Integer.toBinaryString(sum); //returns the answer as a binary value;
}

Random Integers -> file

0 likes • Nov 19, 2022 • 0 views
Java
//sample code to write 100 random ints to a file, 1 per line
import java.io.PrintStream;
import java.io.IOException;
import java.io.File;
import java.util.Random;
public class WriteToFile {
public static void main(String[] args) {
try {
PrintStream writer = new PrintStream(new File("randInts.txt"));
Random r = new Random();
final int LIMIT = 100;
for (int i = 0; i < LIMIT; i++) {
writer.println(r.nextInt());
}
writer.close();
} catch (IOException e) {
System.out.println("An error occured while trying to write to the file");
}
}
}