You must Sign In to post a response.
  • Category: .NET

    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
  • #234987
    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

  • #235497
    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;
    }
    }
    }


This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.