473,394 Members | 1,893 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,394 software developers and data experts.

Case Insensitive Alphabetical Bubble Sort

16
Here's my working code for simple a 2-D alphabetical bubble sort. All I need now is to just make it case insensitive (ignores capitalization) and I've been trying to throw toupper or tolower in there but it never seems to work properly. Any ideas?

Expand|Select|Wrap|Line Numbers
  1. void SortArray(char Array[MaxNames][MaxChars], int pass)
  2. {
  3.       bool swap;
  4.       int temp[MaxChars];
  5.  
  6.       do {
  7.            swap=false;
  8.            for (i=0; i<pass-1; i++)
  9.     {
  10.     if (strcmp(Array[i], Array[i+1])>0)
  11.          for(j=0; j<MaxChars-1; j++)
  12.                              {
  13.         temp[j]=Array[i][j];
  14.         Array[i][j]=Array[i+1][j];
  15.         Array[i+1][j]=temp[j];
  16.         swap=true;
  17.              }
  18.                   }
  19.             }  while(swap);
  20. }
  21.  
Mar 30 '07 #1
6 4888
RedSon
5,000 Expert 4TB
Here's my working code for simple a 2-D alphabetical bubble sort. All I need now is to just make it case insensitive (ignores capitalization) and I've been trying to throw toupper or tolower in there but it never seems to work properly. Any ideas?

Expand|Select|Wrap|Line Numbers
  1. void SortArray(char Array[MaxNames][MaxChars], int pass)
  2. {
  3.       bool swap;
  4.       int temp[MaxChars];
  5.  
  6.       do {
  7.            swap=false;
  8.            for (i=0; i<pass-1; i++)
  9.     {
  10.     if (strcmp(Array[i], Array[i+1])>0)
  11.          for(j=0; j<MaxChars-1; j++)
  12.                              {
  13.         temp[j]=Array[i][j];
  14.         Array[i][j]=Array[i+1][j];
  15.         Array[i+1][j]=temp[j];
  16.         swap=true;
  17.              }
  18.                   }
  19.             }  while(swap);
  20. }
  21.  
In this area:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. if (strcmp(Array[i], Array[i+1])>0)
  3.          for(j=0; j<MaxChars-1; j++)
  4.                              {
  5. ...
  6.  
you will want to do something like this:

Expand|Select|Wrap|Line Numbers
  1. if (strcmp(Array[i].toLower(), Array[i+1].toLower())>0)
  2.          for(j=0; j<MaxChars-1; j++)
  3.                              {
  4.  
Mar 30 '07 #2
Randeh
16
That's the part I'd though of but when I do that:

Expand|Select|Wrap|Line Numbers
  1. if (strcmp(tolower(Array[i]) , tolower(Array[i+1])) >0)
  2.      for(j=0; j<MaxChars-1; j++)
  3.  
I get two errors on each of the tolower expressions, both of which are:
error C2664: 'tolower' : cannot convert parameter 1 from 'char [16]' to 'int'

I'm not sure how to bypass this, to be honest.
Mar 30 '07 #3
RedSon
5,000 Expert 4TB
Try something like this

toLower((int)Array[i])) You need type cast it.
Mar 30 '07 #4
Randeh
16
Still trying to get it to work. Nothing like feeling retarded to encourage your comprehension of a language. =( But I feel like there's a more simple, if not roundabout answer staring me right in the face.
Mar 30 '07 #5
Randeh
16
I bypassed the entire thing like this:

Expand|Select|Wrap|Line Numbers
  1. void SortArray(char Array[MaxNames][MaxChars], int pass)
  2.      {
  3.      bool swap;
  4.      char temp[MaxChars];
  5.      int k=0;
  6.  
  7.      do {
  8.           swap=false;
  9.            for (i=0; i<pass-1; i++)
  10.                 {
  11.                 for (k=0; k<MaxChars-1; k++)
  12.                     if (tolower(Array[i][k]) > tolower(Array[i+1][k]))
  13.                          for (j=0; j<MaxChars-1; j++)
  14.                              {
  15.                  temp[j]=Array[i][j];
  16.                  Array[i][j]=Array[i+1][j];
  17.                  Array[i+1][j]=temp[j];
  18.                  swap=true;
  19.                  }
  20.        }
  21.              }  while(swap);
  22.      }
  23.  
But for some reason it only works for 4 names. Any amount over 4 names and it just hangs up on the sorting for some reason... never even prints anything beyond the UNSORTED section.
Mar 30 '07 #6
RedSon
5,000 Expert 4TB
I bypassed the entire thing like this:

Expand|Select|Wrap|Line Numbers
  1. void SortArray(char Array[MaxNames][MaxChars], int pass)
  2.      {
  3.      bool swap;
  4.      char temp[MaxChars];
  5.      int k=0;
  6.  
  7.      do {
  8.           swap=false;
  9.            for (i=0; i<pass-1; i++)
  10.                 {
  11.                 for (k=0; k<MaxChars-1; k++)
  12.                     if (tolower(Array[i][k]) > tolower(Array[i+1][k]))
  13.                          for (j=0; j<MaxChars-1; j++)
  14.                              {
  15.                  temp[j]=Array[i][j];
  16.                  Array[i][j]=Array[i+1][j];
  17.                  Array[i+1][j]=temp[j];
  18.                  swap=true;
  19.                  }
  20.        }
  21.              }  while(swap);
  22.      }
  23.  
But for some reason it only works for 4 names. Any amount over 4 names and it just hangs up on the sorting for some reason... never even prints anything beyond the UNSORTED section.
Thats pretty odd!
Mar 30 '07 #7

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

Similar topics

0
by: lis | last post by:
Hi! I need have records on different languages simultaneously on some DB for i18n of my application. Application is multilingual. I decide use for this UNICODE. (UNICODE is encoding for my...
3
by: Adam J. Schaff | last post by:
Hello. I recently noticed that the Sort method of the .NET ArrayList class does not behave as I expected. I expect 'A' < '_' < 'a' (as per their ascii values) but what I got was the opposite....
10
by: JohnR | last post by:
I have an arraylist of string values. I would like to search the arraylist to find the index of a particular string and I would like the search to be case insensitive. dim al as new arraylist...
2
by: yenra | last post by:
..how can I insert entry in a linked list and arrange my entries according to the name in alphabetical order? I need to make a student record which contains the name, yr level and course of the...
1
by: Randeh | last post by:
Using Visual Studio 2005, right now my only error (for now) is something with the function prototype that I can't figure out for the life of me. Every data type is unexpected for my function. I'm...
0
by: Randeh | last post by:
Using Visual Studio 2005. Finally got this thing to compile, but now it prints an empty blank row for the first pass, so I guess there's an error somewhere in the function I have to show the Array. ...
1
by: benhoefer | last post by:
I have been searching around and have not been able to find any info on this. I have a unique situation where I need a case sensitive map: std::map<string, intimap; I need to be able to run a...
7
by: Adrian | last post by:
Hi, I want a const static std::set of strings which is case insensitive for the values. So I have the following which seems to work but something doesnt seem right about it. Is there a better...
6
by: Derik | last post by:
Okay, I THINK this is a PHP question... I've been mucking with PHP for awhile now, but just recently I've been poking at some ajax stuff, and I ran into something confusing; my Queries were...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.