Why does a sorting algoritm require nested loops ?

This is a discussion on Why does a sorting algoritm require nested loops ? within the Tech Board forums, part of the Community Boards category; thanks...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    2

    Why does a sorting algoritm require nested loops ?

    thanks

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    If you can think of a more efficient way to sort without using nested loops, I'd be interested to hear it.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Also, you can avoid nested loops if you utilize recursion, but it just adds unnecessary complexity:
    Code:
        class Program
        {
            static void Main(string[] args)
            {
                int[] array = { 7, 2, 9, 8, 3, 4 };
    
                Sort(array);
    
                // Print the now sorted array
                for (int i = 0; i < array.Length; ++i)
                    Console.Write("{0} ", array[i]);
                Console.WriteLine("");
            }
    
            private static void Sort(int[] array, int index = 0)
            {
                // Find the index (greater than or equal to the index passed in) with the smallest value
                int smallest = index;
                for (int i = index + 1; i < array.Length; ++i)
                    if (array[i] < array[smallest])
                        smallest = i;
    
                // If the passed in index doesn't contain the smallest value, swap the values at the two indexes
                if (smallest != index)
                {
                    int temp = array[smallest];
                    array[smallest] = array[index];
                    array[index] = temp;
                }
    
                // If we're not at the end of the array, sort the rest
                if (index < array.Length)
                    Sort(array, index + 1);
            }
        }
    Output...
    Code:
    2 3 4 7 8 9
    Press any key to continue . . .
    Last edited by itsme86; 09-29-2011 at 11:38 AM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help needed for sorting nested loops...segmentation error
    By starvinmarvin in forum C Programming
    Replies: 17
    Last Post: 12-01-2008, 07:13 PM
  2. Nested loops
    By zeondik in forum C# Programming
    Replies: 2
    Last Post: 10-26-2008, 06:58 AM
  3. Nested For Loops
    By smitsky in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2004, 12:58 PM
  4. nested while loops possible?
    By prepare in forum C Programming
    Replies: 15
    Last Post: 10-18-2004, 02:10 AM
  5. nested loops PLEASE HELP!!
    By scuba22 in forum C++ Programming
    Replies: 6
    Last Post: 10-08-2002, 09:31 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21