Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C: quicksort fails to sort #181

Open
dwrz opened this issue Jan 19, 2020 · 0 comments
Open

C: quicksort fails to sort #181

dwrz opened this issue Jan 19, 2020 · 0 comments

Comments

@dwrz
Copy link

dwrz commented Jan 19, 2020

I was trying to understand why my implementation of quicksort was not working, and figured I'd look at this repo for some insights.

The array input that I am testing is:

int nbr[8] = {3, 1, 2, 0, 8, 9, 11, -1};

If I print the elements after sorting, I see:

nbr failed
-1 1 2 0 3 8 9 11 
nbr_small passed
nbr_null passed

0 is at index 4, instead of index 2.

Following is main with my modifications:

int main(void){
  int nbr[8] = {3, 1, 2, 0, 8, 9, 11, -1};
  int nbr_small[2] = {100, 2};
  int *nbr_null = NULL;

  quicksort(nbr, 8);
  quicksort(nbr_small, 2);
  quicksort(nbr_null, 10);

  if(is_sorted(nbr, 8))
    printf("nbr passed\n");
  else
    printf("nbr failed\n");
  for (int i = 0; i < 8; i++) {
	  printf("%d ", nbr[i]);
  }
  printf("\n");

  if(is_sorted(nbr_small, 2))
    printf("nbr_small passed\n");
  else
    printf("nbr_small failed\n");

  if(nbr_null == NULL)
    printf("nbr_null passed\n");
  else
    printf("nbr_null failed\n");

  return EXIT_SUCCESS;
}

Quick diff:

int main(void){						      |	int main(){
  int nbr[8] = {3, 1, 2, 0, 8, 9, 11, -1};		      |	  int i;
							      >	  int nbr[5] = {10, 1, 0, 5, 2};
  quicksort(nbr, 8);					      |	  quicksort(nbr, 5);
  if(is_sorted(nbr, 8))					      |	  if(is_sorted(nbr, 5))
  for (int i = 0; i < 8; i++) {				      <
	  printf("%d ", nbr[i]);			      <
  }							      <
  printf("\n");						      <
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant