Friday, March 10, 2017

Bits and Pieces

(Picture Copyright: My own work)


I can hardly believe that after a mere five days another chapter's challenges are finished! This last Sunday I speculated that by today the first challenge code would go live. I wasn't too sure how the chapter to come would turn out in terms of difficulty. When I started with it, not all of the searching and sorting concepts introduced made sense at first sight. For instance this:

do
   {
      swap = false;

      for (int count = 0; count < (SORT_NUMELS - 1); count++)
      {
         if (bubbleSort[count] > bubbleSort[count + 1])
         {
            temp = bubbleSort[count];
            bubbleSort[count] = bubbleSort[count + 1];
            bubbleSort[count + 1] = temp;

            ++swapCount;

            swap = true;
         }
      }
   } while (swap);

What is actually exchanged when the numbers are sorted? Yes, it is explained in the book, and thoroughly so, but it makes all the difference in the world to read about a concept, writing off the listing in the books, and actually running the code through the debugger to see what happens during execution. By adding parts of the code to watch, things became a little clearer, but still I felt that I didn't fully grasp it. Being very short a chapter, the challenges were soon to come, and after getting my feet wet, I noticed how easy it is to work with searching and sorting algorithms. 

If there was one challenge I felt was difficult then it had to be the one keeping track of the number of swaps. It wasn't the task of writing the code that was difficult, I mean, how difficult can it be to add a counter in a certain place, that takes care of counting the number of swaps? No, the problem there was: How can I verify the number output to screen is correct? What is very easy with selection sort, even past a certain number of elements, proofs to be rather difficult with bubble sort. There are many factors deciding how many swaps are taking place - one among many is the unsorted list. Are certain numbers in certain places already? 

Suffice to say that the web wasn't very helpful in finding an answer to the above question. Not that I didn't learn anything when trying to find one, though. Big-O, Omega and Theta notation, having to do with worst-case time complexity of an algorithm. Or a formula like the following:

(n1)+(n2)+(n3)++3+2+1=n(n1)2=O(n2)

Having to do with the total number of comparisons taking place. I even watched some MIT Lectures hoping to find some answer. They were great but also not the answer to what I was looking for. I eventually found it in a book called The Algorithm Design Manual provided by Penn State University. Not only offers this book past what the title suggests some insights into searching and sorting, proofs and the like, but also this: "The moral of logarithmic growth is clear: If you are gonna do the crime, make it worth the time!". Eventually I found the sought after answer in part in this book as well as on a very dated website that has bubbled down to the depths of my bottomless number of visited websites. This challenge off the table, the other challenges following this one were again very easy. 

What I learned in this lesson is to what good use sorting algorithms can be put when it comes to writing a game. I think with the things learned in this lesson it should be very well possible to write a Tower of Hanoi clone. Besides that the most important lesson learned is the importance of not only giving variables meaningful names, but also how helpful staying consistent with using them can be. My returning visitors will probably know what I mean. For instance having a variable with the name girlNames in place inside main, using this name when calling a function, but in the function header, all of a sudden, girlNames would become something very different. After typing down one of the first code examples this became clear to me. The reason as far as I can tell is that I paid more attention to what was going on in my IDE than seemingly I did in the past. When calling a function, there was also always a little window showing the function prototype: functionName(string[], int, int), being replaced by typing in variable names defined in main. I wonder why I never consciously noticed this, maybe just because it always was there and didn't seem very important. It was almost like a revelation and certainly a step in the right direction. It is like forming a unity between different parts of code by naming things in a certain way so that everything fits together. 

This not only helped in the planning stage before starting to write any code. It also enabled me to write the code faster than it was the case in the past. I think this new understanding is reflected in the code written for all of this chapters challenges. A change - hopefully for the better, has also taken place. I drastically cut down the source-code comments. Sad to say that I managed to incorporate so many that they practically loose their meaning. Another thing I should have noticed and paid more attention to in the past. What good does it do in terms of understanding to have a list of variable names above the variables - telling you that this variable is a counter, and that other one an accumulator? Thinking about it while writing this really makes me laugh. On the the other hand I feel that I owe all of my visitors who read any of my old code an honest apology ... 

S-O-R-R-Y!               ごめんなさい!           Je suis vraiment désolé...            мне очень, очень жаль!

Anyway, I hope you still find some use past the trouble of weeding out all the unnecessary comments when deciding to use any of it for your own projects! That said, it is time to move on, past any mistakes - let bygones be bygones. What is important is what is to come. For me it is chapter 9 and the topic of pointers, which I can only hope will be as easy as this chapter has been. 

I wish my fellow learners will also be blessed with important insights while working on their challenges, overcoming their past mistakes, so they be able to improve. Those kind souls who gave my listings +'s, thank you very much! And to all my visitors, past, present and future, have a nice relaxing weekend. See you all soon, with more code, and myself being one step closer in reaching my ultimate goal!

No comments:

Post a Comment