Click here to Skip to main content
15,562,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm very new to coding and have been staring at this bit of code for hours...

I understand how the parameters in the for statement work when just printing out a range of numbers to the console but in this example it is sorting the values in ascending order.

To me the parameters look very similar, I can see that they're different but it does look as though it is using the same principle. I was just hoping someone could explain how this actually sorts the array into ascending order, I cannot work it out.

What I have tried:

C#
 const int arrayLength = 5;
           int[] sortArray = new int[arrayLength];
           for (int i = 0; i < arrayLength; i++)
           {
               Console.WriteLine("Enter value: ");
               sortArray[i] = Convert.ToInt32(Console.ReadLine());
           }

           Console.WriteLine("Now sort it: ");
           for (int j = 0; j < arrayLength; j++)
           {
               for (int k = j + 1; k < arrayLength - 1; k++)
               {
                   if (sortArray[j] > sortArray[k])
                   {
                       int big = sortArray[j];
                       sortArray[j] = sortArray[k];
                       sortArray[k] = big;
}
               }
           }

           Console.WriteLine("Now print it: ");
           for (int l = 0; l < sortArray.Length; l++)
           {
               Console.WriteLine(sortArray[l]);
           }
C++

Posted
Updated 9-Mar-18 5:12am

This is your homework, but ... that's an odd sort algorithm.
The way I'd do it (well, if I had to write a basic sort algorithm) would be to do a bubble sort: Bubble sort - Wikipedia[^]
Yours doesn't do that - it kinda stops too early, so the output order is a little odd.
But it's easy to solve! Just change this:
for (int k = j + 1; k < arrayLength - 1; k++)
To this:
for (int k = j + 1; k < arrayLength; k++)
 
Share this answer
 
Comments
Member 13717732 9-Mar-18 10:40am    
I was given this exercise but when I looked at the solution for it, it completely threw me... Wasn't expecting it to be as complicated as it was as I did think there would be a fairly simple way to do it. An alternate to this method is probably more useful to me than trying to understand how it actually works... I will have a look at how to do a bubble sort - thanks for the suggestion!
OriginalGriff 9-Mar-18 11:08am    
I think it is a bubble sort you are doing: it's called that because the largest items "bubble up" to the top.

(I was confused for a moment because the end condition for the inner loop was too short, so it didn't work very well at all.)

And if you think bubble is complicated ... wait till you get to Quick Sort! :laugh:
Bubble is good for a "quick and dirty" solution that won't get used much and where there are few items out of sequence, but there are quite a few ways to do this. One simple improvement is to "bubble" in two directions - first "up" the array in the inner loop, then "down", noting where you made the last change in each direction.
It's called a "bubble sort", now you know what it's called you can google for it, there are plenty of articles\animations that explain it

Algorithms Lesson 1: Bubblesort - YouTube[^]
 
Share this answer
 
You can use Linq:
using System;
using System.Collections.Generic;
using System.Linq;
					
public class Program
{
	public static void Main()
	{
		List<int> li = new List<int>();
		li.Add(1);
		li.Add(2);
		
		var lidesc = li.OrderByDescending(i => i);
		
		
		foreach (var l in lidesc)
		{
			Console.WriteLine("number " + l);
		}
	}
}
 
Share this answer
 
Comments
OriginalGriff 9-Mar-18 10:32am    
Um ... "Sorting an array into ascending order" :laugh:
And Array.Sort will do that without confusing beginners ... might not get him any marks though.
RickZeeland 9-Mar-18 11:33am    
Friday afternoon, what do you expect :)
OriginalGriff 9-Mar-18 11:52am    
Sobriety? :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900