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

Write a program to sort arrays using 3 different methods: Bubble Sort, Selection

ID: 643412 • Letter: W

Question

Write a program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. All three sorting techniques are discussed in class and described in your book, there are also numerous websites that describes each technique. All these sorting methods use comparisons and swaps, and they are not equally efficient; to check on the efficiency, you will count the swaps and comparisons done by each method. Do this using Java and put all in one program.

Fill an array with 119 real numbers between 0.00 and 923.00. Generate the numbers using the subroutine that generates random numbers. Use the seed value 1900 while generating random numbers. Make a spare copy of the array; you will need it later. Call a subroutine to print the contents of the array with 9 numbers per line, in F6.2 (ex. 835.60) format. Use double-spacing. The last line will not be full. Call a subroutine to sort the array in ascending order using Selection Sort. You will need to pass the array as an argument of this subroutine.

Call the print subroutine again, as in (2). Also: skip 2 lines and then print lines stating the number of swaps and the number of comparisons used in the Selection Sort step.

Starting over with the original contents of the array, call a subroutine to sort the array in ascending order using Bubble Sort. You will need to pass the array as an argument of this subroutine, also. Call the print subroutine again, as in (2). Also: skip 2 lines and then print lines stating the number of swaps and the number of comparisons used in the Bubble Sort step.

Starting over with the original contents of the array, call a subroutine to sort the array in ascending order using Insertion Sort. You will need to pass the array as an argument of this subroutine, also. Call the print subroutine again. Also: skip 2 lines and then print lines stating the number of swaps and the number of comparisons used in the Insertion Sort step.

Explanation / Answer

import java.io.*;

import java.util.*;



class ArraySortExample

{


void getArray(double[] a)

{

System.out.println("Elements in Array:");

Random r=new Random();

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

a[i]=r.nextDouble();

}


void display(double[] a)

{

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

System.out.println(a[i]);

}


void bubbleSort(double[] a)

{

double swap;

for(int c = 0; c < ( 120 - 1 ); c++)

{

for (int d = 0; d < 120 - c - 1; d++)

{

if (a[d] > a[d+1]) /* For descending order use < */

{

swap=a[d];

a[d]=a[d+1];

a[d+1]=swap;

}

}

}

System.out.println("Bubble Sorted list of numbers");

for (int c = 0; c < 120; c++)

System.out.println(a[c]);

}


void selectionSort(double[] arr)

{

for (int i = 0; i < arr.length - 1; i++)

{

int index = i;

for (int j = i + 1; j < arr.length; j++)

if (arr[j] < arr[index])

index = j;

  

double smallerNumber = arr[index];

arr[index] = arr[i];

arr[i] = smallerNumber;

}

System.out.println("Selection Sorted list of numbers");

for (int c = 0; c < 120; c++)

System.out.println(arr[c]);


}


void insertionSort(double[] arr)

{

for(int i = 1; i < arr.length; i++)

{

double valueToSort = arr[i];

int j = i;

while (j > 0 && arr[j - 1] > valueToSort)

{

arr[j] = arr[j - 1];

j--;

}

arr[j] = valueToSort;


System.out.println("Insertion Sorted list of numbers");

for (int c = 0; c < 120; c++)

System.out.println(arr[c]);



}



}


}

  

class SortExample

{

public static void main(String args[])

{

double[] a=new double[120];

ArraySortExample as=new ArraySortExample();

as.getArray(a);

as.display(a);

as.bubbleSort(a);

as.selectionSort(a);

}

}

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