Can someone please explain this array sorting program?

This is a discussion on Can someone please explain this array sorting program? within the C Programming forums, part of the General Programming Boards category; Code: #define _CRT_SECURE_NO_WARNINGS#include <stdio.h> int main() { int i,j; double a[9]; printf ("This program determines the middle value\n"); printf ("of ...

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

    Can someone please explain this array sorting program?

    Code:
    #define _CRT_SECURE_NO_WARNINGS#include <stdio.h>
    
    
    int main()
    {
        int i,j;
        double a[9];
    
    
        printf ("This program determines the middle value\n");
        printf ("of nine real numbers that you enter from the keyboard\n");
    
    
        for (i = 0; i < 9; i++)
        {
            printf ("%d: ", i);
            scanf("%lg", &a[i]);
        }
    
    
        // Print the numbers
        printf ("You entered:\n");
        for (i = 0; i < 9; i++)
        {
            printf ("%d:  %lg\n", i, a[i]);
        }
        printf ("\n\n");
    
    
    
    
    
    
        // Sort the array
        for (i = 0; i < 8; i++)
        {
            for (j = i+1; j < 9; j++)
            {
                if (a[j] < a[i])
                {
                    // Exchange them
                    double temp = a[j];
                    a[j] = a[i];
                    a[i] = temp;
                }
            }
        }
    
    
        printf ("Sorted array:\n");
        for (i = 0; i < 9; i++)
        {
            printf ("%d:  %lg\n", i, a[i]);
        }
        printf ("\n\n");
    
    
        printf ("The middle value is %lg\n", a[4]);
        getchar();
        getchar();
        return 0;
    }
    I don't understand the part about sorting the array. I know I am misunderstanding it, but I have been trying to understand it for quite some time and its not making sense to me.

    For example: Say these are my array values
    a[0]=5
    a[1]=3
    a[2]=2
    a[3]=1
    a[4]=7
    a[5]=8
    a[6]=9
    a[7]=2
    a[8]=3

    This is how I am interpreting the program:

    Is a[1] < a[0]? If so, switch them.

    Since a[0] = 5 and a[1] = 3 this is true. Thus, I switch them and get:

    a[0] = 3 and a[1] = 5.

    Then i and j are both incremented to get the following:

    is a[2] < a [1]? If so switch them.

    a[1] is now 5, correct? Thus, a[1] IS larger than a[2]. So we switch them.

    a[1] = 2 and a[2] = 5.

    I continued, but there is already a problem here.

    So far the array is a[0]=3, a[1] = 2, and a[2] = 5.

    This is not sorted already from low to high. How exactly is this program taking values from a[8] for example and comparing them with a[2]?

    I am just having trouble understanding this. I apologize for the long post, but I would really appreciate it if anyone can tell me exactly whats going on.

    Thanks!
    -Frankie

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They aren't both incremented:
    Code:
        for (i = 0; i < 8; i++)
        {
            for (j = i+1; j < 9; j++)
            {
                if (a[j] < a[i])
                {
                    // Exchange them
                    double temp = a[j];
                    a[j] = a[i];
                    a[i] = temp;
                }
            }
        }
    First pass:
    Code:
    if a1 < a0
        swap a1, a0
    Second pass of inner loop:
    Code:
    if a2 < a0
        swap a2, a0
    ...
    Code:
    if a8 < a0
        swap a8, a0
    Outer loop increments. Inner loop starts over:
    Code:
    if( a2 < a1 )
        swap a2, a1
    Repeat until done.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Inside the sort you are declaring double temp ... move the declaration to the top of main and jsut use temp = inside the sort.

    Also... your outer loop ends at 8, but it needs to run the full array, the inner loop should be limited to only unsorted values.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Quote Originally Posted by Frankie15 View Post
    Then i and j are both incremented to get the following:
    That line seems to be the source of your misunderstanding. The j loop is nested inside the i loop. That means j increments from i+1 all the way to 8, then i increments, then j resets to the new i+1, and goes all the way up to 8 again before i increments again. A table of values for i and j:
    Code:
    i j
    - -
    0 1
    0 2
    0 3
    ...
    0 8
    1 2
    1 3
    ...
    1 8
    2 3
    ...
    7 8

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    16
    Thanks for the tables quzah and anduril. I think I understand it. It does much more than I originally thought it did.

    Really appreciate the quick responses. You guys were very helpful!
    Last edited by Frankie15; 10-03-2011 at 05:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-23-2011, 06:27 AM
  2. what is the program output please explain
    By sunil17 in forum C Programming
    Replies: 2
    Last Post: 09-02-2010, 08:34 AM
  3. Can someone explain this program to me?
    By jaja009 in forum C Programming
    Replies: 4
    Last Post: 03-28-2010, 03:10 PM
  4. c++ array question please explain
    By corbinc in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2009, 02:02 PM
  5. Could someone explain this program?
    By Beachblue in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:51 AM

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