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
gregori [183]
3 years ago
5

write a 〕ava program to sort a random list of 10 numbers entered b y user in an array using Selection Sort technique

Computers and Technology
1 answer:
Anton [14]3 years ago
5 0

Here is code in java to perform selection sort.

// import library

import java.util.*;

import java.lang.*;

import java.io.*;

class Solution

{

public static void main (String[] args) throws java.lang.Exception

{

   try{

     // creating object of Solution class

       Solution obj=new Solution();

    // creating object of Scanner class to read input

       Scanner sc=new Scanner(System.in);

    // create array of size 10

    int arr[]=new int[10];

       System.out.println("Please Enter 10 numbers :");

    // reading input from user

       for(int i=0;i<10;i++)

       {

           arr[i]=sc.nextInt();

       }

    // calling sort_fun with object "obj" to sort array

       obj.sort_fun(arr);

// printing the array after selection sort

 System.out.println("Array after performing selection sort:");

 for (int i=0; i<10; ++i)

  System.out.print(arr[i]+" ");

 System.out.println();

   }catch(Exception ex){

       return;}

}

// creating function to perform selection sort

public static void sort_fun(int arr[])

{

 for (int i = 0; i < 10-1; i++)

 {

  int m_idx = i;

  for (int j = i+1; j < 10; j++)

   if (arr[j] < arr[m_idx])

    m_idx = j;

    // find the minimum element and put them in the beginning

  int temp = arr[m_idx];

  arr[m_idx] = arr[i];

  arr[i] = temp;

 }

}

}

Explanation:

First create an object of "Solution" class. Then create an object of "Scanner" class to read input from user.After this create an array of size 10 of type integer and read 10 value from user in the array.In the sort_fun(),it will find the smallest element of unsorted array and put it in the beginning of the array. Then it find the second smallest number and put it after the first element. Similarly it will find the next element and put it after second element.When the loop end, the array will be sorted.

Output:

Please Enter 10 numbers :

45 34 12 90 84 1 4 56 12 6

Array after performing selection sort:

1 4 6 12 12 34 45 56 84 90

You might be interested in
Write an application that determines whether the first two files are located in the same folder as the third one. The program sh
marissa [1.9K]

Answer:

The program in Python is as follows:

fname1 = input("Filepath 1: ").lower()

fname2 = input("Filepath 2: ").lower()

fname3 = input("Filepath 3: ").lower()

f1dir = ""; f2dir = ""; f3dir = ""

fnamesplit = fname1.split("/")

for i in range(len(fnamesplit)-1):

   f1dir+=fnamesplit[i]+"/"

fnamesplit = fname2.split("/")

for i in range(len(fnamesplit)-1):

   f2dir+=fnamesplit[i]+"/"

fnamesplit = fname3.split("/")

for i in range(len(fnamesplit)-1):

   f3dir+=fnamesplit[i]+"/"

if f1dir == f2dir == f3dir:

   print("Files are in the same folder")

else:

   print("Files are in the different folder")

Explanation:

The next three lines get the file path of the three files

<em>fname1 = input("Filepath 1: ").lower()</em>

<em>fname2 = input("Filepath 2: ").lower()</em>

<em>fname3 = input("Filepath 3: ").lower()</em>

This initializes the directory of the three files to an empty string

f1dir = ""; f2dir = ""; f3dir = ""

This splits file name 1 by "/"

fnamesplit = fname1.split("/")

This iteration gets the directory of file 1 (leaving out the file name)

<em>for i in range(len(fnamesplit)-1):</em>

<em>    f1dir+=fnamesplit[i]+"/"</em>

This splits file name 2 by "/"

fnamesplit = fname2.split("/")

This iteration gets the directory of file 2 (leaving out the file name)

<em>for i in range(len(fnamesplit)-1):</em>

<em>    f2dir+=fnamesplit[i]+"/"</em>

This splits file name 3 by "/"

fnamesplit = fname3.split("/")

This iteration gets the directory of file 3 (leaving out the file name)

<em>for i in range(len(fnamesplit)-1):</em>

<em>    f3dir+=fnamesplit[i]+"/"</em>

This checks if the file directories hold the same value

This is executed, if yes

<em>if f1dir == f2dir == f3dir:</em>

<em>    print("Files are in the same folder")</em>

This is executed, if otherwise

<em>else:</em>

<em>    print("Files are in the different folder")</em>

5 0
3 years ago
What is the default method of referencing cells in formulas?
ELEN [110]
In Microsoft Excel, cell referencing is the method by which you refer to a cell or series of cells in a formula. Cell referencing is not important unless you plan to copy the formula to a number of other cells. In Excel, cell referencing is relative by default
7 0
3 years ago
What is imagined risk
lara31 [8.8K]
Hello there!

Imagined Risks are simply 'imaginary' risks that are created out of doubt/fear.

Hope This Helps You!
Good Luck :)
3 0
4 years ago
What type of maintenance can prevent the computer from breaking?
qwelly [4]
<span>The answer is, "Load anti-virus software".
</span>Anti-virus softwares are used to prevent computer from virus, as it name shows that are anti-virus means they prevent from virus. Anti virus softwares are also known as anti-malware software. These software work in such a way that they <span>prevent, detect and then remove viruses or </span><span>malicious software.</span>
3 0
3 years ago
Read 2 more answers
Graphic images can be stored in a variety of formats including ____.
Marrrta [24]
Graphic images can be stored in a variety of formats including JPEG and GIF. Hope this helped!
5 0
4 years ago
Other questions:
  • 3. If B3=6 and D5=8, what would the following function return? IF(B3&gt;D5, "Closed", D5-B3) *
    10·2 answers
  • How can you best utilize CSS for Web Development?
    6·1 answer
  • Array A is not a heap. Clearly explain why does above tree not a heap? b) Using build heap procedure discussed in the class, con
    15·1 answer
  • Which device lets you hear audio on your computer?
    12·2 answers
  • What languages other than English are spoken in the United States?
    14·1 answer
  • Plz help ASAP
    10·1 answer
  • Using the employment websites, search for part-time jobs that relate to the career fields you are interested in pursuing or to y
    12·2 answers
  • What is the difference between i++ and ++ii?
    6·1 answer
  • What is the setting an alarm clock output??
    10·1 answer
  • Complete the sentence based on the given information.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!