473,569 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Case Insensitive Alphabetical Bubble Sort

16 New Member
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 4905
RedSon
5,000 Recognized Expert Expert
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 New Member
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 Recognized Expert Expert
Try something like this

toLower((int)Ar ray[i])) You need type cast it.
Mar 30 '07 #4
Randeh
16 New Member
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 New Member
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 Recognized Expert Expert
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
1581
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 cluster) But PostgreSQL do not work properly with UNICODE :( I have the following problems: 1) ORDER BY is not alphabetical, sorting is by symbol code...
3
6407
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. Simple code: Dim y As New ArrayList y.Add("Abc") y.Add("abc") y.Add("_bc")
10
9653
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 al.add("one") al.add("two") dim x as integer = al.indexof("ONE") I would like this to find the match and return 0
2
6518
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 student. Is bubble sort applicable in this problem?if yes, can anyone help me how to implement it..tnx..
1
5266
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 sure there's more errors but I'll figure those out if I can just get it to compile. #include <iostream> #include <cstring> using namespace std;...
0
1927
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. It basically prints out the user prompt, then after you enter the number of names, such as 3... it prints out: Enter a name: Enter a name: ...
1
5498
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 find on this map with a case sensitive AND case insensitive search. I need to be able to change this dynamically during execution. Is this...
7
3321
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 way or any gotcha's from my code below.
6
4442
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 coming back case-sensitive. I.e. WHERE user_name LIKE 'd%' would return "david" but not "Daryl" That's weirding me out- I thought sql queries were...
0
7701
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7979
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.