Mike Wahler wrote:
[color=blue]
> "ritchie" <ritchie_s01@yahoo.com> wrote in message
> news:3bee6ba6.0311251032.7018c288@posting.google.c om...
>[color=green]
>>Hi Mike,
>>
>>Thanks for the response.
>>
>>I did try debugging the program before but got lost somewhere in the
>>middle.
>>I had the code indented but it must have got messed up along the way.[/color]
>
>
> I thought that might have been the case. E.g. If your code
> contains tabs, they'll often be mangled or omitted by a
> newsreader. Change any tabs to sequences of spaces before
> posting (many/most editors have an option to do this).
>
>[color=green]
>>The program works if the array is: 4,3,2,1 but if it is , 4,2,3,1
>>there is a problem.[/color]
>
>
> Then you've still some more work to do. :-)
>
> I still think a good way to track your problem is
> to inspect the array periodically during the sort,
> as I did in my example.
>
> Post back if you're still stuck.
>[/color]
It is just a loop problem in function sort.
The function was written as:
[color=blue]
> void sort( int iTmpArr[], int iMax)
>{
> int i, iSmallestElement, iSortElement, iTemp;
>
> iTemp = 0; /* MKW */
> printf("\n****************Selection Sort****************\n");
>
> for( iSortElement=0; iSortElement <iMax-1; iSortElement++ )
> {
> printf("iSortElement == %d\n", iSortElement); /* MKW */
> iSmallestElement = iSortElement;[/color]
[color=blue]
>/* ******* HERE IS THE PROBLEM ****** */
>/* for( i=iSortElement +1; i <= iMax; i++ ) /* /* MKW removed */
> for( i=iSortElement +1; i < iMax; i++ ) /* MKW */
>
> {
> printf("i == %d\n", i); /* MKW */
>
> printf("iSmallestElement == %d\n", /* MKW */
> iSmallestElement); /* MKW */
>
> printf("iTmpArr[%d] == %d\n", /* MKW */
> i, /* MKW */
> iTmpArr[i]); /* MKW */
>
> printf("iTmpArr[%d] == %d\n", /* MKW */
> iSmallestElement, /* MKW */
> iTmpArr[iSmallestElement]); /* MKW */
>
> printf("iTmpArr[%d] < iTmpArr[%d] == %s\n", /* MKW */
> i, /* MKW */
> iSmallestElement, /* MKW */
> iTmpArr[i] < iTmpArr[iSmallestElement] /* MKW */
> ? "true" : "false"); /* MKW */
>
>
> if( iTmpArr[ i ] < iTmpArr[ iSmallestElement ] )
> iSmallestElement = i;
>
> printf("iSmallestElement == %d\n", /* MKW */
> iSmallestElement); /* MKW */
>
> printf("iSortElement == %d\n", /* MKW */
> iSortElement); /* MKW */
>
> printf("iSmallestElement != iSortElement == %s\n", /* MKW >*/
> iSmallestElement != iSortElement /* MKW >*/
> ? "true" : "false"); /* MKW >*/[/color]
[color=blue]
> if( iSmallestElement != iSortElement )[/color]
Here is the problem. This if statement should not be in this
second(nested) for loop. It should be in the first for loop.
See the correction below.
[color=blue]
> {
> iTemp = iTmpArr[ iSmallestElement ];
> iTmpArr[ iSmallestElement ] = iTmpArr[ iSortElement ];
> iTmpArr[ iSortElement ] = iTemp;
> }[/color]
[color=blue]
> printf("iSortElement == %d\n", iSortElement); /* MKW */
> printf("iSmallestElement == %d\n", iSmallestElement); /*
>MKW
>*/
> printf("iTemp == %d\n", iTemp); /* MKW */[/color]
[color=blue]
> show_array(iTmpArr, iMax); /* MKW */
> printf(("Press return")); /* MKW */
> fflush(stdout); /* MKW */
> getchar(); /* MKW */
> putchar('\n'); /* MKW */
> }
> }
> printf("Sorted array\n");[/color]
[color=blue]
> for(i=0; i<iMax;i++)
> printf("%d ", iTmpArr[i]);[/color]
[color=blue]
> printf("\n");
>}[/color]
\************************************************* ******/
The corrected version:
#include <stdio.h>
#define MAX 4
void sort(int [], int ); //sort function
void show_array(int *array, size_t elems);
int main(void) /* MKW fixed return type */
{
int iArr[MAX] = {4,2,3,1}; /* MKW */
int iTmpArr[MAX]; // tmp array...for copying
int i;
for(i=0;i<MAX;i++)
iTmpArr[i] = iArr[i];
sort(iTmpArr, MAX); //call selection sort
show_array(iTmpArr,MAX);
return 0; /* MKW main() must return an int */
}
void show_array(int *array, size_t elems) /* MKW */
{
size_t i = 0; /* MKW */
puts("array contents:"); /* MKW */
for(i = 0; i < elems; ++i) /* MKW */
printf("array[%d] == %d\n", i, array[i]); /* MKW */
putchar('\n'); /* MKW */
}
void sort( int iTmpArr[], int iMax)
{
int i, iSmallestElement, iSortElement, iTemp;
for( iSortElement=0; iSortElement <iMax-1; iSortElement++ )
{
iSmallestElement = iSortElement;
for( i=iSortElement +1; i < iMax; i++ ) /* MKW */
{
if( iTmpArr[ i ] < iTmpArr[ iSmallestElement ] )
iSmallestElement = i;
}
if( iSmallestElement != iSortElement )
{
iTemp = iTmpArr[ iSmallestElement ];
iTmpArr[ iSmallestElement ] = iTmpArr[ iSortElement ];
iTmpArr[ iSortElement ] = iTemp;
}
}
}
--
Al Bowers
Tampa, Fl USA
mailto:
xabowers@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/