Originally Posted by
aghast
There is no temp[i]. You only ever need one temporary variable, to hold the value that you are swapping right now. So just store and load to temp.
And why would you print temp[i]? You're not sorting that array, you're sorting the array array. Print that!
I tried but it doesn't give correct output
Code:
#include<stdio.h>
int main()
{
int array[5] = {5, 4, 3, 2, 1 };
int temp;
for ( int i = 0; i < 5; i++)
{
for ( int j = i+1; j < 5; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[j] = array[i];
array[j] = temp;
}
}
}
for ( int i = 0; i < 5; i++)
{
printf("%d", array[i]);
}
return 0;
}
55555