Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys!

I was thinking for sorting algo,i have written a small algo for this...can it be more refined...

int[] unSortedArray = { 9, 4, 5, 1,7, 3, 8, 2, 6 };
           int j = 0;
           for (int i = 0; i < (unSortedArray.Length - 1) * (unSortedArray.Length - j); i++)
           {
               if (i == unSortedArray.Length - j -1)
               {
                   i = 0;
                   j++;
               }
               if (unSortedArray[i] > unSortedArray[i + 1])
               {
                   int temp = unSortedArray[i];
                   unSortedArray[i] = unSortedArray[i + 1];
                   unSortedArray[i + 1] = temp;
               }

           }


Any thought is appreciated!!!

Regards,
Vikas
Posted
Updated 1-Oct-13 17:08pm
v2
Comments
phil.o 3-Oct-13 11:53am    
Sorting algorithms come by all kinds and performances.
The first link given by Kenneth Haughland right below has been a must for me when it comes to sorting algorithms. You can try each of them and measure their performance in your specific context, then choose which one is the most suitable.

You could proberbly find the algorithm here:
Visualization and comparison of sorting algorithms in C#[^]

Although what you are trying to do seems to be a bit overkill, as you could write a predicate to go with array sorting. Like this:
http://www.csharp-examples.net/sort-array/[^]
 
Share this answer
 
v2
Comments
phil.o 3-Oct-13 11:53am    
5'd!
Kenneth Haugland 3-Oct-13 12:41pm    
Thanks.
You can used build in function present in the c#.
Please see the below example

C#
using System;

class Program
{
    static void Main()
    {
    // Simple sort call.
    int[] values = {1,5,3,0,3,5,7 };
    Array.Sort(values);
    foreach (int value in values)
    {
        Console.Write(value);
        Console.Write(' ');
    }
    Console.WriteLine();
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900