473,322 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Selection Sort

Okay, I have this homework assignment which is to sort a 2D array of characters using selection sort.

I've written the selection sort but I can't seem to get it working. (it's not sorting!)

Please tell me what I'm doing wrong, thanks.

Expand|Select|Wrap|Line Numbers
  1. void selectionSort(char array[][SIZE], int rows)
  2. {
  3.     const int size = 17;
  4.     int startScan, minIndex, index;
  5.     char minValue[NUM_NAMES][size];
  6.  
  7.     for (startScan = 0; startScan < (rows - 1); startScan++)
  8.     {
  9.         minIndex = startScan;
  10.         strncpy(minValue[0], array[startScan], 20);
  11.         for (index = startScan + 1; index < rows; index++)
  12.         {
  13.             if (strcmp(array[index], minValue[NUM_NAMES]) < 0)
  14.             {
  15.                 strncpy(minValue[0], array[index], 20);
  16.                 minIndex = index;
  17.  
  18.             }    
  19.         }
  20.     strncpy(array[startScan], array[minIndex] , 20);
  21.  
  22.     }
  23. }
Nov 18 '07 #1
1 3389
weaknessforcats
9,208 Expert Mod 8TB
Originally Posted by
void selectionSort(char array[][SIZE], int rows)
{
const int size = 17;
int startScan, minIndex, index;
char minValue[NUM_NAMES][size];

for (startScan = 0; startScan < (rows - 1); startScan++)
{
minIndex = startScan;
strncpy(minValue[0], array[startScan], 20);
for (index = startScan + 1; index < rows; index++)
{
if (strcmp(array[index], minValue[NUM_NAMES]) < 0)
{
strncpy(minValue[0], array[index], 20);
minIndex = index;

}
}
strncpy(array[startScan], array[minIndex] , 20);

}
}
Several things:
1) What is the 17 for??? You never use it.
2) Your function argument is an array of strings. That means its an array of char*
3) Your minIndex should start wirth array[0] and avance to array[1] on the next cycle of the loop
4) When you compare strings, you swap the pointers. You cannot swap the strings themselves since they are different lengths. I expect that's what the 20 was for. A hack to avoid a crash.
5) the outer loop shoukld run from 0 to rows.
6) the inner loop should frim from the outer loop index to rows.
Nov 21 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: shaveta | last post by:
pls help me to write a program in c that reads n elements of type float, stores them in a linked list ,sort it in the descending order using selection sort methods, and finally output the sorted...
1
by: gemacjr1201 | last post by:
I need hints for an algorithm using string compare in a selection sort. I will be sorting lastnames and first namesalphbetically. if the selection sort encounters the same last name it then goes to...
1
by: ShaveDave27 | last post by:
Hi, I've created a Person Class with a comparable interface. And i've created an ArrayList People with varaibles from the person class in - First_name, Surname, Month(Birthday), Day(Birthday). Now...
12
by: ShaveDave27 | last post by:
HI, I've created a Comparable interface class Person. Which has variables First_name, surname, month(Of birth), day(Of birth) , and birthday(which i have created from month and day). I have a...
0
Ganon11
by: Ganon11 | last post by:
Earlier, mmccarthy was kind enough to post a short article explaining the Bubble Sort. As she said, this is a relatively slow sorting algorithm. Here, I will attempt to present a slightly faster...
1
by: fatimaballes | last post by:
how to apply bubble and selection sort in assembly?this is for my subject IT103Lab.. were using notepad++ for encoding then amke everything appear in the command prompt.. another thing... our...
1
mfshake
by: mfshake | last post by:
i am using only a driver and not a server. I know how to do selection sort for ints but can't figure out how to do it for Strings. I want to sort it my first letter only Here is my code that i...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.