15,999,481 members
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
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.

## Solution 1

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/[^]

v2
phil.o 3-Oct-13 11:53am
5'd!
Kenneth Haugland 3-Oct-13 12:41pm
Thanks.

## Solution 3

You can used build in function present in the c#.

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();
}
}```