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. -
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);
-
-
}
-
}
1 3389
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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
|
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...
| |