I have written this code for Bubble Sort using Pointers, I'm not getting output. Here is my code. Please fix this code. I am getting error basically in swapping syntax. Please help.
int sort_array(int n, int *a[]){
for (int i = 1; i <= n - 1; i ) {
for (int j = 0; j <= n - i - 1; j ) {
if (*(a[i]) < *(a[j 1])) {
int temp = *(a[j]);
*(a[j]) = *(a[j 1]);
*(a[j 1]) = temp;
}
}
}
}
int main() {
int n;
scanf("%d", &n);
long int a[n];
for (int i = 0; i <= n; i ) {
scanf("%d", &a[i]);
}
sort_array(n, &a[n]);
for (int i = 0; i <= n; i ) {
printf("%d", a[i]);
}
}
CodePudding user response:
The code is full of bugs.
You declared a variable length array
long int a[n];
with n
elements of the type long int
.
Thus in this loop
for(int i=0;i<=n;i ){
scanf("%d",&a[i]);
}
the index i
equal to n
will be a reason of accessing memory outside the defined array.
Also the conversion specification %d
is used for objects of the type int
instead of long int
.
So the loop must look like
for(int i=0;i < n;i ){
scanf("%ld",&a[i]);
}
Similar problems exist in this for loop
for(int i=0;i<=n;i ){
printf("%d",a[i]);
}
that must be rewritten like
for(int i=0;i < n;i ){
printf("%ld ",a[i]);
}
In this call
sort_array(n,&a[n]);
the second argument expression has the type long int *
and points to a non-existent element of the array with the index equal to n
. That is the pointer expression points outside the array.
On the other hand, the corresponding function parameter
int sort_array(int n,int *a[]){
in fact has the type int **
. The compiler shall issue an error message that the argument expression can not be converted to the type of the parameter.
Also the function has the return type int
but returns nothing.
The function should be declared like
void sort_array( long int a[], size_t n );
Due to this for loop
for(int i=1;i<=n-1;i ){
the function will not even try to sort an array containing two elements because this if statement
if(*(a[i])<*(a[j 1])){.
will look like
if(*(a[1])<*(a[1])){
In any case the if statement
if(*(a[i])<*(a[j 1])){
int temp=*(a[j]);
*(a[j])=*(a[j 1]);
*(a[j 1])=temp;
}
does not make a sense. There are compared elements with indices i
and j 1
but there are swapped elements with indices j
and j 1
.
Thus the code shall be entirely rewritten. You shall do that yourself.
CodePudding user response:
In addition to all valid remarks by Vlad from Moscow:
- you should always test the return value of
scanf()
to detect missing or invalid input, temp
must be defined with typelong int
,- the test in the nested loop should be
if (a[j] > a[j 1])
Here is a modified version:
#include <stdio.h>
void sort_array(long int a[], size_t n) {
for (size_t i = 0; i < n; i ) {
for (size_t j = 0; j < n - i; j ) {
if (a[j] > a[j 1]) {
long int temp = a[j];
a[j] = a[j 1];
a[j 1] = temp;
}
}
}
}
int main() {
int n;
if (scanf("%d", &n) != 1 || n <= 0) {
fprintf(stderr, "invalid input\n");
return 1;
}
long int a[n];
for (int i = 0; i < n; i ) {
if (scanf("%ld", &a[i]) != 1) {
fprintf(stderr, "invalid input\n");
return 1;
}
}
sort_array(a, n);
for (int i = 0; i < n; i ) {
printf("%ld ", a[i]);
}
printf("\n");
return 0;
}
CodePudding user response:
Why should we use fprintf......