Question

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. :)

Was it helpful?

Solution

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);
     }
}

OTHER TIPS

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

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!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top