Jump to content

Variations of Algorithms

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
sebkom

sebkom

    Newbie

  • Members
  • Pip
  • 2 posts
There is this module on my course, called Algorithms and Complexity, and so far we have been going through the very basic algorithms (linear and binary search, bubble sort, quick sort, etc). I did some research online and I have found so many different pseudocodes for most of them. How do we tell if a pseudocode is a specific algorithm? Is it about the basic concept or do other details play a part?

Take bubble sort for example. This is what the lecturer gave us in the slides:

int tmp,i,j;

for (i=0; i<n-1; i++) 

{

     for (j=0; j<n-i-1; j++)

     {

          if (data[j] > data[j+1]) 

             {

                tmp = data[j];

                data[j] = data[j+1];

                data[j+1] = tmp;

             }

      }

}

And this is what I find so much easier to understand:

for (int i=1; i<=db.length-1; i++)

{

	for (int j=0; j<db.length-1; j++)

	{

		if (db[j] > db[j+1])

		{

			temp = db[j+1];

			db[j+1] = db[j];

			db[j] = temp;

		}

           }

}

The only actual difference appears in the boundaries of the "for-loops" so they must be both different implementations of bubble sort, right?

(My concern is general and not only limited to bubble sort, just used it as a way to explain what I mean, hope I didn't confuse you! :))

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
As you get used to code, you learn what differences are essential and which are just window-dressing.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
If you're really stumped, try to use an example and run the code on paper. Come up with a list of, say, ten random numbers, and follow the code step by step, rearranging the numbers as necessary. Once you notice a pattern characteristic of a certain algorithm, you can quickly narrow down which one it is.
sudo rm -rf /