#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.
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.
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!".