Question

In: Computer Science

java You are creating an array where each element is an (age, name) pair representing guests...

java

You are creating an array where each element is an (age, name) pair representing guests at a dinner Party.
You have been asked to print each guest at the party in ascending order of their ages, but if more than one
guests have the same age, only the one who appears first in the array should be printed.
Example input:
(23, Matt)(2000, jack)(50, Sal)(47, Mark)(23, will)(83200, Andrew)(23, Lee)(47, Andy)(47, Sam)(150, Dayton)

Example output:

(23, Matt) (47, Mark) (50, Sal (150, Dayton) (2000, Jack) (83200, Andrew)

Read all your party goers from a file. That is passed through the command line.

In your readme:
Describe a solution that takes O (1) space in addition to the provided input array. Your algorithm may modify the input array.
This party has some very old guests and your solution should still work correctly for parties that have even older guests, so your algorithm can’t assume the maximum age of the partygoers.
Give the worst-case tight-O time complexity of your solution.

I have been working on this problem but have been struggling with reading the file and
not getting errors such as no element found.

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

GUESTS.TXT

(23, Matt)
(2000, jack)
(50, Sal)
(47, Mark)
(23, will)
(83200, Andrew)
(23, Lee)
(47, Andy)
(47, Sam)
(150, Dayton)

=============================

SortGuests.java

import java.io.File;
import java.util.Scanner;
public class SortGuests
{
    // assume maximum 100 people will come
    private static final int MAX_NUM =100 ;

    public static void main(String[] args) throws Exception {
        // pass the path to the file as a parameter
        File file = new File("/home/praveen_kumar/hu16-java-leavetracker/dummy/src/guests.txt");
        Scanner sc = new Scanner(file);

        // declare two arrays
        int[] age=new int[MAX_NUM];
        String[] names=new String[MAX_NUM];

        // read line by line into two parallel arrays
        int count = 0;
        while (sc.hasNextLine())
        {
            String data= sc.nextLine();
            age[count] = Integer.parseInt(data.split(",")[0].substring(1));
            String temp =data.split(" ")[1];
            names[count] = temp.substring(0,temp.length()-1);
            count++;
        }
        // Now we have the data in separate arrays
        for(int i=0;i<count-1;i++)
        {
            for(int j=0;j<count-i-1;j++)
            {
                if(age[j]>age[j+1])
                {
                    //swap ages
                    int temp = age[j];
                    age[j]=age[j+1];
                    age[j+1]=temp;

                    // swap names
                    String tempName = names[j];
                    names[j]=names[j+1];
                    names[j+1]=tempName;
                }
            }
        }
        System.out.println("\n\nThe Sorted Data is: ");
        // print the first guest
        System.out.print("("+age[0]+", "+names[0]+") ");

        // print only non-repeated guests ages
        for(int i=1;i<count;i++)
        {
            if(age[i]!=age[i-1]) //check if repeated
                System.out.print("("+age[i]+", "+names[i]+") ");
        }
    }
}

======================

SCREENSHOT:

FILE:

OUTPUT:

  1. Here we are using the arrays used to store the given data. We are not using any other arrays. We are just replacing the values as in swap(). Hence it is O(1) space complexity
  2. We are not assuming any age of the guests. it will work for any age.
  3. We are using the Bubble Sort here. So, the time complexity is O(n2) in the worst case as we are using two loops.


Related Solutions

Using Java, Given an array A[0 ... n-1], where each element of the array represent a...
Using Java, Given an array A[0 ... n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.
10) Create a java program that will ask for name and age with method age where...
10) Create a java program that will ask for name and age with method age where it will check the condition that id you are greater than 18 you can vote.
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a...
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a vote in the election. Assume that each vote is given as integers representing the ID of the chosen candidate. Write the code determining who wins the election. Problem 2: How do we find the number which appeared maximum number of times in an array? ( Use Java and an original code )
Input: An array of non-negative integers, where each element in the array represents your maximum jump...
Input: An array of non-negative integers, where each element in the array represents your maximum jump length at that position. Output: A boolean value if you are able to reach the last index starting if you start at the first spot in the array. [Please write a recursion function!] Example 1: Input: [2,4,1,2,4,1] Output: True (Ex. 0 to 1 to 5 or 0 to 2 to 3 to 5) Example 2: Input: [3,2,1,0,4] Output: false (You will always arrive at,...
Input: An array of non-negative integers, where each element in the array represents your maximum jump...
Input: An array of non-negative integers, where each element in the array represents your maximum jump length at that position. Output: A boolean value if you are able to reach the last index starting if you start at the first spot in the array. Example 1: Input: [2,4,1,2,4,1] Output: True (Ex. 0 to 1 to 5 or 0 to 2 to 3 to 5) Example 2: Input: [3,2,1,0,4] Output: false (You will always arrive at, and get stuck at, index...
Write a Java program that creates a three-dimensional array. Populate each element with a string that...
Write a Java program that creates a three-dimensional array. Populate each element with a string that states each coordinate position in the array.
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
In Java Please: * posOfLargestElementLtOeT returns the position of the largest element in the array that...
In Java Please: * posOfLargestElementLtOeT returns the position of the largest element in the array that is * less than or equal to the limit parameter * if all values are greater than theVal, return -1; * * Precondition: the array is nonempty and all elements are unique. * Your solution must go through the array exactly once. * * <pre> * 0 == posOfLargestElementLtOeT(3, new double[] { -7 }) // value:-7 is in pos 0 * 5 == posOfLargestElementLtOeT(3,...
You need a data structure that contains fields for name (char array), color (char array), age...
You need a data structure that contains fields for name (char array), color (char array), age (int), and height (int). Name the struct Info when you define it. Create a function named getUserInfo() that asks the user for name, color, age, and height and then returns a data structure containing that information. It should do the following: What is your name? George What is your favorite color? Green What is your age? 111 What is your height in inches? 72...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT