Question

USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list...

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 = new Comparable[a.length]; // Allocate space just once.
sort(a, 0, a.length - 1);
}
private static void sort(Comparable[] a, int lo, int hi)
{ // Sort a[lo..hi].
if (hi <= lo)
return;
int mid = lo + (hi - lo)/2;
sort(a, lo, mid); // Sort left half.
sort(a, mid+1, hi); // Sort right half.
merge(a, lo, mid, hi); // Merge results (code on page 271).
}

public static void merge(Comparable[] a, int lo, int mid, int hi)
{ // Merge a[lo..mid] with a[mid+1..hi].
int i = lo, j = mid+1;
for (int k = lo; k <= hi; k++) // Copy a[lo..hi] to aux[lo..hi].
aux[k] = a[k];
for (int k = lo; k <= hi; k++) // Merge back to a[lo..hi].
if (i > mid)
a[k] = aux[j++];
else if (j > hi )
a[k] = aux[i++];
else if (less(aux[j], aux[i]))
a[k] = aux[j++];
else
a[k] = aux[i++];
}

private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}

/***************************************************************************
* Check if array is sorted - useful for debugging.
***********************************************************************/
private static boolean isSorted(Comparable[] a) {
return isSorted(a, 0, a.length - 1);
}

private static boolean isSorted(Comparable[] a, int lo, int hi) {
for (int i = lo + 1; i <= hi; i++)
if (less(a[i], a[i-1])) return false;
return true;
}

// print array to standard output
private static void show(Comparable[] a) {
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}

public static void main(String[] args) {
String [] fields = {"D","A","C","B","A"};
sort(fields);
show(fields);
}





}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

import java.util.*;
import java.lang.*;
import java.io.*;

class Person
{
static Node head;
  
  
static class Node {
String name;
int age;
Node prev;
Node next;
  
Node(String n, int a) { name=n; age = a; }
}
  
public void add(String n, int a)
{
   Node new_node = new Node(n,a);
  
   Node last = head;
  
   new_node.next = null;
  
   if (head == null) {
       new_node.prev = null;
       head = new_node;
       return;
   }
  
   while (last.next != null)
       last = last.next;
  
   last.next = new_node;
   new_node.prev = last;
}

Node split(Node head) {
Node fast = head, slow = head;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
Node temp = slow.next;
slow.next = null;
return temp;
}
  
Node mergeSort(Node node) {
if (node == null || node.next == null) {
return node;
}
Node second = split(node);
  
node = mergeSort(node);
second = mergeSort(second);
  
return merge(node, second);
}
  
Node merge(Node first, Node second) {
if (first == null) {
return second;
}
if (second == null) {
return first;
}
  
if (first.age < second.age) {
first.next = merge(first.next, second);
first.next.prev = first;
first.prev = null;
return first;
} else {
second.next = merge(first, second.next);
second.next.prev = second;
second.prev = null;
return second;
}
}
void print(Node node) {
Node temp = node;
System.out.println("Forward Traversal using next pointer");
while (node != null) {
System.out.println(node.age + " " + node.name);
temp = node;
node = node.next;
}
}
  
   public static void main (String[] args) throws java.lang.Exception
   {
       // write main code here
       Person list= new Person();
       list.head = new Node("xyz",10);
list.head.next = new Node("pqr",30);
list.head.next.next = new Node("amaaa",3);
list.head.next.next.next = new Node("abc",4);
list.head.next.next.next.next = new Node("answer",20);
list.head.next.next.next.next.next = new Node("sjj",5);
Node node = null;
node = list.mergeSort(head);
System.out.println("Linked list after sorting :");
list.print(node);
   }
}

SAMPLE OUTPUT:

Linked list after sorting :
Forward Traversal using next pointer
3 amaaa
4 abc
5 sjj
10 xyz
20 answer
30 pqr

Add a comment
Know the answer?
Add Answer to:
USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 4. (5 Points) The following is another merge sort top down implementation, what is the running...

    4. (5 Points) The following is another merge sort top down implementation, what is the running time and space complexity for this implementation in big-0? Briefly explain your answer. public static 〈T extends Comparable〈 T> > void sort2(T[] a) { sort2(a, , a.length - 1); @Suppresswarnings ("unchecked") private static <T extends Comparable<T>> void sort2(ΤΠ a, intlo, int hi) { if (hi (z lo) return; T[] aux- (TLD new comparable[a,length]; int mid- (10 + hi) / 2; sort2 (a, lo, mid);...

  • A4: Merge Sort - Reverse Hide Assignment Information Instructions void merge (int al], int aux[], int...

    A4: Merge Sort - Reverse Hide Assignment Information Instructions void merge (int al], int aux[], int lo, int mid, int hi) { for (int k = lo; k <= hi; k++) aux [k] = a[k]; int i = lo, j = mid+1; for (int k = lo; k <= hi; k++) if (i > mid) a[k] = aux [j++]; else if (i > hi) a[k] = aux[i++) ; else if (less (aux(j), aux[i])) a[k] = aux [j++); else a[k] =...

  • A4: Merge Sort - Reverse Hide Assignment Information Instructions void merge (int a[], int aux[], int...

    A4: Merge Sort - Reverse Hide Assignment Information Instructions void merge (int a[], int aux[], int lo, int mid, int hi) for (int k = lo; k <= hi; k++) aux[k] = a[k]; int i = lo, j = mid+1; for (int k = lo; k <= hi; k++) { if (i > mid) a[k] = aux [j++); else if (j > hi) a[k] = aux[i++]; else if (less (aux()], aux[i])) a[k] = aux [j++]; else a[k] = aux[i++]; The...

  • A4: Merge Sort - Reverse - Due Jul 30, 2020 11:59 PM Summer II 2020 -...

    A4: Merge Sort - Reverse - Due Jul 30, 2020 11:59 PM Summer II 2020 - Data Structures and Algorithms (COSC-2336-01W) void merge (int a[], int aux[], int lo, int mid, int hi) { for (int k = lo; k <= hi; k++) aux[k] = a[k]; int i = lo, j mid+1; for (int k = lo; k <= hi; k++) { if (i > mid) a[k] = aux [j++]; else if (j > hi) a[k] = aux[i++] ; else...

  • private static void sort(Comparable[] a, int lo, int hi) { if (hi <= lo) return; int...

    private static void sort(Comparable[] a, int lo, int hi) { if (hi <= lo) return; int j = partition(a, lo, hi); show(a, j, lo, hi); sort(a, lo, j-1); sort(a, j+1, hi); assert issorted(a, lo, hi); } Give the code fragment above, what type of sort is this? Insertion sort Bubble sort Quicksort Mergesort

  • Please merge all the codes below and add comments using JAVA Program. I need a complete...

    Please merge all the codes below and add comments using JAVA Program. I need a complete code which is the combination of the following codes: // Merges the left/right elements into a sorted result. // Precondition: left/right are sorted public static void merge(int[] result, int[] left,                                        int[] right) {     int i1 = 0;   // index into left array     int i2 = 0;   // index into right array     for (int i = 0; i < result.length; i++)...

  • use the same code. but the code needs some modifications. so use this same code and...

    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...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    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...

  • Hey i got the program to work but i cant get the merge sorter from big...

    Hey i got the program to work but i cant get the merge sorter from big to little import java.util.*; class MergeSorter {    public static void sort(int[] a)    { if (a.length <= 1) { return; } int[] first = new int[a.length / 2]; int[] second = new int[a.length - first.length]; for (int i = 0; i < first.length; i++) {    first[i] = a[i]; } for (int i = 0; i < second.length; i++) {    second[i] =...

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT