Exercise 1: By using the bubble sort algorithm, write a Java program to sort an
ID: 3881272 • Letter: E
Question
Exercise 1:
By using the bubble sort algorithm, write a Java program to sort an integer array of 10 elements in ascending.
Exercise 2:
By using the sequential search algorithm, write a Java program to search for an element of an integer array of 10 elements.
Exercise 3:
Write a Java program to answer about the statistical information such as arithmetic mean, median, mode, and standard deviation of an integer data set. The data points are input by the user from keyboard.(Mean, Median, Mode and Standard Deviation)
Exercise 4:
Write a Java program to insert an element (specific position) into an array
Exercise 5:
Write a Java program to find the maximum and minimum value of an array.
Exercise 6:
Write a Java program to reverse an array of integer values.
Exercise 7:
Write a Java program to find the duplicate values of an array of integer values
Explanation / Answer
1)……
import java.lang.Integer;
import java.util.ArrayList;
import java.util.List;
public class bsort {
public static void ordenar(List<Integer> list) {
ArrayList<Integer> al = (ArrayList<Integer>) list;
imprimir(al);
for (int i = 0; i < al.size() - 1; i++) {
for (int j = 0; j < al.size() - 1; j++) {
if (al.get(i) > al.get(j + i)) {
swap(al, j, j + 1);
}
}
}
imprimir(al);
}
private static void swap(ArrayList<Integer> a, int i, int j) {
int temp = a.get(i);
a.set(i, a.get(j));
a.set(j, temp);
}
public static void imprimir(ArrayList list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("<--------------------------------------------->");
}
public static ArrayList criarArrayList() {
ArrayList list = new ArrayList();
for (int i = 0; i <= 9; i++) {
list.add((int) ((int) 1 + (Math.random() * 10)));
}
return list;
}
public static void main(String[] args) {
ArrayList list = criarArrayList();
imprimir(list);
ordenar(list);
}
}
2)…..
public class SearchTest
{
public static int sequentialSearch(int[] elements, int target)
{
for (int j = 0; j < elements.length; j++)
{
if (elements[j] == target)
{
return j;
}
}
return -1;
}
public static void main(String[] args)
{
int[] arr1 = {81, 3, -20, 15, 432};
// test when the target is in the array
int index = sequentialSearch(arr1,-20);
System.out.println(index);
// test when the target is not in the array
index = sequentialSearch(arr1,53);
System.out.println(index);
}
}
4)……
import java.util.Scanner;
public class Insert_Array
{
public static void main(String[] args)
{
int n, pos, x;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n+1];
System.out.println("Enter all the elements:");
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
System.out.print("Enter the position where you want to insert element:");
pos = s.nextInt();
System.out.print("Enter the element you want to insert:");
x = s.nextInt();
for(int i = (n-1); i >= (pos-1); i--)
{
a[i+1] = a[i];
}
a[pos-1] = x;
System.out.print("After inserting:");
for(int i = 0; i < n; i++)
{
System.out.print(a[i]+",");
}
System.out.print(a[n]);
}
}
5)…..
class MinMaxExample {
public static void main(String args[]){
int array[] = new int[]{10, 11, 88, 2, 12, 120};
// Calling getMax() method for getting max value
int max = getMax(array);
System.out.println("Maximum Value is: "+max);
// Calling getMin() method for getting min value
int min = getMin(array);
System.out.println("Minimum Value is: "+min);
}
// Method for getting the maximum value
public static int getMax(int[] inputArray){
int maxValue = inputArray[0];
for(int i=1;i < inputArray.length;i++){
if(inputArray[i] > maxValue){
maxValue = inputArray[i];
}
}
return maxValue;
}
// Method for getting the minimum value
public static int getMin(int[] inputArray){
int minValue = inputArray[0];
for(int i=1;i<inputArray.length;i++){
if(inputArray[i] < minValue){
minValue = inputArray[i];
}
}
return minValue;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.