‫Okay.
‫For this project, we are going to push this thing and get some real results.
‫Let's first start a new project and copy paste our existing code in it.
‫So new project.
‫Call it whatever you like.
‫And we'll copy this class and paste it over to our new project.
‫So here's the plan.
‫We are going to create an array of 1000 elements populated randomly, then call each one of our sorting
‫algorithms ten times and keep track of how long it takes to sort them.
‫First things first, let's delete everything in main with the exception of start and stop time.
‫Let's also go down here and delete array.
‫We won't be using it.
‫Now let's create regions.
‫So copy, insert sort.
‫Let's paste that up top, then paste it again and call this one random generator.
‫We'll paste insert sort three more times and replace the title with quicksort up here.
‫Selection sort.
‫Bubble sort.
‫And finally insert sort.
‫So we have all of our regions.
‫We'll need our random utility.
‫So we'll import Java utilities.
‫Random.
‫We'll instantiate a random object by saying random rnd is equal to new random.
‫We also need a long to hold our runtime variable.
‫So we'll say long runtime and set it to zero.
‫Let's start working on our random generator.
‫But I guess first let's change this int array to an array of a 1000 elements.
‫And we won't be cloning it just yet.
‫So let's remove this here statement.
‫Let's also move this statement in here so it's not lonely.
‫Now we need a loop so for int I is equal to zero i less than nums dot length i plus plus.
‫And in the brackets we set nums I is equal to rnd next int 1000.
‫So we're picking the number between zero and 1000.
‫Now that we have all of our random elements, let's make sure to clone our array so that we perform
‫the algorithms on the same array and it's fair to all parties involved.
‫So we say reset is equal to nums dot clone.
‫The last bit of code will right here is a simple print statement to display what values are being printed.
‫So we'll say system print line and we skip a bunch of spaces, say runtime.
‫Skip comparisons.
‫Skip.
‫Swaps.
‫Now we move on to QuickSort and as we said before.
‫We'll be testing each one 10 times and getting an average.
‫So for int i is equal to zero.
‫I.
‫Less than ten i.
‫Plus plus.
‫Let's set start.
‫Time is equal to system.
‫nano time.
‫Then called quicksort and pass zero and nums length minus one as parameters.
‫We then say stop time is equal to system . nano time.
‫All right.
‫Instead of storing ten runtimes for each and every iteration, let's add runtime to rntm.
‫And when the time comes, we'll divide by ten.
‫So rntm plus equals runtime, and we pass start time and stop time as parameters.
‫Finally, let's reset the array so that we go into the next iteration with the same unsorted array we
‫started with.
‫So we say nums is equal to reset clone.
‫Our last bit of code is to just print our results.
‫So system . out . print.
‫QuickSort.
‫Plus rntm divided by ten to get the average.
‫Plus empty spaces.
‫Plus comps divided by ten.
‫Since we never reset the computation.
‫Plus empty spaces.
‫Plus swaps divided by ten.
‫Same deal.
‫We never reset them.
‫So that's how we get the number of swaps per iteration.
‫And finally, we skip a line by saying system line separator.
‫When we are done, we call reset stats.
‫The rest of these are all copy paste and all we do is change the call.
‫Since we have made it this far into the series, I won't go through narrating word for word.
‫All we'll be doing is changing quicksort to selection, sort, bubble sort and insertion sort when necessary.
‫It's up to you if you want to follow along with me or just download the rest.
‫Okay.
‫We made it.
‫Let's run the program and see our results.
‫With an array of 1000 elements, you can really see the difference between each and every one of these.
‫To put it in perspective, we could run quicksort 50 times in the same time.
‫It takes insert to run once and we can also see the vast difference in the number of swaps.
‫While quicksort isn't bad at all, we only need 999 swaps with selection sorted.
‫So there you have it.
‫Ladies and gents, use quicksort is the lesson we learned today.
‫Thank you for tuning in.