You must Sign In to post a response.

how to sort array in ascending and descending order without using in built funct in c#.net

hello friends
i have to sort an array in Ascending and descending order with out using inbuilt functions like
array.sort() method

i have tried with this code
but it is not working

int[] a = {42, 54, 1, 14, 300};
int[] b = new int[31];
int temp;
string str = "";
for (int i = 0; i <= a.Length - 1; i++)
{
for (int j = 0; j <= a.Length - 1; j++)
{
if (j > a.Length)
{

if (a[i] > a[j])
{
temp = a[i];
a[i]= a[j];
a[j] = temp;

}
}
}
}

//Array.Sort(a)

for (int j = 0; j <= a.Length - 1; j++)
{
str = str + a[j]+ "" ;
}

Response.Write(str);
}

Thanks in advance


Comments

Author: Muhilan22 May 2008 Member Level: Gold   Points : 2

for (int i = 0; i <= a.Length - 1; i++)
{
for (int j = 0; j <= a.Length - 1; j++)
{

if (a[i] > a[j])
{
temp = a[i];
a[i]= a[j];
a[j] = temp;


}
}
}

// remove third if condition and check out

Author: Sachin Kumar23 May 2008 Member Level: Gold   Points : 2

for (int i = 0; i <= a.Length - 1; i++)
{
for (int j = 0; j <= a.Length - 1; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i]= a[j];
a[j] = temp;
}
}
}