patternjavaMinor
Count the number of checks each method had to perform before fully sorting the array
Viewed 0 times
sortingthenumbereachchecksmethodbeforearrayhadperform
Problem
So I'm writing a program that compares Bubble, Selection, Merge, and Quick Sort. All 4 methods are given a randomized array of 1000 elements and I count to see how many times it takes a method to perform to fully sort the array. My question revolves around the placement of my counts. I know I should look at the Big O complexities for each to get a ballpark estimate of the number but I'm just asking if I put the counts in the right spots.
import java.util.*;
public class SortingComparison1
{
//Creates class variables
static Random random = new Random();
static int bubbleCount = 0;
static int totalBubbleCount = 0;
static int totalSelectionCount = 0;
static int totalMergeCount = 0;
static int totalQuickCount = 0;
static int mergeCount = 0;
static int selectionCount = 0;
static int quickCount = 0;
static int [] arr;
static int [] copyArr;
static int [] copyArr2;
static int [] copyArr3;
public static void main(String[] args)
{
System.out.println("Welcome to the sort tester.");
//runs this 20 times
for(int i = 0; i a[i+1])//Swap
{
bubbleCount++;
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
done = false;
}
}
}
return a;
}
public static void mergeSort(int [] a)
{
//put counter for checks inside method
int size = a.length;
if(size pivot)
{
j--;//correct position
}
if(i <= j)
{
//swaps and increments i and decrements j
int temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
}
return i;
}
}Solution
Magic number
Printing arrays
Too much
You are currently using too many
An alternative modeling approach
How about considering that each test should implement an
Then you can have implementations...
You can then rely on the methods to give you your results.
Note that this simplified approach does not take multi-threading into consideration. With some slight tweaks to how
20 - the number of iterations - should be replaced with a static final variable, so that you can freely modify the number of iterations to perform.Printing arrays
Arrays.toString(int[]) gives you a nice String representation for printing to the console, so you do not have to loop yourself.Too much
staticYou are currently using too many
static variables to store your data and counts, and that can make it unwieldy.An alternative modeling approach
How about considering that each test should implement an
interface that provides a count of every run, and other useful metrics?public interface Sorter {
/**
* Sorts an array.
*
* @param the array to sort
* @return the number of steps it takes to sort an array
*/
int sort(int[] array);
/**
* Gets the number of method calls to {@link #sort(int[])} so far.
*
* @return the number of iterations done
*/
int getIterations();
/**
* Gets the total sorting count so far.
*
* @return the total number of sorting steps done
*/
long getTotalCount();
}Then you can have implementations...
public class BubbleSorter implements Sorter {
int iterations = 0;
long totalCount = 0;
@Override
public int sort(int[] array) {
int countResult = 0;
// do sorting while calling countResult++ where appropriate
iterations++;
totalCount += countResult;
return countResult;
}
@Override
public int getIterations() {
return iterations;
}
public long getTotalCount() {
return totalCount;
}
}You can then rely on the methods to give you your results.
public static void main(String[] args) {
Sorter bubbleSort = new BubbleSorter();
int lastCount = 0;
for (int i = 0; i < ITERATIONS; i++) {
lastCount = bubbleSort.sort(getNewRandomArray());
}
System.out.println("Last count: " + lastCount);
System.out.println("Average: " +
((bubbleSort.getTotalCount() * 1.0) / bubbleSort.getIterations());
}Note that this simplified approach does not take multi-threading into consideration. With some slight tweaks to how
iterations and totalCount can be accumulated in a thread-safe manner, making calls to each sorting algorithms' sort() method concurrently to reduce execution time is certainly achievable.Code Snippets
public interface Sorter {
/**
* Sorts an array.
*
* @param the array to sort
* @return the number of steps it takes to sort an array
*/
int sort(int[] array);
/**
* Gets the number of method calls to {@link #sort(int[])} so far.
*
* @return the number of iterations done
*/
int getIterations();
/**
* Gets the total sorting count so far.
*
* @return the total number of sorting steps done
*/
long getTotalCount();
}public class BubbleSorter implements Sorter {
int iterations = 0;
long totalCount = 0;
@Override
public int sort(int[] array) {
int countResult = 0;
// do sorting while calling countResult++ where appropriate
iterations++;
totalCount += countResult;
return countResult;
}
@Override
public int getIterations() {
return iterations;
}
public long getTotalCount() {
return totalCount;
}
}public static void main(String[] args) {
Sorter bubbleSort = new BubbleSorter();
int lastCount = 0;
for (int i = 0; i < ITERATIONS; i++) {
lastCount = bubbleSort.sort(getNewRandomArray());
}
System.out.println("Last count: " + lastCount);
System.out.println("Average: " +
((bubbleSort.getTotalCount() * 1.0) / bubbleSort.getIterations());
}Context
StackExchange Code Review Q#120584, answer score: 2
Revisions (0)
No revisions yet.