1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
mixas84 [53]
3 years ago
10

[10] Create a program called SelectionSort.java that implements the Selection Sort algorithm (The Art of Computer Programming -

Donald Knuth). The algorithm is as follows: The program should be able to do the following: accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated by a single whitespace. reads the integers from the text file in part a into an array of integers. sort the integers in ascending order, and then prints out a sorted version of these integers, one per line. The implementation should follow the given the pseudo code/algorithm description.
Engineering
1 answer:
Alex787 [66]3 years ago
8 0

Answer:

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Scanner;

public class SelectionSort {

  public static void main(String[] args) throws FileNotFoundException {

      //For array

      ArrayList<Integer>array=new ArrayList<Integer>();

      //If argument found

      if(args.length>=1) {

          //File path

          Scanner sc=new Scanner(new File(args[0]));

          //Loop until end

          while(sc.hasNextLine()) {

              //Read each line and add into array

              String[] temp=sc.nextLine().split(" ");

              for(int i=0;i<temp.length;i++) {

                  array.add(Integer.parseInt(temp[i]));

              }

          }

          //Display array

          System.out.println("Display array: ");

          printArray(array);

          System.out.println("\nDisplay array after sort: ");

          sortArray(array);

          printArray(array);

      }

      //If argument not found

      else {

          System.out.println("Argument not found!!!");

      }

  }

  //Method to print array

  public static void printArray(ArrayList<Integer>array) {

      for(int i=0;i<array.size();i++) {

          System.out.println(array.get(i));

      }

  }

  //Method to sort array using straight selection sort

  public static void sortArray(ArrayList<Integer>array) {

      //Step1

      for(int j=array.size()-1;j>=1;j--) {

          int max=array.get(j);

          int index=j;

          //Step2

          for(int k=j;k>=0;k--) {

              if(max<array.get(k)) {

                  max=array.get(k);

                  index=k;

              }

          }

          //Step3

          array.set(index,array.get(j));

          array.set(j,max);

      }

  }

}

Explanation:

You might be interested in
What did the Spanish have that the Aztecs didn’t
notsponge [240]

Answer:

The Spanish brought chickenpox, smallpox, measles, mumps, and rubella to the new world. These diseases killed 75% of the Native Americans. ... The advantages that the Spanish had over the Aztecs were 16 horses, guns, armor, formed alliances, and diseases, steel.

Explanation:

I hope this helped!

6 0
3 years ago
Does anybody know how to take a screenshot on a HP pavilion computer?
Setler79 [48]

Answer:

I do i do it everyday

Explanation:

Press windows and prt sc at the same time

6 0
2 years ago
Calculate the unit cell edge length for an 80 wt% Ag−20 wt% Pd alloy. All of the palladium is in solid solution, the crystal str
alisha [4.7K]

Answer:

λ^3 = 4.37

Explanation:

first let us to calculate the average density of the alloy

for simplicity of calculation assume a 100g alloy

80g --> Ag

20g --> Pd

ρ_avg= 100/(20/ρ_Pd+80/ρ_avg)

         = 100*10^-3/(20/11.9*10^6+80/10.44*10^6)

         = 10744.62 kg/m^3

now Ag forms FCC and Pd is the impurity in one unit cell there is 4 atoms of Ag since Pd is the impurity we can not how many atom of Pd in one unit cell let us calculate

total no of unit cell in 100g of allow = 80 g/4*107.87*1.66*10^-27

                                                          = 1.12*10^23 unit cells

mass of Pd in 1 unit cell = 20/1.12*10^23

Now,

                      ρ_avg= mass of unit cell/volume of unit cell

                      ρ_avg= (4*107.87*1.66*10^-27+20/1.12*10^23)/λ^3

                          λ^3 = 4.37

6 0
3 years ago
8. A voltmeter measures a voltage drop of 600 V across a heating element while an
Alchen [17]

Answer:

How do I calculate voltage drop?

To calculate voltage drop, E, across a component, you need to know the resistance of the component and the current thru it. Ohm's Law is E=I⋅R , which tells us to then multiply I by R . E is the voltage across the component also known as voltage drop

Explanation:

5 0
2 years ago
Why do engineers play a variety of roles in the engineering process? They are part of a team. They need to act as both scientist
Kamila [148]

Answer: They showed that many people worked on the project and demonstrate how hard they worked.

Explanation:

4 0
3 years ago
Read 2 more answers
Other questions:
  • 5. Switch a in the circuit has been open for a long time and switch b has been closed for a long time. Switch a is closed at t =
    13·1 answer
  • At an auction of antiques, a bidder for a particular porcelain statue would be trying to ________(a) Maximize the difference bet
    14·1 answer
  • In Josiah Johnson Hawes and Albert Sands Southworth, Early Operation under Ether, Massachusetts General Hospital the elevated vi
    11·1 answer
  • You are given that kc = 10-1 kg eq-1 min-1, ku = 10-3 kg2 eq-2 min-1 and [A]0 = 10 eq kg-1, where kc is the rate constant for a
    15·1 answer
  • Consider a potato being baked in an oven that is maintained at a constant temperature. the temperature of the potato is observed
    14·1 answer
  • 14. The maximum amount a homeowner should spend on housing is
    11·1 answer
  • 15 POINTS! Help.
    9·2 answers
  • Problem 3.10 One/half million parts of a certain type are to be manufactured annually on dedicated production machines that run
    7·2 answers
  • Who was the American founder and leader of the Shakers in the 1770’s who advocated equality, individual responsibility, and peac
    11·2 answers
  • Instructions: For each problem, identify the appropriate test statistic to be use (t test or z-test). Then compute z or t value.
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!