2

This is one of the programs, where a set of numbers are sorted into ascending order, by finding the largest number in the range between the left point and the end of the array, then moving that element into its correct index position by switching the elements. My problem is that it is not in ascending order, as the numbers in between don't get ordered, and I'm wondering how to fit that in the program. Here is my code at the moment:

#include <stdio.h>                                    /* Library inclusions */ 
#include "genlib.h" 
#include "simpio.h"
#define size 7                                        /* Constants */

void sortArray (int numbers[]);                       /* prototypes */
int indexMax (int numbers[], int low, int high);
void swap (int numbers[], int loc, int loc1);
void getArray (int numbers[]);
void displayArray (int numbers[]);

main()
{
      int numbers[size];
      getArray(numbers); 
      sortArray(numbers ); 
      displayArray (numbers); 
      getchar();
}

void getArray (int numbers[])                         /*Function getArray*/
{
     int i;

     for (i=0; i<size; i++)
     {
         printf ("Enter an integer? ");
         numbers[i]=GetInteger();
     }
}

void displayArray (int numbers[])                     /*Function displayArray*/
{
     int i;

     printf ("\n The sorted list is: \n");
     for (i=0; i< size; i++)
     {
         printf ("%d\t", numbers[i]); 
     }
}

void sortArray (int numbers[])                        /*Function sortArray*/
{
     int i , maxInd;

     for (i=0; i<size;i++)
     {
         maxInd = indexMax (numbers, i, size-1);
         swap (numbers, size-1, maxInd);
     }
}

int indexMax (int numbers[], int low, int high)       /*Function indexMax*/
{
    int i, maxInd;

    maxInd=high;
    for (i=low;i<=high;i++)
    {
        if (numbers[i]>numbers[maxInd]) 
        {
                       maxInd =i;
        }
    }
    return (maxInd);
}

void swap (int numbers[], int loc, int loc1)         /*Function swap*/
{
     int temp;

     temp=numbers[loc];
     numbers[loc]=numbers[loc1];
     numbers[loc1]=temp;
}

Thank you very much. :)

2
  • 3
    Make a promise today: You'll change your display name once you aren't a beginner. Commented Jul 4, 2013 at 12:58
  • 2
    Have you tried stepping though the code in a debugger, line by line, to help you see what might be wrong? Do it with a small array so it won't take such long time. Commented Jul 4, 2013 at 12:58

3 Answers 3

1

You SortArray function logic is wrong. You are finding maxindex from i to last index and replacing it with last index and then increment i. In first iteration the largest number reaches end thereafter in subsequent iterations, the last index only will be selected as maxindex and no change in array will be there.

Instead you always need to iterate from first index and upto one index less than previous last index.

void sortArray (int numbers[])                        /*Function sortArray*/
{
     int i , maxInd;

     for (i=size-1; i>=0;i--)
     {
         maxInd = indexMax (numbers, 0, i);
         swap (numbers, i, maxInd);
     }
}
Sign up to request clarification or add additional context in comments.

Comments

0

In indexMax function change greater than (>) to less than (>).

1 Comment

thanks alot :) but it didn't work :( However, Shashwat Kumar already solved the problem so it's okay.
0

There may be something wrong in the function sortArray()

void sortArray (int numbers[])         
{
     int i 

     int maxInd;

     for (i=0; i<size;i++)
     {
         maxInd = indexMax (numbers, i, size-i-1);
         swap (numbers, size-1-i, maxInd);
     }
}

I make a small change, and it worked!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.