I want to write a program for sorting ‘n’ integers. ‘n’ will be entered by the user.
Here’s my code :
#include stdio.h
int main (void)
{
int x, y, z;
int a[100];
printf ("How many integers do you want to sort?\n");
scanf ("%d", &x);
for (y = 0; y < x; ++y)
{
printf ("Enter an integer :");
scanf ("%d", &a[y]);
}
while (y <= x)
{
if (a[y] < a[y - 1])
{
z = a[y - 1];
a[y - 1] = a[y];
a[y] = z;
++y;
}
else
++y;
}
printf ("Sorted integers :\n");
for (y = 0; y < x; ++y)
{
printf ("%d\n", a[y]);
}
return 0;
}
It’s not giving the correct output.
After you have taken all the integers as input into the array, the value of y equals x.
So, you need to re-initialize the value of y to 1 before entering the while loop to sort the integers.
2 Likes
Added y = 1;
before the while loop. Still gives the same output.
Let’s try to describe the algorithm with your own words. What idea you want to implement (not sorting, but deeper idea)? And we will give you some hints where you are not doing what you think you are doing…
Skip not important parts, reading input and writing output is clear.
Describe this part:
while (y <= x)
{
if (a[y] < a[y - 1])
{
z = a[y - 1];
a[y - 1] = a[y];
a[y] = z;
++y;
}
else
++y;
}
re-think what you are trying to do in the while loop…
The statements under the while loop are executed as long as the condition y <= x
is satisfied. So if I enter x
as 5
and initialize y
as 1
, the while
condition is satisfied and it will enter the loop. The if
condition will be evaluated and if it is true, it will swap the values of a[0]
and a[1]
and then increment y
. If not, it will simply increment y
and again test the while
condition.
We know what the code is doing and why it is not working, but try to describe the idea how you want to sort the array. What that loo will do?
Is that while loop same as this for loop (for loop can be better for understanding)?
for ( int i = 1; i < x; ++i ) {
if (a[i] < a[i - 1])
{
z = a[i - 1];
a[i - 1] = a[i];
a[i] = z;
}
}
What it really does?
I think the for
loop you have written above does the same thing as my while
loop. I want to sort the numbers is ascending order.
I think the error is because my code compares only two successive array elements and not all the elements.
If I enter x
as 5
and the 5 integers as 34,2,46,21 and 13, then my code will compare 34 and 2 and since a1
that is 2
is less than a0
(34), it will swap the values and a1
will be 34 and a0
= 2.But after this, only a1
and a2
will be compared. So even if a2
is smaller than a0
, it will not be able to change it’s value.
I have to make a change so that the values of all the elements in the array are compared and not just the successive elements.
bro your code compares the last element with any garbage number so rethink what you want to do and what you are doing
if u want the right code n any help
then email me at [email protected]
you didn’t want the right code otherwise i would have posted it here only with the use of your very while loop n not any known method like bubble sorting or any…!!!
if u find the solution plz do mail it to me i want to see the code
Bravo! @gautam94 You just did successfully, the very things codechef discuss was intended for – To make one learn (possibly from our own mistakes).
Happy Programming!
1 Like
@gautam94 your observation is correct, simply said, loop moves the biggest number to the array end, I believe you know what to do to sort whole array
1 Like