View Single Post
Old 14-08-2009, 11:13   #22
punky
Inactive
 
Join Date: Jun 2003
Age: 44
Posts: 14,750
punky has a golden aurapunky has a golden aura
punky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aurapunky has a golden aura
Re: Programming Challenges?

As I have my code I might as well post it now. I've not beautified or robustified it yet:

Creating the initial random array:

Spoiler: 
Code:
            
            int[] unsorted = new int[10];
            Random random = new Random();

            for (int i = 0; i < 10; i++)
            {
                unsorted[i] = random.Next(0, 10);
            }


The way I did it at university and how it initially last night:

Spoiler: 
Code:
            int[] sorted = new int[10];
            int temp = 99;
            int loc = 0;


            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if (unsorted[j] < temp) 
                    { 
                        temp = unsorted[j]; 
                        loc = j;
                    }
                }
                sorted[i] = temp;
                unsorted[loc] = 99;
                temp = 99;
            }


And my bubble sort:

Spoiler: 
Code:
            int[] sorted = (int[])unsorted.Clone();
            Boolean swapped;
            int temp = 0;
            int i = 0;

            do
            {
                swapped = false;
                i = 0;
                while (!swapped && i < 10 - 1)
                {

                    if (sorted[i] > sorted[i + 1])
                    {
                        temp = sorted[i];
                        sorted[i] = sorted[i + 1];
                        sorted[i + 1] = temp;
                        swapped = true;
                    }
                    else
                    {
                        i++;
                    }
                }

            } while (swapped);


---------- Post added at 11:13 ---------- Previous post was at 11:08 ----------

Quote:
Originally Posted by Damien View Post
Could either have a vote, or simply just get feedback from other people.

I mean the following requirements are all important:

  • Readability (Can I understand it?)
  • Performance (How quick is it compared to other implementations?)
  • Elegance (Is it frakking Cool?)
That's decent criteria. We could also get an experienced developer to act as a judge but maybe rotate the position so everyone gets a go.
punky is offline   Reply With Quote