Try to write Bubble Sort Program

Hi,

I am confused about some code I found online(https://www.techgeekbuzz.com/blog/recursive-bubble-sort-in-c/). It works, but there is a function I need to add to make it work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include <stdio.h>
#include <stdlib.h>
  /* To sort the given numbers in ascending order */
  void bubbleSort(int data, int n) {
        int i, temp;
        if (n > 0) {
                for (i = 1; i < n; i++) {
                        if (data[i - 1] > data[i]) {
                                temp = data[i];
                                data[i] = data[i - 1];
                                data[i - 1] = temp;
                        }
                }
                bubbleSort(data, n - 1);
        }
        return;
  }
  int main() {
  int i, n, data;
        /* Enter the numbers of inputs which is to be sorted */
        printf("Enter the number of inputs:");
        scanf("%d", &n);
        /* To store input values, it allocates dynamic memory */
        data = (int *) malloc(sizeof(int) * n);
        /* Enter the input data */
        for (i = 0; i < n; i++) {
                printf("data[%d]: ", i);
                scanf("%d", &data[i]);
        }
        /* sorts the given numbers */
        bubbleSort(data, n);
        /* print the sorted numbers */
        printf("Sorted array:\n");
        for (i = 0; i < n; i++) {
                printf("%d ", data[i]);
        }
        printf("\n");
        return 0;
  }


When I try to run the code, it shows an error. Please help me to run this code.
You are missing a [] after data in line 5 and a * preceding data on line 20. That’s it.

Basically, you have omitted what is necessary to make data an array.
It's hard to imagine how you managed to make a perfect copy/paste of code posted on a website, yet managed to just mess it up in one specific way.

$ diff -b foo.c bar.c
4c4
<   void bubbleSort(int *data, int n) {
---
>   void bubbleSort(int data, int n) {
19c19
<   int i, n, *data;
---
>   int i, n, data;

It's like a deliberate attempt to make people go look at the website.

But then again, you're fond of posting links to that same site, so my conclusion is that you're just a click baiter.

Oh, and the code is a bad example to begin with, so there - a review for you.
Last edited on
salem c wrote:
It's like a deliberate attempt to make people go look at the website.

Chuckles here mucked up the URL so it 404s. I don't need to click on the link to know it is FUBAR.

Someone else I might Lucy 'splain how to fix the link, but not for this yutz.

Hint, hint, the website is based in India, the OP is just spamming some Indian "train for a tech job" site.
why would you click on a link where the poster found badly written code that does not work? "Oh, look, a bad sorting algorithm that doesn't work, maybe they have more of this kind of thing, and I HAVE TO HAVE IT!".
If you actually do look at the link the code the spammer vomited up isn't the same as what's at the link. Lots of "copy'n'paste" oops above.

Of course Chuckles wants clicks for the spammed site, not help in fixing code deliberately broken.

FYI, there are two similar code listings, the second uses dynamic memory allocation, the spammer mangled the second one.
Topic archived. No new replies allowed.