Create an ArrayListReview class with one generic type to do the
following
• Creates an array list filled with the generic type of the
ArrayListReview
class, and inserts new elements into the specified location index-i
in the list.
(5 points)
• Create a method inside the class to implement the calculation of
Fibonacci
numbers. Use System.nanoTime to find out how much time it takes to
get
fab(50). Create a method inside the class to check if an array list
is a
palindrome, assuming each element in the array list is one
character. (5
points)
• Implement mergeSort using a list and recursion. (5 points)
• Implement insertionSort either recursively or non-recursively.
Implement
mergeSort using recursion. (5 points)
• Use System.nanoTime() to find out the speed of your program.
(required
for each step)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Is This Code Correct?
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.Arrays;
public class ArrayListReview {
public static void fibonacciSeries(int n) {
System.out.println("\n\nFibonacci Series: ");
int first = 0;
int second = 1;
System.out.print(first + " " + second);
for(int i=3; i<=n; i++) {
int third = first + second;
System.out.print(" " + third);
first = second;
second = third;
}
}
public static boolean isPalindrome(ArrayList<Character> list) {
int len = list.size();
for(int i = 0, j = len - 1; i < len/2; i++, j--) {
if(list.get(i) != list.get(j))
return false;
}
return true;
}
public static void merge(int arr[], int left[], int right[]) {
int i = 0;
int j = 0;
int k = 0;
while(i < left.length && j < right.length) {
if(left[i] < right[j]) {
arr[k++] = left[i];
i++;
} else {
arr[k++] = right[j];
j++;
}
}
while(i < left.length) {
arr[k++] = left[i];
i++;
}
while(j < right.length) {
arr[k++] = right[j];
j++;
}
}
public static int[] mergeSort(int arr[]) {
int n = arr.length;
int[] left = new int[n/2];
int[] right = new int[n-n/2];
if( n < 2) {
return arr;
}
int mid = n/2, k = 0;
for(int i=0; i<mid; i++) {
left[k++] = arr[i];
}
k = 0;
for(int i=mid; i<n; i++) {
right[k++] = arr[i];
}
left = mergeSort(left);
right = mergeSort(right);
merge(arr, left,right);
return arr;
}
}
ArrayListReview.java
import java.util.ArrayList;
import java.util.Arrays;
public class ArrayListReview<T>/* create generic type
class*/ {
private T t;
ArrayList<ArrayListReview<T>>
arrayList=new ArrayList<>();
//constructor
public ArrayListReview(T t) {
this.t = t;
}
//default constructor
public ArrayListReview() {
}
void insertIntoARraylist(T data,int index){
arrayList.add(index,new
ArrayListReview<>(data));
System.out.println("Data "+data+"
inserted at index "+index);
}
public static void fibonacciSeries(int n) {
long nano_startTime =
System.nanoTime();
System.out.println("\n\nFibonacci Series: ");
//use long data type to allow bigger values calculation
long first = 0;
long second = 1;
System.out.print(first + " " + second);
for(int i=3; i<=n; i++) {
long third = first + second;
System.out.print(" " + third);
first = second;
second = third;
}
long nano_endTime = System.nanoTime();
System.out.println("\nfibonacci start time
"+nano_startTime+" fibonacci end time "+nano_endTime);
}
public static boolean isPalindrome(ArrayList<Character> list) {
int len = list.size();
for(int i = 0, j = len - 1; i < len/2; i++, j--) {
if(list.get(i) != list.get(j))
return false;
}
return true;
}
public static void merge(int arr[], int left[], int right[]) {
int i = 0;
int j = 0;
int k = 0;
while(i < left.length && j < right.length) {
if(left[i] < right[j]) {
arr[k++] = left[i];
i++;
} else {
arr[k++] = right[j];
j++;
}
}
while(i < left.length) {
arr[k++] = left[i];
i++;
}
while(j < right.length) {
arr[k++] = right[j];
j++;
}
}
public static int[] mergeSort(int arr[]) {
int n = arr.length;
int[] left = new int[n/2];
int[] right = new int[n-n/2];
if( n < 2) {
return arr;
}
int mid = n/2, k = 0;
for(int i=0; i<mid; i++) {
left[k++] = arr[i];
}
k = 0;
for(int i=mid; i<n; i++) {
right[k++] = arr[i];
}
left = mergeSort(left);
right = mergeSort(right);
merge(arr, left,right);
return arr;
}
//insertion sort
int[] insertionSort(int arr[])
{
int n = arr.length;
for (int i=1; i<n; ++i)
{
int key = arr[i];
int j = i-1;
/* Move elements of
array arr[0..i-1], that are
greater than key, to one position ahead
of
their current position */
while (j>=0
&& arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
return arr;
}
}
Main.java
import java.awt.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayListReview<Integer>
arrayListReview=new ArrayListReview<>();
arrayListReview.insertIntoARraylist(23, 0);//insert integer type
data
ArrayListReview<String>
arrayListReview1=new ArrayListReview<>();
arrayListReview1.insertIntoARraylist("text", 0);//insert string
type data
ArrayListReview.fibonacciSeries(50);
ArrayList arrayList=new
ArrayList<>();
arrayList.add('a');
arrayList.add('b');
arrayList.add('a');
System.out.println("arrayList
"+arrayList+" is plalindrome?
"+ArrayListReview.isPalindrome(arrayList));;
int arr[]=
{2,6,5,20,12,45,84};
System.out.println("using merge
sort : sorted array is ");
for(int i=0;i<arr.length;i++)
{
System.out.print(" "+ArrayListReview.mergeSort(arr)[i]);
}
System.out.println("\nusing
insertion sort : sorted array is ");
for(int i=0;i<arr.length;i++)
{
System.out.print(" "+ArrayListReview.mergeSort(arr)[i]);
}
}
}
Output
Data "23" inserted at index 0
Data "text" inserted at index 0
Fibonacci Series:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
6765 10946 17711 28657 46368 75025 121393 196418 317811 514229
832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817
39088169 63245986 102334155 165580141 267914296 433494437 701408733
1134903170 1836311903 2971215073 4807526976 7778742049
fibonacci start time 9028572577483 fibonacci end time
9028599486506
arrayList [a, b, a] is plalindrome? true
using merge sort : sorted array is
2 5 6 12 20 45 84
using insertion sort : sorted array is
2 5 6 12 20 45 84
Create an ArrayListReview class with one generic type to do the following • Creates an array...
use the same code. but the code needs some modifications. so use this same code and modify it and provide a output Java Program to Implement Merge Sort import java.util.Scanner Class MergeSort public class MergeSort Merge Sort function / public static yoid sortfintfl a, int low, int high) int N-high-low; if (N1) return; int mid- low +N/2; Il recursively sort sort(a, low, mid); sort(a, mid, high); I/ merge two sorted subarrays int] temp new int[N]; int i- low, j-mid; for...
Write merge method for mergeSort method, it takes the array of data, starting index of first half, starting index of second half and end index of second half. please use the code below to test it public class A4Sort{ public static void mergeSort(int[] a, int l, int h){ if (l >= h) return; int mid = (h + l) / 2; mergeSort(a, l, mid); mergeSort(a, mid + 1, h); merge(a, l, mid + 1, h); } public static void main(String[]...
Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) { int i, j, k, c[100000]; i = low; k = low; j = mid + 1; while (i <= mid && j <= high) { if (a[i] < a[j]) { c[k] = a[i]; k++; i++; } else { ...
I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...
need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...
Create a java class that user put two inputs and first input generate n length array of randomly generated numbers. and the second input changes that number of multiples in the array into zero. for example, if the user puts 3 and 5 then it generates 34 60 10 and since the second input is 5 then the multiple of 5 eliminates so it generates 34 0 0 here is the main method that I'm going to use class Main...
In this assignment you will implement merge-sort algorithm by creating 2 threads instead of 2 processes. First half of the array will be sorted by thread 1 and the second half by thread 2. When the threads complete their tasks, the main program will merge the half arrays. You should not waste your time on merge-sort algorithm. Use the merge sort algorithm given below void mergesort(int a[],int i,int j) { int mid; if(i<j) { mid=(i+j)/2; mergesort(a,i,mid); //left recursion mergesort(a,mid+1,j); //right...
Add reverse() method which reverses the content of array without using additional array. rotate(k) method which rotates left the content of array without using additional array by k elements. import java.util.*; * Implementation of the ADT List using a fixed-length array. * * if insert is successful returns 1, otherwise 0; * for successful insertion: * list should not be full and p should be valid. * * if delete is successful returns 1, otherwise 0; * for successful deletion:...
USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list and use a Merge Sort to sort the object by age. Create a class called Person : name and age Create methods that add, and delete Person from the link list Create a method that sorts the persons' objects by age. package mergesort; public class MergeSortExample { private static Comparable[] aux; // auxiliary array for merges public static void sort(Comparable[] a) { aux =...
I'm writing this class called CharArrayProject_3 that removes repeating elements from an array of chars. However, when I run the driver class, it just outputs two sets of dashed lines. What am I getting wrong? Is it the deleteRepeats() method?: public class CharArrayProject_3 { private char[] array; privateint length; privateintnumberOfRepeats; public CharArrayProject_3( char[] arr ) { length = arr.length; array = new char[ length ]; numberOfRepeats = 0; for( int k = 0; k < arr.length; k++ ) { array[k]...