Home > Articles > Programming > Algorithms

  • Print
  • + Share This
This chapter is from the book

Experiments

Carrying out these experiments will help to provide insights into the topics covered in the chapter. No programming is involved.

  1. In bubbleSort.
    java
    (Listing 3.1) rewrite main() so it creates a large array and fills that array with data. You can use the following code to generate random numbers:

  2. for(int j=0; j<maxSize; j++)      // fill array with
      {                  // random numbers
      long n = (long)( java.lang.Math.random()*(maxSize-1) );
      arr.insert(n);
      }
  3. Try inserting 10,000 items. Display the data before and after the sort. You'll see that scrolling the display takes a long time. Comment out the calls to display() so you can see how long the sort itself takes. The time will vary on different machines. Sorting 100,000 numbers will probably take less than 30 seconds. Pick an array size that takes about this long and time it. Then use the same array size to time selectSort.java (Listing 3.2) and insertSort.java (Listing 3.3). See how the speeds of these three sorts compare.

  4. Devise some code to insert data in inversely sorted order (99,999, 99,998, 99,997, ...) into bubbleSort.java. Use the same amount of data as in Experiment 1. See how fast the sort runs compared with the random data in Experiment 1. Repeat this experiment with selectSort.java and insertSort.java.

  5. Write code to insert data in already-sorted order (0, 1, 2, ...) into bubbleSort.java. See how fast the sort runs compared with Experiments 1 and 2. Repeat this experiment with selectSort.java and insertSort.java.

  • + Share This
  • 🔖 Save To Your Account