简体   繁体   中英

How to sort elements of an array using nested for-loops?

I'm taking on a programming challenge for practice and I'm having trouble figuring this one out. It might be due to the time and my current sleepiness, but I want to get this done before bed.

I want to sort the values of each element of an array in ascending order. The trick is not to use a sort() method. Here is what I have so far:

          for (int i = 0; i < freq_array.Length; i++)
        {
            for (int n = 1; n < i; n++)
            {
                if (freq_array[n] < freq_array[i])
                    freq_array[i] = freq_array[n];
            }
        }

        for (int x = 0; x < freq_array.Length; x++)
        {
            lblOutDigits.Text = "";
            lblOutDigits.Text += freq_array[x];
        }

When testing it out, I just get a '0' in the label. What the freq_array does is hold the frequency of how often certain buttons are clicked. So if I click Button3 5 times, then Button7 3 times, putting them in order I should see 33333777 - even if I clicked 3 and 7 in a random order.

You need to swap the values

     int temp;
     for (int i = 0; i < freq_array.Length; i++)
            {
                for (int n = 1; n < i; n++)
                {
                    if (freq_array[n] < freq_array[i]){
                        temp = freq_array[i];
                        freq_array[i] = freq_array[n];
                        freq_array[n] = temp;
                    }
                }
            }

This looks dodgy to me:

if (freq_array[n] < freq_array[i])
    freq_array[i] = freq_array[n];

That's just copying the value from index n to index i . You're completely losing the value which used to be at index i . I suspect you want to swap the values instead.

int[] x = { 20, 10, 50, 46, 26, 87, 25, 5, 97, 24 };
for (int i = 0; i < x.Length; i++)
{
    for (int j = i; j < x.Length; j++)
    {
        if (x[i] > x[j])
        {
            int temp;
            temp = x[i];
            x[i] = x[j];
            x[j] = temp;
        }
    }

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

        }

    }

This code helps to sort the given enumeration in ascending order. If you want to make this code to sort in descending order just change the > symbol to < symbol while comparing the values in inner for loop.

Hope this will help you to solve your query.

Alternative way to sort is using .Sort Method.

Example

Array.Sort(a);

* //If you want to sort in descending order, Write below code after sorting done using sort method. *

a=a.Reverse().ToArray();
foreach (var item in a)
{
  Console.WriteLine(item);
}
    #region Bubble Sort

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n - 1; j++)
        {
            if (a[j] > a[j + 1])
            {
                swap(ref a[j], ref a[j + 1]);
            }
        }
    }

    #endregion

    #region Insertion Sort

    for (int i = 1; i < n; i++)
    {
        int j = i;
        while (j > 0)
        {
            if (a[j - 1] > a[j])//not in order
            {
                swap(ref a[j], ref a[j - 1]);
            }
            else//in order
                break;
            j--;
        }
    }

    #endregion

    #region Selection Sort

    int smallest;
    for (int i = 0; i < n - 1; i++)
    {
        smallest = i;
        for (int j = i + 1; j < n; j++)
        {
            if (a[j] < a[smallest])
            {
                smallest = j;
            }
        }
        if (smallest != i)
            swap(ref a[i], ref a[smallest]);
    }
    #endregion

//Swap Function

    public static void swap(ref int a, ref int b)
    {
        int temp = a;
        a = b;
        b = temp;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM