user1704677
user1704677

Reputation: 111

Bubblesort with C

I have a really simple (or at least it should be) assignment in which I have to run bubblesort on a large array of random numbers and see its performance time. I then have to do the same thing except split up the array in half and sort one half in one thread and another in the other thread and see if it is any faster that way.

I've never worked with C before so I am completely clueless when it comes to pointers, having only worked with Java. Here is my code so far as I am just trying to get the bubblesort to work.

#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <libgen.h>

int main() {
  int *array[50000];
  for(int i = 0; i < 50000; i++) {
    array[i] = 1;
  }
  bubbleSort(array, 50000);
}

void bubbleSort(int *numbers[], int *array_size) {
  int i, j, temp;
  for(i = (array_size - 1); i > 0; i--) {
    for(j = 1; j <= i; j++) {
  if(numbers[j-1] > numbers[j]) {
    temp = numbers[j-1];
    numbers[j-1] = numbers[j];
    numbers[j] = temp;
      }
    }
  }
  for(int i = 0; i < 10; i++) {
    printf(numbers[i]);
  }
}

All I'm trying to do here is sort the array and then print out the first ten numbers just so I know it's working. I'm getting all kinds of pointer errors.

"bubbleSort.c", line 11: warning: improper pointer/integer combination: op "="
"bubbleSort.c", line 13: warning: implicit function declaration: bubbleSort
"bubbleSort.c", line 16: identifier redeclared: bubbleSort
        current : function(pointer to pointer to int, pointer to int) returning void
        previous: function() returning int : "bubbleSort.c", line 13
"bubbleSort.c", line 18: warning: improper pointer/integer combination: op "="
"bubbleSort.c", line 21: warning: improper pointer/integer combination: op "="
"bubbleSort.c", line 23: warning: improper pointer/integer combination: op "="
"bubbleSort.c", line 28: warning: argument #1 is incompatible with prototype:
        prototype: pointer to const char : "/usr/include/iso/stdio_iso.h", line 206
        argument : pointer to int
cc: acomp failed for bubbleSort.c

Upvotes: 0

Views: 951

Answers (2)

Eregrith
Eregrith

Reputation: 4366

line 11: You should not have declared int *array[] but int array[] instead
line 13: Prototype your function or declare it above your main
line 16: You declared int *array_size but in the main you give it an int
line 18, 21 and 23: Same as 11.
line 28: Never use printf with a variable format string ! printf("%i, ", numbers[i]); is the way.

You should really review C coding basics

Upvotes: 1

unwind
unwind

Reputation: 399803

This:

int *array[50000];

declares a 50,000-element array of pointers to int, which is probably not what you want. Remove the *.

In the bubbleSort() prototype you're also having spurious asterisks that should be removed.

Please note that the asterisk means something in C, you're not supposed to just randomly decorate your code with them wherever you feel like. If you're unsure what it means and when, you should have access to some tutorial information if this is for a class. Start reading.

Upvotes: 4

Related Questions