-
Code:
#include <stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
int executer[8] = {7, 5, 2, 0, 1, 6, 4, 3};
int x, y, j;
for(x = 0; x < 8; x++)
for(y = 0; y < 7; y++)
if(executer[y] > executer[y+1])
swap(&executer[y], &executer[y+1]);
for(j = 0; j < 8; j++)
printf("%d\n", executer[j]);
getchar();
return 0;
}
This code works.
But why 7 and not 8?
-
First of all, you are NOT doing a "downto" loop, so not you have "stupid sort", rather than "bubble-sort".
Code:
executer[y] > executer[y+1]
If you run "y" up to 7 [e.g. put 8 in your for-loop] what index will y+1 be in the above conditional?
--
Mats
-
Oh now I got it!
Thanks :)
BTW,
GIve me an example of a downto loop and a upto loop.
BTW 2,
This is a bubble sort, isn't it? why stupid-sort?
-
Have you ever done a loop that goes from 10 down to 0? That is a "downto loop". For example print all numbers from 10 down to 1 would be a downto loop. I'm sure whatever book or website you are using to learn C from, should have some examples of that.
--
Mats
-
oh and then the last parameter is i--;
right?
-
Yes, if you count down, then you need to subtract.
--
Mats