Task 2: Internet traffic As a digital business analysist, you have been asked by your line manager to supply the last seven months traffic figures to your company’s website. At the moment, the traffic numbers are not in ascending order. Write the pseudocode to solve the initial problem and then write a Java algorithmic program to sort the array of given traffic figures. The traffic numbers to sort correctly are: 2, -17, 8, 26, 1, 0, 84 Hint: Bubble sort
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] arr = new int[7];
boolean isSorted = true;
int buf;
for (int i = 0; i < arr.length; i++) {
System.out.print("Month " + (i + 1) + ": ");
arr[i] = in.nextInt();
}
for (int i = 0; i < arr.length; i++){
for (int j = 0; j < arr.length - 1; j++) {
if (arr[i] < arr[j]) {
buf = arr[i];
arr[i] = arr[j];
arr[j] = buf;
}
}
}
for (int i : arr) {
System.out.print(i + " ");
}
}
}
Comments
Leave a comment