Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7680931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:13:41+00:00 2026-05-31T18:13:41+00:00

I’m trying to bubble sort a character array in alphabetic order. My code is

  • 0

I’m trying to bubble sort a character array in alphabetic order.
My code is as follows:

#define CLASS_SIZE 10
#include <stdio.h>

void bubbleSortAWriteToB(const char a[], char *b[]);

int main(void){
    char *s_letters[CLASS_SIZE];
    char letters[CLASS_SIZE] = {'a','r','p','b','r','c','x','e','w','j'};
    bubbleSortAWriteToB(letters,s_letters);
        return 0;
}

void bubbleSortAWriteToB(const char a[], char *b[]){
    char temp;
    int i,j;
    for(i=0;i<CLASS_SIZE-1;i++){
        for(j=1;j<CLASS_SIZE;j++){
            if((int)a[j-1]>(int)a[j]){
                temp = a[j];
                *b[j] = a[j-1];
                *b[j-1] = temp;

            }

    }

  }
}

It doesn’t give any kind of error but when i run it it gets stuck like it’s kinda in a inifinte loop. But from what i can see it isn’t that either. Can you help me out?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:13:43+00:00Added an answer on May 31, 2026 at 6:13 pm

    Fixing your code

    First of all, there are some pretty serious fundamental issues with your code. Before we tackle those though, let’s just fix what you have so far. Your sorting loop seemed to be half sorting the a array and half sorting the b array. you also never initialized the b array to contain any values. Here is a corrected version of your code:

    #define CLASS_SIZE 10
    #include <stdio.h>
    
    void bubbleSortAWriteToB(const char a[], char * b[]);
    
    int main(void){
        int i;
    
        // initialize array
        char * s_letters[CLASS_SIZE];
        char letters[CLASS_SIZE] = {'a','r','p','b','r','c','x','e','w','j'};
        // sort array
        bubbleSortAWriteToB(letters,s_letters);
    
        // print sorted array
        for (i=0;i<CLASS_SIZE;i++){
            printf("%c\n", *s_letters[i]);
        }
    
        return 0;
    }
    
    void bubbleSortAWriteToB(const char a[], char * b[]){
        char * temp;
        int i,j;
    
        // initialize b array to hold pointers to each element in a
        for (i=0;i<CLASS_SIZE;i++){
            b[i] = (char *)(a) + i;
        }
    
        // in-place sort the b array
        for(i=0;i<CLASS_SIZE;i++){
            for(j=i+1;j<CLASS_SIZE-1;j++){
                if(*b[j-1]>*b[j]){
                    temp = b[j];
                    b[j] = b[j-1];
                    b[j-1] = temp;
                }
            }   
        }
    }
    

    The fix was to initialize the b array with points to a, and then sort the b array in-place by comparing the corresponding values in the a array.


    Simplifying the code

    In your original code, the strategy was to have an array of pointers (b) that would point to the elements in a, and then get sorted. This was unnecessary here though, because characters are smaller than pointers, so letting b be an array of characters is more space-efficient and simpler.

    Also, your spacing was very squished-together and somewhat difficult to read. Here’s a solution that uses b as an array of characters instead of pointers, and offers improved spacing. Also, declaring the function above was not necessary. It suffices to define the function and declare it once.

    #define CLASS_SIZE 10
    #include <stdio.h>
    
    void bubbleSortAWriteToB(const char a[], char b[]){
        char temp;
        int i,j;
    
        // initialize b array to hold pointers to each element in a
        for (i = 0; i < CLASS_SIZE; i++){
            b[i] = a[i];
        }
    
        // in-place sort the b array
        for(i = 0; i < CLASS_SIZE; i++){
            for(j = i + 1; j < CLASS_SIZE - 1; j++){
                if(b[j-1] > b[j]){
                    temp = b[j];
                    b[j] = b[j-1];
                    b[j-1] = temp;
                }
            }   
        }
    }
    
    int main(void){
        int i;
    
        // initialize array
        char s_letters[CLASS_SIZE];
        char letters[CLASS_SIZE] = {'a','r','p','b','r','c','x','e','w','j'};
    
        // sort array
        bubbleSortAWriteToB(letters, s_letters);
    
        // print sorted array
        int i;
        for (i = 0; i < CLASS_SIZE; i++){
            printf("%c\n", s_letters[i]);
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.