-1

This is a pretty simple, straightforward problem, but, of course, I've managed to do something wrong. First, I generated 5 different arrays of 10 random numbers--from 1 to 10, 1 to 100, up to 1 to 100,000. Then I took each array and performed 5 different types of sorts (for a total of 25), calculating the time it takes to perform the sorts. I cannot figure out why each and every result is 0ms regardless of the size of n. What am I doing wrong?

public class Lab16Sorting {

public static void main(String[] args) 
{
    final int TOTAL_NUMBERS = 10;
    int count;
    int[] num = new int[TOTAL_NUMBERS];
    Random rand = new Random();

    // Generate 10 numbers from 1 - 10  
    System.out.println("SORT 10");
    System.out.println("----------------");

    for (count = 0; count < TOTAL_NUMBERS; count++)
        num[count] = rand.nextInt(10);

    System.out.println("Array: " + num);
    runSort(num);

    // Generate 10 numbers from 1 - 100     
    System.out.println("\nSORT 100");
    System.out.println("----------------");

    for (count = 0; count < TOTAL_NUMBERS; count++)
        num[count] = rand.nextInt(100);

    System.out.println("Array: " + num);
    runSort(num);

    // Generate 10 numbers from 1 - 1,000       
    System.out.println("\nSORT 1,000");
    System.out.println("----------------");

    for (count = 0; count < TOTAL_NUMBERS; count++)
        num[count] = rand.nextInt(1000);

    System.out.println("Array: " + num);
    runSort(num);

    // Generate 10 numbers from 1 - 10,000  
    System.out.println("\nSORT 10,000");
    System.out.println("----------------");

    for (count = 0; count < TOTAL_NUMBERS; count++)
        num[count] = rand.nextInt(10000);

    System.out.println("Array: " + num);
    runSort(num);

    // Generate 10 numbers from 1 - 100,000     
    System.out.println("\nSORT 100,000");
    System.out.println("----------------");

    for (count = 0; count < TOTAL_NUMBERS; count++)
        num[count] = rand.nextInt(100000);

    System.out.println("Array: " + num);
    runSort(num);
}

/**
 * Run sort algorithms
 */

private static void runSort(int[] num)
{
    long before;
    long after;

    // Run and display selection sort
    before = System.currentTimeMillis();
    selectionSort(num);     
    after = System.currentTimeMillis();
    System.out.println("Selection sort took "+ (after-before) +" milliseconds");

    // Run and display bubble sort
    before = System.currentTimeMillis();
    bubbleSort(num);
    after = System.currentTimeMillis();
    System.out.println("Bubble sort took "+ (after-before) +" milliseconds");

    // Run and display insertion sort
    before = System.currentTimeMillis();
    insertionSort(num);     
    after = System.currentTimeMillis();
    System.out.println("Insertion sort took "+ (after-before) +" milliseconds");

    // Run and display merge sort
    before = System.currentTimeMillis();
    mergeSort(num);
    after = System.currentTimeMillis();
    System.out.println("Merge sort took "+ (after-before) +" milliseconds");

    // Run and display quick sort
    before = System.currentTimeMillis();
    quickSort(num);
    after = System.currentTimeMillis();
    System.out.println("Quick sort took "+ (after-before) +" milliseconds");        
}

I printed out the various array addresses and I see they're all the same (which makes sense since I'm using the same array object). I thought that was the problem and so I tried using different arrays (int[] num, int[] num2...) and I tried re-initializing the array after each runSort() method call with num = new int[TOTAL_NUMBERS].

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
speedracer
  • 95
  • 2
  • 5
  • 12

3 Answers3

2

That's because the size 10 is too less to actually find out the difference in timings between the various types of sorts. Try to increase your size to somewhere around 50,000 to 1,00,000, to actually be able to see the difference(even then its gonna be in few seconds).

And if your machine can take enough load, then go about sorting elements in the range of 10,00,000(highly non-advisable just for testing time difference).

Rahul
  • 44,383
  • 11
  • 84
  • 103
  • But the assignment requirement is: Generate an array of random integers for each of the following lengths: n = 10, n = 100, n = 1,000, n = 10,000, n = 100,000. Since my instructor provided the numbers, I presumed they would work and provide diverse results. I just tried 100,000,000 and it took 0ms. Is it possible my computer is just too fast? – speedracer Mar 14 '13 at 03:15
  • what you don't notice is, the size of your array stays the `same(10)`, only the range of random integers generated in them is changing. So it doesn't matter much. Increase your `TOTAL_NUMBERS` to `50000` and see what happens(try 10000 first, as your system may hang for 50000). – Rahul Mar 14 '13 at 03:18
  • @stef2dotoh your array is of size 10 every time, you're just filling the array with numbers from 0 to 10, from 0 to 100 (and on and on...) on every test. – Luiggi Mendoza Mar 14 '13 at 03:19
  • @stef2dotoh it would be better if you make a method that receives the size of the array and do all the job: create the array, fill it with random numbers and sort it, then from your main method just call this method with 10, 100, 1000... And by the way, your computer is not so fast as you may think, don't lie yourself :). – Luiggi Mendoza Mar 14 '13 at 03:21
  • ah! then i read the instructions incorrectly. i was thinking i had to generate 10 numbers within various ranges. clearly sorting 10 numbers each time will yield the same results. thanks! – speedracer Mar 14 '13 at 03:24
0

RJ is correct, you array is just too small that the sorting algorithms don't matter.

Also see this thread Test case for Insertion Sort, MergeSort and Quick Sort

Community
  • 1
  • 1
cwhsu
  • 1,493
  • 24
  • 31
0

The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance. Above is in java doc.

William Feirie
  • 624
  • 3
  • 7