FRQ 3
Data Types in Java
- used to define the type of data that a variable can hold
- main types of data types
- Primitive data types
- byte: 8-bit signed integer (-128 to 127
- short: 16-bit signed integer (-32,768 to 32,767) int: 32-bit signed integer (-2,147,483,648 to 2,147,483,647) long: 64-bit signed integer (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807) float: 32-bit floating-point number (1.4e-45 to 3.4e+38) double: 64-bit floating-point number (4.9e-324 to 1.8e+308) boolean: true or false char: 16-bit Unicode character (0 to 65,535)
import java.util.Scanner;
public class Binary {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.println("Enter The First and Second Numbers = ");
long b1 = sc.nextLong();
long b2 = sc.nextLong();
int i, carry = 0;
int[] sum = new int[10];
for(i = 0; b1 != 0 || b2 != 0; b1 /= 10, b2 /= 10)
{
sum[i++] = (int)((b1 % 10 + b2 % 10 + carry) % 2);
carry = (int)((b1 % 10 + b2 % 10 + carry) / 2);
}
if(carry != 0)
{
sum[i++] = carry;
}
--i;
System.out.print("\nThe Sum = ");
while(i >= 0)
{
System.out.print(sum[i--]);
}
System.out.println();
}
}
public static void main twoBinary
{
Integer.toBinaryString(sum);
}
Sorting
Selection sort
- comparison based
- going to the inner loops and looking for the smallest number and then swapping it into the position
- n^2 because it is doing a i and j loop
- could be a no swap condition if it doesn't find the smallest one ## Insertion Sort
- linear algorithm
- sorts elements from index [0] to index [n-1]
- finds the gap with the inner loops and the insertion point for the next item and inserts it
- each inner loop leave the list partially sorted according to outer loop index
- complexity of the algorithm: n^2 ## Nerge Sort
- splitting - divides and conquers unlike the linear algorithms of insertion or selection
- divides the two different groups recurisvely until it gets only 2 to compare, swaps if necessary.
- finally puts each split in the one group
- Big O complexity: nlong(n)