1
#include <stdio.h>
#include <math.h>

void swap(char* x, char* y)
{
  char temp;
  temp = *x;
  *x = *y;
  *y = temp;
}

int compare(char a, char b)
{
  if((int)a > (int)b)
  {
    return 1;
  }
  return -1;
}

int bubbleSort(char *a, int n, int (*compare) (char, char))
{
  int i, j;
  for(i = 0; i < n; i++)
  {
    for(j = 0; j < n - 1; j++)
    {
      if(compare(a[j], a[j + 1]) > 0)
      {
        swap(&a[j], &a[j + 1]);
      }
    }
  }
} 

void main()
{
  char* name = "Stackoverflow";
  int i;
  bubbleSort(name, sizeof(name) / sizeof(char), compare);
  for(i = 0; sizeof(name) / sizeof(char); i++)
    printf("%c ", name[i]);
}

I have no clue about why this code is giving segmentation fault, I know the case when the segmentation fault occurs,that is, when the application tries to access the memory location out of its dedicated memory.

muXXmit2X
  • 2,745
  • 3
  • 17
  • 34
CocoCrisp
  • 807
  • 1
  • 9
  • 26
  • The segmentation fault occurs because you are trying to sort (and hence modify) a string literal, which you can't. Change `name` in `main` to a chacater array: `char name[] = "Stackoverflow";` You'll also want to use `strlen` to determine the length of the string; with `sizeof` of the array you'll sort the null terminator to the front. – M Oehm Mar 24 '16 at 07:50
  • @MOehm No change in the output except some random characters are getting printed before "Segmentation Fault" – CocoCrisp Mar 24 '16 at 07:52
  • @MOehm I have done both the modifications to the code you suggested but the output remains the same as mentioned in the first comment. – CocoCrisp Mar 24 '16 at 07:54

1 Answers1

1

The error is not in your bubble sort implementation, but in main: You have defined name as a pointer to a string literal, which means you cannot modify its contents. Use an array of characters instead.

You will also need strlen to determine the length of the string to sort. sizeof will give you the size of the buffer where the string is stored, which will be larger than the length of the null-terminated string.

Finally, consider printing the sorted string with one of the standard printing routines for strings instead of printing it char by char.

Finally, you don't return anything from bubbleSort so make it void.

Here's how your main should look like:

    #include <string.h>

    // bubble sort implementation

    int main(void)
    {
        char name[] = "Stackoverflow";

        bubbleSort(name, strlen(name), compare);

        puts(name);
        return 0;
    }
M Oehm
  • 28,726
  • 3
  • 31
  • 42
  • Your original printing loop is infinite, because your condition is `sizeof(name) / sizeof(char)`, which is always 14, the (implied) size of the array ´name`. `sizeof(char)` is equivalent to 1. Your looping variable, the only thing thatchanges, didn't figure in the condition at all. You probably meant `...; i < sizeof(name); ...`, but that's also wrong, because you'd print the terminating null character. (It would not have gone out of bounds like your infinite loop.) – M Oehm Mar 24 '16 at 09:54
  • You could use `strlen(name)` here, but that's also not optimal, because `strlen` starts looking from the start of the string and it does so for every iteration. You can calculate the (constant) string length at the beginning and the use the precalculated value. Vut you don't need the string length at all: Just stop when you find the null character: `for (i = 0; name[i] != '\0'; i++) ...` or just `for (i = 0; name[i]; i++) ...` – M Oehm Mar 24 '16 at 09:55