Algorithms In computer science and mathematics, an algorithm is a series of step
ID: 3882336 • Letter: A
Question
Algorithms
In computer science and mathematics, an algorithm is a series of steps or instructions used to complete a calculation. Flowcharts are type of diagram that is typically used document algorithms. For example,
Figure 1 shows an algorithm which sorts any three integers from smallest to largest.
Figure 1: Flow chart for sorting three integers from smallest to largest.
Challenge: Can develop and program a better algorithm to sort three unique random integers? You should demonstrate your solution on 1000 sets of numbers. Verify your result by comparing your solution to my solution. Report the average number of moves per set. Each comparison that you make counts as one move and each swap counts as one move. You could
Challenge: Can develop and program a better algorithm to sort three unique random integers? You should demonstrate your solution on 1000 sets of numbers. Verify your result by comparing your solution to my solution. Report the average number of moves per set. Each comparison that you make counts as one move and each swap counts as one move. You could keep track of your moves with a counter.
Start Input three integers (x,x,x) Yes-,Swap(x,and%) No No ap (x and x No EndExplanation / Answer
Hi,
Please find the Answer for your Question.
Program:
import java.util.ArrayList;
import java.util.List;
public class ThreeIntegers {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
int num1=0;
int num2=0;
int num3=0;
// Enter three numbers
System.out.print("Enter three integers: ");
num1 = input.nextInt();
num2 = input.nextInt();
num3 = input.nextInt();
List<Integer> list = new ArrayList<Integer>();
int counter =1;
if (num1 > num2) {
int temp = num1;
list.add(counter);
num1 = num2;
list.add(counter);
num2 = temp;
list.add(counter);
}
if (num2 > num3) {
int temp = num2;
list.add(counter);
num2 = num3;
list.add(counter);
num3 = temp;
list.add(counter);
}
if (num1 > num2) {
int temp = num1;
list.add(counter);
num1 = num2;
list.add(counter);
num2 = temp;
list.add(counter);
}
int totalSum=0;
for(int sum=0;sum<list.size();sum++){
totalSum += list.get(sum);
}
System.out.println("moves ---> "+totalSum);
System.out.println("Avg of set---> "+totalSum/list.size());
System.out.println("The Numbers in small to big in order "
+ num1 + " " + num2 + " " + num3);
}
}
OUTPUT:
Enter three integers: 50 10 40
moves ---> 6
Avg of each set---> 1
The Numbers in small to big in order 10 40 50
Note : like that we can test 1000 times and each set will give you avg and swaps count.
Thank You.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.