Thread: [Jumping into C++] What is Chapter 10, Exercise 1 asking?

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    40

    [Jumping into C++] What is Chapter 10, Exercise 1 asking?

    It said:

    Turn the code that we wrote for insertionSort into an insertionSort function that works for any sized array.
    Except that the insertionSort shown in that same chapter already does that. Have a look:
    Code:
    void sort (int array[], int size)
    {
        for ( int i = 0; i < size; i++ )
        {
        int index = findSmallestRemainingElement( array, size, i );
    
    
        swap( array, i, index );
        }
    
    
    }
    The code of the findSmallestRemainingElement and swap functions were shown in the same few pages, but this already looks like it can handle an array of any size. At least, within the numbers the int type can handle.

    I could make it handle more by having the array and size args be unsigned longs instead, but thats just too easy (and the chapter wasnt even about what the various types do).

    Was this a mistake, or am I missing something, here?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The code you are referencing looks like selection sort. A proper insertion sort doesn't necessarily use a swap.

  3. #3
    Registered User
    Join Date
    Jun 2016
    Posts
    40
    Though, in the book, the sort itself was specifically referred to as an insertion sort (which made things confusing). :/ I'll read up some to better grasp the difference, to see what I can do for this assignment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-04-2016, 05:35 AM
  2. Replies: 3
    Last Post: 07-20-2016, 07:45 AM
  3. Jumping into C++ Chapter 14, exercise 6.
    By CppProgrammer88 in forum C++ Programming
    Replies: 5
    Last Post: 04-12-2016, 07:27 PM
  4. Replies: 6
    Last Post: 08-20-2012, 07:09 AM

Tags for this Thread