Hello Everyone, I have a java program where the user can select 1000, 10000, or 100000 elements and I want to be able to count the swaps and comparisons for each of the 3 options of elements above this is my code for the bubble sort array. Can anyone suggest me, where I am wrong?
When I run this code on the java compiler here, the output for every array size is
Comparisons: 99980001 swaps: 0
Comparisons: 99980001 swaps: 0
Edit: I have a tester class that holds a random array with values from 1-100
This is my tester class:
Java:
public static void bubbleSort(int[] arrayToSort)
{
int comps=0;
int swaps=0;
for(int i = 0; i < arrayToSort.length-1; i++)
{
for(int j = 0; j < arrayToSort.length-1; j++)
{
comps++;
if(arrayToSort[j] > arrayToSort[j+1])
{
swaps++;
int temp = arrayToSort[j];
arrayToSort[j] = arrayToSort[j+1];
arrayToSort[j+1] = temp;
}
}
}
System.out.println("\nComparisions: " + comps + "swaps: " + swaps);
}
When I run this code on the java compiler here, the output for every array size is
Comparisons: 99980001 swaps: 0
Comparisons: 99980001 swaps: 0
Edit: I have a tester class that holds a random array with values from 1-100
This is my tester class:
Java:
import java.util.*;
import java.util.Scanner;
import java.util.Random;
public class Tester extends algorithmTest
{
public static void main(String [] args)
{
int options;
int sortType=0;
int numElements = 0;
int algorithm;
String algName="";
Scanner keyIn = new Scanner(System.in);
System.out.print("****************MENU****************");
System.out.print("\nPlease Select The Number Of Elements In The Array:");
System.out.print("\n1. 1000");
System.out.print("\n2. 10000");
System.out.print("\n3. 100000");
System.out.print("\n4. Quit\n");
numElements = keyIn.nextInt();
switch(numElements){
case 1 :
numElements = 1000;
System.out.print("You have selected " + numElements + " elements");
break;
case 2 :
numElements = 10000;
System.out.print("You have selected " + numElements + " elements");
break;
case 3 :
numElements = 100000;
System.out.print("You have selected " + numElements + " elements");
break;
default :
System.out.println("Error");
}
System.out.print("\n***********MENU***********");
System.out.print("\nPlease Select A Sorting Algorithm");
System.out.print("\n1. Bubble Sort");
System.out.print("\n2. Enhanced Bubble Sort");
System.out.print("\n3. Selection Sort");
System.out.print("\n4. Insertion Sort");
System.out.print("\n5. Quit\n");
algorithm = keyIn.nextInt();
int [] array = new int[numElements];
Assignment.bubbleSort(array);
if(algorithm ==1)
{
algName = "Bubble Sort: ";
Assignment.bubbleSort(array);
}
else if(algorithm ==2)
{
algName = "Enhanced Bubble Sort: ";
Assignment.bubbleSortEnhanced(array);
}
else if(algorithm ==3)
{
algName = "Selection Sort: ";
Assignment.selectionSort(array);
}
else if(algorithm ==4)
{
algName = "Insertion Sort: ";
Assignment.insertionSort(array);
}
System.out.print("\n*************MENU****************");
System.out.print("\nPlease Select A Sorting Option");
System.out.print("\n1. Random Sorted Array");
System.out.print("\n2. Sorted Array");
System.out.print("\n3. Inversely Sorted Array\n");
sortType=keyIn.nextInt();
System.out.print("\nRandom Array: ");
for(int i=0; i<array.length; i++)
{
array[i] = (int)(Math.random() * 101);
System.out.print(array[i] +" ");
}
System.out.print("\nSorted Array: ");
Assignment.bubbleSort(array);
for(int i=0; i<array.length; i++)
{
System.out.print(array[i] +" ");
}
if(sortType ==1)
{
Assignment.bubbleSort(array);
System.out.print("\nRandomly Sorted Array: ");
for(int i=0; i<array.length; i++)
{
array[i] = (int)(Math.random() * 101);
System.out.print(array[i] +" ");
}
}
else if(sortType ==2)
{
Assignment.bubbleSort(array);
System.out.print("\n" +algName);
for(int i=0; i<array.length; i++)
{
System.out.print(array[i] +" ");
}
}
else if(sortType ==3)
{
long start = System.nanoTime();
Assignment. inverseSort(array);
System.out.print("\n" +algName);
for(int i=0; i<array.length; i++)
{
System.out.print(array[i] +" ");
}
long end = System.nanoTime();
System.out.print("\nThe algorithm was completed in " + (end-start) + " nanoseconds");
}
}}