Thread: Exercise asks me to use a subarray. What is a subarray?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    Exercise asks me to use a subarray. What is a subarray?

    I'm working on the pointer chapter in "C How to Program" and I'm having trouble interpreting an exercise.

    There is talk of developing a recursive function and the use of "subarrays." I'm not precisely sure what they mean when they say subarray. I'll write
    out the whole exercise to give the whole context, and bolden the issue. I realize this is a little long but bare with me please.


    Code:
    (Quicksort) In the examples and exercises of Chapter 6, we discussed the sorting techniques of bubble sort, bucket sort, and selection sort.  We now 
    present the recursive sorting technique called Quicksort.  The basic algorithm for a single-subscripted array of values is as follows:
    
        a) Paritioning Step: Take the first element of the unsorted array and determine its final location in the sorted array(i.e., all values to the left of the 
    element in the array are less than the element, and all values to the right of the element in the array are greater than the element).  We now have 
    one element in its proper location and two unsorted subarrays.
    
        b) Recursive Step: Perform step 1 on each unsorted subarray.
    
    Each time step 1 is performed on a subarray, another element is placed in its final location of the sorted array, and two unsorted subarrays are 
    created.  When a subarray consists of one element, it must be sorted; therefore that element is in its final location.
    
    
        The basic algorithm seems simple enough, but how do we determine the final position of the first element of each subarray.  As an example, consider 
    the following set of values (the elements in bold is the paritionting element--it will be placed in its final location in the sorted array):
    
    
        37 2 6 4 89 8 10 12 68 45
    
        a)  Starting from the rightmost element of the array, compare each element with 37 until an element less than 37 is found.  Then swap 37 and that 
    element.  The first element less than 37 is 12, so 37 and 12 are swapped.  The new array is
        12 2 6 4 89 8 10 37 68 45
    
        b) Starting from the left of the array, but beginning with the element after 12, comopare each element with 37 until an element greater than 37 is 
    found.  Then swap 37 and that element.  The first element greater than 37 is 89, so 37 and 89 are swapped.  The new array is
        12 2 6 4 37 8 10 89 68 45
    
        c) Starting from the right, but beginning with the element before 89, compare each element with 37 until an element less than 37 is found.  Then 
    swap 37 and that elmeent.  The first element less than 37 is 10, so 37 and 10 are swapped.  The new array is
        12 2 6 4 10 8 37 89 68 45
    
        d) Starting from the left, but beginning with the lement after 10, compare each element with 37 until an element greater than 37 is found.  Then 
    swap 37 and that element.  There are no more elements greater than 37, so when we compare 37 with itself, wek now that 37 has been placed in its 
    final location of the sorted array.
    
    
        Once the partition has b een applied on the array, there are two unsorted subarray.  The subarrays with values less than 37 contrains 12, 2, 6, 4 
    10, and 8.  The subarray with values greater than 37 contains 89, 68, and 45.  The sort continues with both subarrays being partitioned int he same 
    manner as the original array.
        Based on the preceding discussion, write recursive function quicksort to sort a single-subscripted integer array.  The funciton should recieve as 
    arguments an integer array, a starting subscript and an ending subscript.  Function partition should be called by quicksort to perform the partitioning 
    step.


    So what is a subarray exactly?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A subarray is a part of the whole array or a section of the array.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if you have
    int arr[10];

    Then you might call a function with
    func( arr, 10 );

    Or you might call it with
    func( &arr[4], 3 );

    The second example is a sub-array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Think of a subarray as a substring, where a substring is a portion of a string.

    "Todd" is a string
    "odd" is a substring of Todd.

    So, with an array of 10 elements, a subarray of that might be 5 elements, starting with element #2. Or, 2 elements, starting with the first, etc.

    Todd

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    182
    Ahh, so that's where pointers come in. You input the address of the split-off point in the array, and set it into a pointer.

    Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. programming exercise
    By mashour06 in forum C Programming
    Replies: 1
    Last Post: 06-01-2009, 06:22 AM
  2. Line of data input method
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 04-28-2009, 11:34 PM
  3. Can I get some idea for exercise?
    By Rokemet in forum C++ Programming
    Replies: 6
    Last Post: 11-22-2007, 09:10 AM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM