•

Check out our Community Blogs

Register and join over 40,000 other developers!

### Recent Status Updates

• phi

I love this community !

• JackJames

hi i am jack i am seo expert jack james would love you to read new post

# Why isn't my Selection Sort C code working?

selection sort algorithm c

Best Answer AamirYousafi, 13 July 2016 - 11:23 PM

fkl, thanks for the help.  I ran it printing the array as the numbers were being swapped, after each iteration, and I found that maxIndex needed to be reset to 0 after each iteration, based on the way I had set up the logic ...  I feel like an idiot!  Anyway, thanks much for the suggestions.  Below is the working code:

```/* includes and macros */
// required for input / output with minGW GCC
#include <stdio.h>
#define printf __mingw_printf

/* global declarations */
static unsigned short m, n;

/* function prototypes */
void selectionSortMax(int []);

int main()
{
/* variable array declaration */
printf("How many integers do you want sorted by Selection Sort Max? ");
scanf("%hu", &n);
int arrayInput[n];

/* markers */
m = n;

/* array input */
printf("\nPlease enter the integers one by one, pressing the 'Enter' key after each input: ");
for(register unsigned short i = 0; i <= n-1; i++)
scanf("%d", &arrayInput[i]);

/* selection and sort using the Selection Sort Max algorithm */
selectionSortMax(arrayInput);

/* print the sorted array */
printf("\n");
for(register unsigned short i = 0; i <= n-1; i++)
printf("[%d] ", arrayInput[i]);

return 0;
}

/* function definitions */
void selectionSortMax(int *cursor)
{
int temp;
for(unsigned short maxIndex = 0; m >= 2; m--)
{
// find max
for(register unsigned short i = 0; i <= m-2; i++)
{
if(cursor[i] > cursor[i+1] && cursor[maxIndex] < cursor[i])
maxIndex = i;
else if(cursor[i+1] > cursor[i] && cursor[maxIndex] < cursor[i+1])
maxIndex = i+1;
}

// swap the largest integer with the last one in the array
if(maxIndex != m-1)
{
temp = cursor[m-1];
cursor[m-1] = cursor[maxIndex];
cursor[maxIndex] = temp;
}

maxIndex = 0;
}
}```
Go to the full post

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

### #1 AamirYousafi

AamirYousafi

CC Lurker

• New Member
• 4 posts

Posted 11 July 2016 - 01:44 AM

Hello everyone.  I am trying to complete a simple selection sort algorithm program in C.  Compiling the code below gives no errors or warnings and the program seems to run fine but the problem must be in the logic because it is not sorting correctly.  I don't know what's wrong with me but I can't seem to figure out where the defect is exactly and I've been through this many times.  I hope someone has the time to go through it and let me know where the problem is.  It's not long.  Thanks in advance.

```/* includes and macros */
// required for input / output with minGW GCC
#include <stdio.h>
#define printf __mingw_printf

/* global declarations */
int *start, *finish, *cursor, *finish2;

/* function prototypes */
void selectionSortMax(int []);

int main()
{
/* variable array declaration */
unsigned short n;
printf("How many integers do you want sorted by Selection Sort Max? ");
scanf("%hu", &n);
int arrayInput[n];

/* pointing the pointers */
cursor = start = arrayInput;
finish2 = finish = start+(n-1);

/* array input */
printf("\nPlease enter the integers one by one, pressing the 'Enter' key after each input: ");
for(; cursor <= finish; cursor++)
scanf("%d", cursor);

/* selection and sort using the Selection Sort Max algorithm */
cursor = start;
selectionSortMax(cursor);

/* print the sorted array */
printf("\n");
for(cursor = start; cursor <= finish; cursor++)
printf("[%d] ", *cursor);

return 0;
}

/* function definitions */
void selectionSortMax(int *cursor)
{
for(int *maxIndex = start, temp; finish2 >= start+1; finish2--)
{
// find max
for(cursor = start; cursor <= finish2-1; cursor++)
{
if(*cursor > *(cursor+1) && *maxIndex < *cursor)
maxIndex = cursor;
else if(*(cursor+1) > *cursor && *maxIndex < *(cursor+1))
maxIndex = cursor+1;
}

// swap the largest integer with the last one in the array
if(*maxIndex != *finish2)
{
temp = *finish2;
*finish2 = *maxIndex;
*maxIndex = temp;
}
}
}```

### #2 fkl

fkl

CC Devotee

• Senior Member
• 417 posts

Posted 13 July 2016 - 11:36 AM

A few immediate mistakes that i could see

finish2 is a pointer to int, not an int. So both the terminating condition and decrement in your for loop aren't achieving what's intended.

Also where your start increment i.e. current index moves forward in the array? I don't see it happening any where.

You first need to fix these two things to even iterate through the whole set of nested loops. Whether that correctly sorts or not comes latter.

It is a good situation to debug / dry run your code for array of just 3 or 4 elements.

Edited by fkl, 13 July 2016 - 11:38 AM.

Today is the first day of the rest of my life

### #3 AamirYousafi

AamirYousafi

CC Lurker

• New Member
• 4 posts

Posted 13 July 2016 - 10:52 PM

fkl, actually, someone told me to get rid of the pointers all together as they weren't needed and made the program and its processing more complex and costly.  After making the adjustments, my code looks like what's below.

Also, after some testing with only 4 - 6 integers, sometimes, it sorts perfectly; other times, it does not.

```/* includes and macros */
// required for input / output with minGW GCC
#include <stdio.h>
#define printf __mingw_printf

/* global declarations */
static short m, n;

/* function prototypes */
void selectionSortMax(int []);

int main()
{
/* variable array declaration */
printf("How many integers do you want sorted by Selection Sort Max? ");
scanf("%hu", &n);
int arrayInput[n];

/* markers */
m = n;

/* array input */
printf("\nPlease enter the integers one by one, pressing the 'Enter' key after each input: ");
for(register unsigned short i = 0; i <= n-1; i++)
scanf("%d", &arrayInput[i]);

/* selection and sort using the Selection Sort Max algorithm */
selectionSortMax(arrayInput);

/* print the sorted array */
printf("\n");
for(register unsigned short i = 0; i <= n-1; i++)
printf("[%d] ", arrayInput[i]);

return 0;
}

/* function definitions */
void selectionSortMax(int *cursor)
{
int temp;
for(unsigned short maxIndex = 0; m >= 2; m--)
{
// find max
for(register unsigned short i = 0; i <= m-2; i++)
{
if(cursor[i] > cursor[i+1] && cursor[maxIndex] < cursor[i])
maxIndex = i;
else if(cursor[i+1] > cursor[i] && cursor[maxIndex] < cursor[i+1])
maxIndex = i+1;
}

// swap the largest integer with the last one in the array
if(maxIndex != m-1)
{
temp = cursor[m-1];
cursor[m-1] = cursor[maxIndex];
cursor[maxIndex] = temp;
}
}
}```

Edited by AamirYousafi, 13 July 2016 - 11:02 PM.

### #4 AamirYousafi

AamirYousafi

CC Lurker

• New Member
• 4 posts

Posted 13 July 2016 - 11:23 PM   Best Answer

fkl, thanks for the help.  I ran it printing the array as the numbers were being swapped, after each iteration, and I found that maxIndex needed to be reset to 0 after each iteration, based on the way I had set up the logic ...  I feel like an idiot!  Anyway, thanks much for the suggestions.  Below is the working code:

```/* includes and macros */
// required for input / output with minGW GCC
#include <stdio.h>
#define printf __mingw_printf

/* global declarations */
static unsigned short m, n;

/* function prototypes */
void selectionSortMax(int []);

int main()
{
/* variable array declaration */
printf("How many integers do you want sorted by Selection Sort Max? ");
scanf("%hu", &n);
int arrayInput[n];

/* markers */
m = n;

/* array input */
printf("\nPlease enter the integers one by one, pressing the 'Enter' key after each input: ");
for(register unsigned short i = 0; i <= n-1; i++)
scanf("%d", &arrayInput[i]);

/* selection and sort using the Selection Sort Max algorithm */
selectionSortMax(arrayInput);

/* print the sorted array */
printf("\n");
for(register unsigned short i = 0; i <= n-1; i++)
printf("[%d] ", arrayInput[i]);

return 0;
}

/* function definitions */
void selectionSortMax(int *cursor)
{
int temp;
for(unsigned short maxIndex = 0; m >= 2; m--)
{
// find max
for(register unsigned short i = 0; i <= m-2; i++)
{
if(cursor[i] > cursor[i+1] && cursor[maxIndex] < cursor[i])
maxIndex = i;
else if(cursor[i+1] > cursor[i] && cursor[maxIndex] < cursor[i+1])
maxIndex = i+1;
}

// swap the largest integer with the last one in the array
if(maxIndex != m-1)
{
temp = cursor[m-1];
cursor[m-1] = cursor[maxIndex];
cursor[maxIndex] = temp;
}

maxIndex = 0;
}
}```

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download