Good morning all,
So I have an program that I am running doing a quicksort on an array with 5000 elements. I run it with the array in random order (300, 60, 57, 299), in ascending numerical order (0,1,2,3,4.....) and in reverse numerical order (4999, 4998, 4997, 4996.....). First off, I am not a math guy so the BigO notation is fuzzy to me, but I figure it out on the fly and use web resources to get the information. I have read that the big O notation for a quicksort was O(n log n) so when I turn that into numbers that becomes if using base 2 log: O(5000 * 12.287712) = 61438.56, and if using base 10 log O(5000 * 3.69897) = 18494.85

As I sit here writing this, I was under the impression that the counts were based on the base log 2, but as I read the formula, I now think it might be based on the base log 10 since there is not indicator in O(n log n) that I should use base log 2?

I am counting the number of times numbers were swapped and I also count how many times the median is defined, the second number is probably not relevant. But i am surprised to see that there are a similar number of swaps when comparing a random number list, to sorted and a reverse sorted list. I have posted my code below:

I guess what I am trying to see is if I am using the right counts to see how much Big O time is required to create a sorted array using the quicksort.

I would appreciate any feedback. This is homework and I am having to do comparisons on mergesorts and quicksorts, the code is done, I am just asking if I am going in the right direction with the comparisons and if I am understanding the Big O notation correctly.

Thanks
Wally

Details below:
When I run my quicksort I am getting the below outputs for 20 runs:

Random-Order-Number of swaps made 16196 Recursion Count is 1429
Sorted-Order-Number of swaps made 18242 Recursion Count is 2452
Reverse-Sorted-Number of swaps made 22790 Recursion Count is 3475
Random-Order-Number of swaps made 16072 Recursion Count is 1427
Sorted-Order-Number of swaps made 18118 Recursion Count is 2450
Reverse-Sorted-Number of swaps made 22666 Recursion Count is 3473
Random-Order-Number of swaps made 16205 Recursion Count is 1430
Sorted-Order-Number of swaps made 18251 Recursion Count is 2453
Reverse-Sorted-Number of swaps made 22799 Recursion Count is 3476
Random-Order-Number of swaps made 16333 Recursion Count is 1422
Sorted-Order-Number of swaps made 18379 Recursion Count is 2445
Reverse-Sorted-Number of swaps made 22927 Recursion Count is 3468
Random-Order-Number of swaps made 16283 Recursion Count is 1435
Sorted-Order-Number of swaps made 18329 Recursion Count is 2458
Reverse-Sorted-Number of swaps made 22877 Recursion Count is 3481
Random-Order-Number of swaps made 16224 Recursion Count is 1437
Sorted-Order-Number of swaps made 18270 Recursion Count is 2460
Reverse-Sorted-Number of swaps made 22818 Recursion Count is 3483
Random-Order-Number of swaps made 16203 Recursion Count is 1420
Sorted-Order-Number of swaps made 18249 Recursion Count is 2443
Reverse-Sorted-Number of swaps made 22797 Recursion Count is 3466
Random-Order-Number of swaps made 16366 Recursion Count is 1431
Sorted-Order-Number of swaps made 18412 Recursion Count is 2454
Reverse-Sorted-Number of swaps made 22960 Recursion Count is 3477
Random-Order-Number of swaps made 16277 Recursion Count is 1427
Sorted-Order-Number of swaps made 18323 Recursion Count is 2450
Reverse-Sorted-Number of swaps made 22871 Recursion Count is 3473
Random-Order-Number of swaps made 16290 Recursion Count is 1425
Sorted-Order-Number of swaps made 18336 Recursion Count is 2448
Reverse-Sorted-Number of swaps made 22884 Recursion Count is 3471
Random-Order-Number of swaps made 16231 Recursion Count is 1440
Sorted-Order-Number of swaps made 18277 Recursion Count is 2463
Reverse-Sorted-Number of swaps made 22825 Recursion Count is 3486
Random-Order-Number of swaps made 16088 Recursion Count is 1413
Sorted-Order-Number of swaps made 18134 Recursion Count is 2436
Reverse-Sorted-Number of swaps made 22682 Recursion Count is 3459
Random-Order-Number of swaps made 16195 Recursion Count is 1436
Sorted-Order-Number of swaps made 18241 Recursion Count is 2459
Reverse-Sorted-Number of swaps made 22789 Recursion Count is 3482
Random-Order-Number of swaps made 16274 Recursion Count is 1419
Sorted-Order-Number of swaps made 18320 Recursion Count is 2442
Reverse-Sorted-Number of swaps made 22868 Recursion Count is 3465
Random-Order-Number of swaps made 16074 Recursion Count is 1419
Sorted-Order-Number of swaps made 18120 Recursion Count is 2442
Reverse-Sorted-Number of swaps made 22668 Recursion Count is 3465
Random-Order-Number of swaps made 16246 Recursion Count is 1436
Sorted-Order-Number of swaps made 18292 Recursion Count is 2459
Reverse-Sorted-Number of swaps made 22840 Recursion Count is 3482
Random-Order-Number of swaps made 16177 Recursion Count is 1415
Sorted-Order-Number of swaps made 18223 Recursion Count is 2438
Reverse-Sorted-Number of swaps made 22771 Recursion Count is 3461
Random-Order-Number of swaps made 16006 Recursion Count is 1421
Sorted-Order-Number of swaps made 18052 Recursion Count is 2444
Reverse-Sorted-Number of swaps made 22600 Recursion Count is 3467
Random-Order-Number of swaps made 16245 Recursion Count is 1431
Sorted-Order-Number of swaps made 18291 Recursion Count is 2454
Reverse-Sorted-Number of swaps made 22839 Recursion Count is 3477
Random-Order-Number of swaps made 16102 Recursion Count is 1427
Sorted-Order-Number of swaps made 18148 Recursion Count is 2450
Reverse-Sorted-Number of swaps made 22696 Recursion Count is 3473

Hello all,
I am just reviving this as I was hoping that someone might be able to tell me the best place to put counters to make comparisons. I can add a zipped file with the code if necessary

Thanks in advance
Wally

04-03-2013, 02:42 AM

Ronin

Re: Another question about Comparison counts

Hi Wally,

Looking at the outputs you already have I am a little confused by what you would like to compare.

Going over you code though, lines 23-24 for example should read:

Code:

arr = new ArrayQuickSort(maxSize);
arr.setSwapCount(0);

Your code if full of lines which could be changed in a similar fashion. Making these changes would allow you to call methods like getMedianCount() on each instance for lines 55-59.

Regards.

04-03-2013, 05:10 PM

wfsteadman

Re: Another question about Comparison counts

Thanks for the catch, I will make those changes. Not sure why I did that. So I have a list of 5000 random numbers and I am just trying to find the best way to count the number of comparisons and swaps I guess, to get a sorted list. I am not sure I am making my counts in the right places.