Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the System class introduced in our lab session and the selection sort prog

ID: 3673149 • Letter: U

Question

Using the System class introduced in our lab session and the selection sort program discussed in our lecture to report the elapsed time when we use selection sort program to sort n integers, where n is an integral number input by the user of your program and each integer is between 0 and 99999 inclusive. The following is a sample run of your program.

Input a number (at least 1000):

4000

Selection sort needs 60 milliseconds to sort 4000 integers.

Continue ? (Yes/No) Yes

Input a number (at least 1000):

10000

Selection sort needs 310 milliseconds to sort 10000 integers.

Continue ? (Yes/No) No

Explanation / Answer

Here is the program to solve the question. Please rate the answer if it helped. Thank you very much.

import java.util.Random;

import java.util.Scanner;

public class SelectionSortTimer {

   public static long selectionSort(int numbers[])

   {

       long start=System.currentTimeMillis() ,end;

       int minIdx, size = numbers.length, temp;

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

       {

           minIdx = i;

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

           {

               if(numbers[j] < numbers[minIdx])

               {

                   minIdx =j;

               }

                  

           }

          

           if(minIdx != i)

           {

               temp = numbers[i];

               numbers[i] = numbers[minIdx];

               numbers[minIdx] = temp;

           }

       }

      

       end = System.currentTimeMillis();

       return end - start;

   }

  

   public static void main(String[] args) {

       Scanner scanner=new Scanner(System.in);

       int numbers[],n;

       String choice;

      

       do

       {

           System.out.print(" Input a number (atleast 1000):");

           n = scanner.nextInt();

          

           //generate n random numbers in the range 0- 99999 inclusive

           Random random=new Random();

           numbers = new int[n];

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

           {

               numbers[i] = random.nextInt(100000);

           }

          

           long time = selectionSort(numbers);

           System.out.println("Selection sort needs "+time+" milliseconds to sort "+n+" integers.");

           System.out.println("Continue ? (Yes/No) ");

           choice = scanner.next();

       }while(choice.equalsIgnoreCase("yes"));

   }

}

output

Input a number (atleast 1000):5000

Selection sort needs 22 milliseconds to sort 5000 integers.

Continue ? (Yes/No)

yes

Input a number (atleast 1000):9000

Selection sort needs 43 milliseconds to sort 9000 integers.

Continue ? (Yes/No)

yes

Input a number (atleast 1000):20000

Selection sort needs 201 milliseconds to sort 20000 integers.

Continue ? (Yes/No)

no

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote