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

Java Programming: public class SelectionSort { public static void sort(double[]

ID: 3672646 • Letter: J

Question

Java Programming:

public class SelectionSort
{

public static void sort(double[] a, int numberUsed)
{
int index, indexOfNextSmallest;
  
for (index = 0; index < numberUsed - 1; index++)
{
  
indexOfNextSmallest = indexOfSmallest(index, a, numberUsed);
interchange(index,indexOfNextSmallest, a);

}
}

private static int indexOfSmallest(int startIndex, double[] a, int numberUsed)
{
double min = a[startIndex];
int indexOfMin = startIndex;
int index;
for (index = startIndex + 1; index < numberUsed; index++) {
if (a[index] < min)
{
min = a[index];
indexOfMin = index;
}
}
return indexOfMin;
}

private static void interchange(int i, int j, double[] a)
{
double temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
  
}

//////////////////////////////////////////////////////////////////


public class SelectionSortDriver
{
public static void main(String[] args)
{
double[] b = {7.7, 5.5, 11, 3, 16, 4.4, 20, 14, 13, 42};

System.out.println("Array contents before sorting:");
int i;
for (i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
System.out.println( );
  
SelectionSort.sort(b, b.length);

System.out.println("Sorted array values:");
for (i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
System.out.println( );
}
}

Design a class called BubbleSort that is similar to the class SelectionSort. The class BubbleSort will be used in the same way as the class SelectionSort, but it will use the bubble sort algorithm. . BubbleSort.java . BubbleSortDriverjava The bubble sort algorithm checks all adjacent pairs of elements in the array from the beginning to the end and interchanges any two elements that are out of order. The process is repeated until the array is sorted. The algorithm is as follows: Bubble Sort Algorithm to Sort an Array a Repeat the following until the array a is sorted: for (index = 0; index aindex 1]) interchange the values of a[index] and a[index1]

Explanation / Answer

BubbleSorter.java
public class BubbleSorter {
  
   public static void sort(int[]a){
       boolean sorted = false;
       int iterations = a.length - 1; // max number of passes
      
       while(!sorted && iterations >0){
           sorted = true;
           for (int i=0; i< iterations; ++i){
               if (a[i]> a[i + 1]){
                   // swap; bubble largest value up
                   // to highest position
                  
                   interchange(i, a);
                   sorted = false;
               }
           }
           -- iterations;
       }
   }
  
   private static void interchange(int i, int[] array){
       int temp;
       temp = array[i];
       array[i] = array[i +1];
       array[i + 1] = temp; // original value of a[i];
   }

}

BubbleSortDemo.java

import java.util.Scanner;

public class BubbleSortDemo {
  
   public static void main(String[] args){
  
       Scanner keyboard = new Scanner(System.in);
       int[]b = new int[7];
  
       for (int i = 0; i<7; i++){
           System.out.println("Please enter number " + (i+1));
           b[i]=keyboard.nextInt();
       }
      
       System.out.println("Array values before sorting:");
      
       int i;
       for (i=0; i<b.length; i++){
           System.out.println(b[i] + " ");
       }
      
       System.out.println();
      
       BubbleSorter.sort(b);
      
       System.out.println("Array values after sorting:");
      
       for (i = 0; i < b.length; i++){
           System.out.println(b[i] + " ");
       }
      
       System.out.println();
   }

}

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