sorting an array

hi guys I am using a quick sort to sort elements in an array but I have across a problem,my program when run displays a blank screen instead of the order of the letters alphabetically in "Hello"

I'm trying to say if a letter is a capital check if it is less than the other letter -32 since small letters are 32 less than capitals

thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  void order(char *str,int size){


     bool sorted = false;

     while(!sorted){

        sorted = true;

        for(int i = 0; i < size-1; i++){

            if(str[i] >= 'A' && str[i] <= 'Z'){

                if((str[i+1])-32 < str[i]){

                char temp = str[i];
                str[i] = str[i+1];
                str[i+1] = temp;

                sorted = false;
            }
            }
            else if(str[i+1] < str[i]){

                char temp = str[i];
                str[i] = str[i+1];
                str[i+1] = temp;

                sorted = false;
            }
        }
     }
}
Last edited on
I'm trying to say if a letter is a capital check if it is less than the other letter -32 since small letters are 32 less than capitals
You're saying what?
letters in the ascii table,uppercase are 32 less than than lowercase so I'm checking to see if str[i] (if its uppercase) is less than str[i+1] -32(lower case)

but it's not working
Last edited on
I tried adding a continue statement in but still prints blank when I have a capital letter in a string such as acB but if they are all lowercase acb it sorts them fine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

void order(char *str,int size){


     bool sorted = false;

     while(!sorted){

        sorted = true;

        for(int i = 0; i < size-1; i++){

              if(str[i] >= 'A' && str[i] <= 'Z'){

                if((str[i+1])-32 < str[i]){

                char temp2 = str[i];
                str[i] = str[i+1];
                str[i+1] = temp2;
                sorted = false;
                continue; // thought this may have fixed it but no luck

         }
              }
              else if(str[i+1] < str[i]){

                char temp = str[i];
                str[i] = str[i+1];
                str[i+1] = temp;

                sorted = false;
            }
        }
     }
     }



updated code got rid of the else if changed it to an if but still does not work,still prints nothing just a blank console window


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

void order(char *str,int size){


     bool sorted = false;

     while(!sorted){

        sorted = true;

        for(int i = 0; i < size-1; i++){

              if(str[i] >= 'A' && str[i] <= 'Z'){

                if((str[i+1])-32 < str[i]){

                char temp2 = str[i];
                str[i] = str[i+1];
                str[i+1] = temp2;
                sorted = false;
                continue;
         }
              }
              if(str[i+1] < str[i]){

                char temp = str[i];
                str[i] = str[i+1];
                str[i+1] = temp;

                sorted = false;
            }
        }
     }
     }


int main()
{
    char str[] = "aCb";
    int size = sizeof(str) / sizeof(char);
    order(str,size-1);
    cout << str << endl;
}
Last edited on
anybody have any idea?
I just don't understand how this block of code isn't doing what I thought it was doing


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

while(!sorted){

        sorted = true;

        for(int i = 0; i < size-1; i++){

              if(str[i] >= 'A' && str[i] <= 'Z'){

                if((str[i+1])-32 < str[i]){

                char temp2 = str[i];
                str[i] = str[i+1];
                str[i+1] = temp2;
                sorted = false;
                continue;
         }
              }
              if(str[i+1] < str[i]){

                char temp = str[i];
                str[i] = str[i+1];
                str[i+1] = temp;

                sorted = false;
            }
        }
     }



I want the letters to be in order no matter if they are lower or upper case so lets say we have the string aCb and C is now i,i+1 is b the first if statement will be true because i is greater than A and less than Z then second if statement will run because B is less than C swapping them and then continuing to the next iteration

but when I run the code it just prints a blank console window instead of the string
ok I figured out what was wrong,I got myself into an infinite loop if anybody ever comes across the same problem here is why my code is not working in the manner I expected,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

void order(char *str,int size){


     bool sorted = false;

     while(!sorted){

        sorted = true;

        for(int i = 0; i < size-1; i++){

        cout << "loop" << endl; // will keep on printing this line
              if(str[i] >= 'A' && str[i] <= 'Z'){

                if((str[i+1])-32 < str[i]){

                char temp2 = str[i];
                str[i] = str[i+1];
                str[i+1] = temp2;
                sorted = false;        // this block of code will cause an infinite loop for example if we have aCb,when i = 0
                        //str[i] = a and str[i+1] = C str[i] is not greater than 'A' and less than 'Z' so this if block won't run
                        // the next if block WILL run because i+1 which is C is less than 'a' (ASCII values) so they will be
                        // swapped and sorted set to false so now str is Cab,i now = 1 since i is not >= 'A' and less than 'Z'
                        // first if will not run now next if gets evaluated str[i+1] = b and str[i] = C b is not less than a
                        // so this if does not execute for loop ends and sorted still equals false so while loop continues again
                        // for loop runs again i = 0 this time 0 is C so this if statement is true then the next if statement is 
                        // tested, i+1 is a - 32 is A,A is less than C so C and a are swapped and sorted set to false now the
                        // string is back to aCb the continue statement goes to the next iteration and i = 1,str[i] = C and
                        // str[i+1] = b the first if statement is true then the second is tested if B < C which is true so the second
                        // if runs and swaps C and b so now the string is abC ,sorted is set to false,the for ends and since sorted is
                        // false the while loop executes again,the first if does not run because str[i] 'a' is not >= 'A' and 
                        // <= 'Z'  the second if statement does not execute either because b is not less than a
                        // i now = 1 the first loop does not execute because b is not >= 'A' and <= 'Z' but the second if statement
                        // will execute because C (ascii value) is less than b so b and C are swapped and sorted set to true
                        // str now = aCb yet again! since sorted = false the while loop will run again this pattern now continues
                        // over and over again in an infinite loop
                continue;
         }
              }
              if(str[i+1] < str[i]){

                char temp = str[i];
                str[i] = str[i+1];
                str[i+1] = temp;

                sorted = false;
            }
        }
     }
     cout << "hey " << endl; // line will never be reached
     }


now to figure out a solution,if anybody wants to give me some ideas on how I can attack this,they are more than welcome.

thanks
Do you want to perform a case insensitive sort? If so, I think the problem is you consider only one character each time, and not the next one. When you compare characters, you need to check if both are uppercase or lowercase.

Please let me know if the following example can be of any inspiration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <limits>

void order(char *str, size_t size);
void waitForEnter();

int main()
{
    char str[] = "aCb";
    size_t size = sizeof(str) / sizeof(char);
    order(str, size);
    std::cout << str << '\n';
    waitForEnter();
}

void order(char *str, size_t size)
{
    bool sorted = false;
    while(!sorted) {
        sorted = true;
        for(size_t i = 0; i < size-1-1; ++i) {
            char one { str[i] }, two { str[i+1] };
            if(str[i]   >= 'A' && str[i]   <= 'Z') { one += 32; }
            if(str[i+1] >= 'A' && str[i+1] <= 'Z') { two += 32; }
            if(two < one) {
                char temp = str[i];
                str[i] = str[i+1];
                str[i+1] = temp;
                sorted = false;
            }
        }
    }
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Output:
abC

Press ENTER to continue...

Topic archived. No new replies allowed.