Question

Create a program in java to find the 3rd largest and 3rd smallest element in a...

Create a program in java to find the 3rd largest and 3rd smallest element in a single dimension array (Input 10 elements) after arranging them in ascending and descending order separately.

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

import java.util.Scanner;

public class LargestSmallest {
   public static void sortAsc(int arr[], int n) {
       int i, j, temp;
       // outer loop to travel through the all elements
       for (i = 0; i < n - 1; i++) {
           // inner loop to compare the outer loop elements
           for (j = 0; j < n - i - 1; j++)
               // if element at j< than j+1 than swap both
               if (arr[j] > arr[j + 1]) {
                   // swap logic
                   temp = arr[j];
                   arr[j] = arr[j + 1];
                   arr[j + 1] = temp;
               }
       }

   }

   public static void sortDsc(int arr[], int n) {
       int i, j, temp;
       // outer loop to travel through the all elements
       for (i = 0; i < n - 1; i++) {
           // inner loop to compare the outer loop elements
           for (j = 0; j < n - i - 1; j++)
               // if element at j< than j+1 than swap both
               if (arr[j] < arr[j + 1]) {
                   // swap logic
                   temp = arr[j];
                   arr[j] = arr[j + 1];
                   arr[j + 1] = temp;
               }
       }

   }

   public static void main(String[] args) {
       int arr[] = new int[10];
       System.out.println("Enter 10 elements: ");
       Scanner sc = new Scanner(System.in);
       for (int i = 0; i < 10; i++)
           arr[i] = sc.nextInt();
       sortDsc(arr, arr.length);
       System.out.println("3rd largest element: " + arr[2]);
       sortAsc(arr, arr.length);
       System.out.println("3rd smallest element: " + arr[2]);
       sc.close();

   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
Create a program in java to find the 3rd largest and 3rd smallest element in a...
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
  • Q2. Write programs in Java as follows: a. Get user input for the size and eleme...

    Q2. Write programs in Java as follows: a. Get user input for the size and eleme innut for the size and elements of arr10 and arr2( which are of integer and double type nain(). Find the second smallest and second largest element in both arrays in sort and sort20) after arranging them in increasing order simultaneously. Find the difference of elements found in sort1() and sort2() separately and display them in main() ...8 Marks

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

  • Write a JAVA program to sort a given array of integers (1 Dimensional) in ascending order...

    Write a JAVA program to sort a given array of integers (1 Dimensional) in ascending order (from smallest to largest). You can either get the array as input or hardcode it inside your program.

  • Write a C++ program that will create an array with a given number of elements. Use...

    Write a C++ program that will create an array with a given number of elements. Use size = 10 for demonstration purposes, but write the program where the size of the array can be changed by changing the value of one constant. Fill the array with random numbers between 0 and 100. Write the program to perform the following operations: 1. Find and display the largest value in the array. 2. Find and display the smallest value in the array....

  • Write a Java program that will create an array of Strings with 25 elements. Input Strings...

    Write a Java program that will create an array of Strings with 25 elements. Input Strings from the user and store them in the array until either the user enters “quit” or the array is full. HINT: the sentinel value is “quit” all lowercase. “Quit” or “QUIT” should not stop the loop. HINT: you have to use the equals method (not ==) when you are comparing two Strings. After the input is complete, print the Strings that the user entered...

  • Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values,...

    Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values, of same size         b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....

  • can someone please help me with this. I need to use java. Recursion Write and run...

    can someone please help me with this. I need to use java. Recursion Write and run a program that reads in a set of numbers and stores them in a sequential array. It then calls a recursive function to sort the elements of the array in ascending order. When the sorting is done, the main function prints out the elements of the newly sorted array Hint: How do you sort an array recursively? Place the smallest element of the array...

  • Create a program called Merge.java that implements a variety of the Merge function (in the Merge...

    Create a program called Merge.java that implements a variety of the Merge function (in the Merge Sort algorithm). The program should be able to do the following: accepts two command line parameters, each specifies a text file containing the integers to be merged. The structure of the files is as follows: For both files, there will be multiple lines, each of which contains one integer. The number of lines is unknown. reads the integers from the text files into two...

  • Using Java In this assignment we are working with arrays. You have to ask the user...

    Using Java In this assignment we are working with arrays. You have to ask the user to enter the size of the integer array to be declared and then initialize this array randomly. Then use a menu to select from the following Modify the elements of the array. At any point in the program, if you select this option you are modifying the elements of the array randomly. Print the items in the array, one to a line with their...

  • Must use JOPTIONPANE. Using arrays and methods create a JAVA program. Create a java program that...

    Must use JOPTIONPANE. Using arrays and methods create a JAVA program. Create a java program that holds an array for the integer values 1-10. Pass the array to a method that will calculate the values to the power of 2 of each element in the array. Then return the list of calculated values back to the main method and print the values

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