2

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.

2
  • In case you don't know that already, this sorting algorithm is called bubble sort. Commented Dec 11, 2012 at 9:42
  • I saw it before I posted the question but when I tried it didn't work. Trying it now, still doesn't really. I'm thinking I must have missed something. Commented Dec 11, 2012 at 9:45

7 Answers 7

4

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;
                    }
                }
            }
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, this is what Jon mentioned. This makes a lot more sense. And after this I can just display freq_array using another for-loop?
2

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.

3 Comments

That would make more sense. Aha. I would indeed like to swap the values to make the lower number come up first.
@Bex: Right. So now you need to work out how to do that. Hint: you'll probably want three statements within the if statement body, using a temporary variable...
I see, that does make more sense. Thank you. I'll keep tinkering.
1
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;
        }
    }

}

Comments

1
    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);
}

Comments

0
    #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;
    }

Comments

0

//sort an array without using sort() method

let myArray = [5, 56, 8, 9, 54, 1, 3, 6, 9, 74, 57, 65, 45, 25, 88, 99, 1021, 3254, 97, 4, 369, 25, 456, 654, 9887, 564, 65787]

let empty;
for (let i = 0; i < myArray.length; i++) {
    for (let j = i; j < myArray.length; j++) {
        if (myArray[i] > myArray[j]) {
            empty = myArray[i];
            myArray[i] = myArray[j];
            myArray[j] = empty;
        }

    }
}
console.log(myArray)

1 Comment

Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?
0

This works

for i in range(len(list)):
    for j in range(len(list)-1):
        if list[i] < list[j]:
            list[j], list[i] = list[i], list[j]
print(list)

1 Comment

It would be better to explain what your code is doing, how it solves OP's question, and how it differs from the already existing answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.