How to sort elements of an array using nested for-loops?
Solution 1
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;
}
}
}
Solution 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.
Solution 3
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);
}
Solution 4
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;
}
}
}
Bex
Updated on November 23, 2022Comments
-
Bex 12 months
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.
-
Sebastian Negraszus almost 11 yearsIn case you don't know that already, this sorting algorithm is called bubble sort.
-
Bex almost 11 yearsI 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.
-
-
Bex almost 11 yearsThat would make more sense. Aha. I would indeed like to swap the values to make the lower number come up first.
-
Jon Skeet almost 11 years@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... -
Bex almost 11 yearsI see, that does make more sense. Thank you. I'll keep tinkering.
-
Bex almost 11 yearsAh, this is what Jon mentioned. This makes a lot more sense. And after this I can just display freq_array using another for-loop?