1

The function is suppose to sort an array of random integers by ascending order. I found a method for solving this problem, the bubble sort, swaping a by b if b < a. However, my implementation, or the lack of it, keeps returning a segmentation fault: 11. Could it have something to do with the parameter "int *tab" or subscripts I'm using during the swaping of elements?

void  ft_sort_integer_table(int *tab, int size)
{
  int i;
  int j;
  int t;

  i = 1;
  j = 0;
  t = 0;
  while (tab[j] != '\0')
  {
    if (tab[i] < tab[j])
    {
      t = tab[i];
      tab[i] = tab[j];
      tab[j] = t;
    }
    i++;
    j++;
  }
}
2
  • the posted code will fail on most unsorted arrays. Suggest using a valid bubble sort algorithm Commented Feb 12, 2019 at 5:28
  • please post a minimal reproducible example so we can reproduce the problem and then can help you debug it Commented Feb 12, 2019 at 5:31

5 Answers 5

3

Unless you are guaranteed beforehand that the value 0 terminates your buffer and doesn’t appear elsewhere in the array (like you are with null terminated strings) you can’t test for tab[i] being zero to determine that you have reached the end of the array. Your function takes size as a parameter too; why not use that?

EDIT: Also, no sorting algorithm runs in O(n). Bubble sort, which looks like what you’re trying to implement, requires two nested loops.

Sign up to request clarification or add additional context in comments.

1 Comment

"no sorting algorithm runs in O(n)" --> How about O(1) via Quantum Bogo Sort ;-)
2

Skipping the correctness of this implementation of the sorting algorithm (as it seems wrong) the segmentation is caused by the null termination check that you are doing. The NULL('\0') character is specified for strings, or char array types in C programming language, and it is used to signal their termination. It doesn't work with int type arrays. You should be using the size argument for iterating the array.

Comments

1

You do not use the size parameter. Instead you are trying find the null-terminator which int array is not supposed to have (unlike C-string). So in case you have to compare j with size and keep swapping till the array is fully sorted.

Also, it is better of using size_t size instead of int size in order to stay pedantic.

Comments

1

You pass an array and a size to your sorting function but do not use the size anywhere so potentially i and j could go out of bounds causing undefined behavior.

An int array can contain 0s so you need to have other criteria for when your sorting is finished. E.g. when you go through all the elements in the array [0..size] and do not do a swap - then it is sorted.

Comments

0

Firstly, your while loop logic is wrong. The character '\0' refers to the null character at the end of string. This doesn't make sense if you compare it with int type. Secondly, the logic you implemented is comparing side by side elements of an array and not a single element with all others and placing it. I would recommend you study bubble sort. Geekforgeeks is the best source for cse guys. Hope it solves. Cheers !! Feel free to ask questions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.