• Post Reply Bookmark Topic Watch Topic
  • New Topic

Do I really need to understand this to be a good programmer?  RSS feed

 
Pablo Jose Alvarez
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm learning Java through a series of books, tutorials, etc and so far so good. There've been some examples however, that I find really complicated and hard to understand, and it frustrates me because it makes me wonder if I'm really good at this (I know it requires years of practice). Here's one example, taken from Java A Beginners Guide



The purpose of the program is to sort the variables in an array. I find the logic of the second set of nested loops (after the "bubble" comment)really weird and complicated. Now, I know there are many other ways to sort arrays in java, even built-in classes and methods for that sole purpose and in general, the Java API allows for us to do a lot of stuff that without them, would require many lines of code....my question is then...do good programmers really need to be able to design what in my opinion, is a very complicated algorithm? Meaning, in real life applications, do people really program like that?

I wonder if I really need to be able to understand code like this in order to be able to become a good programmer in the future.

I hope you understand where I'm coming from and hope you can give me some guidance.

Cheers!
 
Pablo Jose Alvarez
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I found this is a common and "simple" algorithm and found some resources online that explain how it works

http://mathbits.com/MathBits/Java/arrays/Bubble.htm
http://www.youtube.com/watch?v=Fs_elYHrTHU

I'll check these out tomorrow and try to really understand what the algorithm does and how, just for the sake of understanding it. Still, I'd like my initial question addressed if possible Do I really need to know this stuff? Thanks!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
545
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are going to need to be able to read code like that and understand what it does. The code isn't that complicated (in the scheme of things) even though it seems that way now.

It will get easier to read over time though. There are programming idioms so you learn to read in chunks. Also, draw what is happening. It helps to follow the flow.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pablo Jose Alvarez wrote:my question is then...do good programmers really need to be able to design what in my opinion, is a very complicated algorithm? Meaning, in real life applications, do people really program like that?


To start, the bubble sort algorithm is not that complicated. In fact, it is the sorting algorithm that is generally taught first, because it is likely the simplest of all the sorting algorithm.

Question. Why do you think that the bubble sort algorithm is complicated? Is there something regarding the design that you don't understand?

Or ... do you regard it as complicated, because you are working in reverse? Meaning you are trying to figure out the algorithm based on the code. That's generally not a good way to figure out the algorithm. The bubble sort algorithm is highly documented, and there are plenty of explanations. Trying to figure out an algorithm based on the code is reverse engineering -- and yes, that can get very complicated.

Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pablo Jose Alvarez wrote:I find the logic of the second set of nested loops (after the "bubble" comment)really weird and complicated. Now, I know there are many other ways to sort arrays in java...

OK, but those aren't your problem at the moment, so forget about them and concentrate on what's troubling you now. It's a very important thing to learn, especially in programming: deal with one thing at a time.

I agree with Henry that trying to work out an algorithm from the code is quite a tough way to go (to be honest, it took me a while to work out what those loops were doing too ), but there's a couple of things you can do to help you:

First: space out your code. For example:Don't you find that easier to read? I think most people do. There's very few restrictions on how much space you can put in Java code, and spacing things out can often help you to separate them in your mind. I like to think of it as giving code "room to breathe".

Second: (very important) Java array indexes start at 0, so, for example, nums[1] is the 2nd element, not the first. So, given that, what is the last element going to be?

Third: It sometimes helps to write stuff out in English (or your native tongue). So, just taking that first line:
for(a = 1; a < size; a++) { ...
What is it doing? Forget about all the other stuff for the moment and just concentrate on it. You might want to have a notepad and pencil handy so you can scribble down notes or values for yourself too.

Also: a picture is often worth a thousand words. The Wiki page, for example, has a very nice little GIF that shows the bubble sort in action.

And if you're still confused? Forget that code altogether, and try and write a bubble sort of your own. How would you go about it? There are many ways to write a bubble sort; another one being:Do you find that version easier? It's not as efficient as the one they gave you, but it'll do the job.

To get back to your question: Yes, you do need to know this stuff and you will have to understand code; but you don't need to understand it all at once. Take it in small pieces - line by line if need be.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pablo Jose Alvarez and Others wrote: . . .
. . .
I can think of several other ways to write those three linesYou don't need to know how the exchange method works. Or the IntSwapper class. But those names may make it easier to understand what is happening inside that loop where you exchange the two values.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But here are those two bits of codeLook at lines 12-14. See anything familiar?You can write different versions of the same class.
 
Pablo Jose Alvarez
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RESOLVED

Hi all,

Thank you very much for all the advises! The one that really helped me, was to first try to understand the code line by line, instead of the algorithm as a whole. So here's what I did

1. I first created this program to understand how the two loops work together and when each of them finishes iterating



That was extremely useful to understand what was going on.

2. Then I moved on to understand what the if control statement is doing. This is what I wrote



Once I saw this, I had a very good idea of what the code is doing and how, but I wanted to put it all together.

3. I wrote every single iteration of both loops in notepad to understand exactly what each of them was doing (I won't copy everything because it's a lot)



I did this with every single iteration and the end result was a nicely sorted array!!! Looking at it this way, the algorithm is indeed simple and very clever too.

This was a great learning experience for me and has taught me how to approach algorithms that might seem challenging.

Thank you very much and happy coding!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pablo Jose Alvarez wrote:...I did this with every single iteration and the end result was a nicely sorted array!!!

Very well done! And you get a cow for showing us what you did...thanks for that.

Looking at it this way, the algorithm is indeed simple and very clever too...

Many of them are. Clever lads, these mathematicians.

Winston
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Winston. That sort of thing is a good way to see how the program works and you learn a great deal from it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!