Question: - need to add javadoc in the code listed below - Create UML in Word FIle ...... - add one demo class for each sub
- need to add javadoc in the code listed below
- Create UML in Word FIle ......
- add one demo class for each sub classes
********************************************************************************************************************************
Bubble sort
//Define a method "bubblesort()"
public void bubblesort()
{
//Loop until length
for(int out = nElements-1; out > 1; out--)
{
//Loop until length
for(int in = 0; in < out; in++)
{
//If value is greater
if(array[in] > array[in+1])
{
//Sawp values
swap(in, in + 1);
//Increment count
totalSwaps++;
//Increment count
totalComparisons++;
}
}
}
}
Selection Sort
//Define a method selectionSort()
public void selectioSort()
{
//Loop until length
for (int i = 0; i < nElements-1; i++)
{
//Declare variables
int currentMinIndex = i;
//Loop until length
for (int j = i + 1; j < nElements; j++)
//If value is lesser
if (array[j] < array[currentMinIndex])
{
//Assign minimum index
currentMinIndex = j;
//Increment count
totalComparisons++;
}
//Swap values
swap(i, currentMinIndex);
//Increment count
totalSwaps++;
}
}
Insertion sort
//Define a method "insertionSort()"
public void insertionSort()
{
//Loop until length
for(int i = 1; i < nElements; i++)
{
//Loop until length
for(int j = i; j > 0; j--)
{
//If value is lesser
if(array[j] < array[j-1])
{
//Swap values
swap(j, j - 1);
//Increment count
totalSwaps++;
//Increment count
totalComparisons++;
}
}
}
}
Quick Sort
//Define a method "quickSort()"
public void quickSort()
{
//Call "sort()" method
sort(this.array, 0, array.length - 1);
}
//Define a method sort()
private void sort(int[] array, int left, int right)
{
//If left is greater than or equals right
if(left >= right)
//Return
return;
//Declare variables
int randomIndex = new Random().nextInt(array.length);
//Declare variables
int pivot = array[randomIndex];
//Declare variables
int index = partition(array, left, right, pivot);
//Call "sort()" method
sort(array, left, index - 1);
//Call "sort()" method
sort(array, index, right);
}
//Define a method "partition()"
private int partition(int[] array, int left, int right, int pivot)
{
//Loop until left is less than or equals right
while(left <= right)
{
//Loop until value is less than pivot
while(array[left] < pivot)
//Increment left
left++;
//Loop until value is greater than pivot
while(array[right] > pivot)
//Decrement right
right--;
//If left is less than or equals right
if(left <= right)
{
//Swap
swap(left, right);
//Increment left
left++;
//Decrement right
right--;
//Increment count
totalSwaps++;
}
}
//Return left
return left;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
