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

In JAVA, you will be implementing three very well known sorting algorithms in a

ID: 3710222 • Letter: I

Question

In JAVA, you will be implementing three very well known sorting algorithms in a class named ArraySorter. Sorting algorithms sort arrays from the smallest value to the largest value. Be sure to comment on every method, and any non-elementary code within the method.

Selection Sort: Create a method selectionSort that performs a selection sort of an array of doubles. Selection sort works by selecting the smallest value in the array and swapping with the value at index 0. Then the algorithm moves onto finding the smallest value starting from index 1 and swapping it with the value at index 1. Then the algorithm moves onto finding the smallest value starting from index 2 and swapping it with index 2. This continues until all the values are sorted.

Bubble Sort: Create a method bubbleSort that performs a bubble sort of an array of doubles. The bubble sort algorithm examines all adjacent pairs of elements in the array from the beginning to the end and swaps any two elements that are out of order. Each interchange makes the array more sorted than it was until it is entirely sorted. The algorithm in pseudocode follows:

A good visualization of the algorithm in action can be found at https://visualgo.net/en/sorting

where the above function is the next number in the sequence. Using these rules and starting at 10, we get the sequence:

Thus, the Collatz sequence starting at 10, has a length of 7, as there are 7 terms. Find the starting number under one million, which produces that longest chain. Additionally, if you can prove or disprove that for any positive integer (integer in the math sense, not the 32-bit data type) n, the sequence always terminates with 1 (this can be done if commenting if necessary).

TL if n is even f (n) - (3^-i if n is odd

Explanation / Answer

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sorting;
import java.util.Scanner;
class Sort
{
void swap(double arr[],int i, int j)
{
double temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
void displayarray(double arr[])
{
int i,n=arr.length;
System.out.print(" Array after sorting: ");
for(i=0;i<n;i++)
System.out.print(arr[i]+" ");
}
void coltzSequence(int n)
{
if (n==1)
System.out.print(n);
else
{
System.out.print(n+"->");
if(n%2==0)
coltzSequence(n/2);
else
coltzSequence(n*3+1);
}

}
void selectionSort(double arr[])
{
int n = arr.length;

// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
if(min_idx != i)
swap(arr,min_idx , i);

}
}
void bubbleSort(double arr[])
{
int n = arr.length;

// One by one move boundary of unsorted subarray
for (int i = 1; i < n; i++)
{
for (int j = 0; j < n-i; j++)
if (arr[j] > arr[j+1])
swap(arr,j, j+1);

}
}
void insertinSort(double arr[])
{
int n = arr.length;
for (int i=1; i<n; ++i)
{
double key = arr[i];
int j = i-1;

while (j>=0 && arr[j] > key)
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
}
public class ArraySorter {

public static void main(String[] args) {
double arr[];
int n,ch,i,num;
Scanner sc=new Scanner(System.in);
System.out.print(" Eneter the no. of elements in Array:");
n=sc.nextInt();
arr=new double[n];
System.out.print(" Eneter the "+ n +"elements in Array: ");
for(i=0;i<n;i++)
arr[i]=sc.nextDouble();
System.out.print(" Eneter your choice: 1.Selection Sort 2. Bubble Sort 3. Insertion Sort 4. Collatz sequence : ");
System.out.print("Your Choice :");
ch=sc.nextInt();
Sort s=new Sort();
switch(ch)
{
case 1:
s.selectionSort(arr);
s.displayarray(arr);
break;
case 2:
s.bubbleSort(arr);
s.displayarray(arr);
break;
case 3:
s.insertinSort(arr);
s.displayarray(arr);
break;
case 4:
System.out.print(" Enter the number:");
num=sc.nextInt();
s.coltzSequence(num);
break;
default:
System.out.print(" Invalid choice:");
break;
}


}
}

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