
how to arrange element from lowest to highest or highest to lowest. bubble sort
small to big or low to high
#include<stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main() {
int i, temp, swapped; (i is counting variable, temp to store a number, swapped is going to either 0 or one)
int howMany = 10;
int goals[howMany];
for(i=0; i<howMany; i++){ (its gonna loop 10 times and generate random number)
goals[i] = (rand()%25 )+1; (0 to 25)
}
printf("Original List\n");
for(i=0; i<howMany; i++){
printf("%d \n", goals[i]);
}
while(1){
swapped = 0; (check sorted or not it will only be zero when it is sorted or do it again)
for(i=0; i<howMany-1; i++){
if(goals[i]>goals[i+1]){ (swap code)
int temp = goals[i];
goals[i] = goals[i+1];
goals[i+1] = temp;
swapped = 1; (responsible for swapping one with another number)
}
} (not in order so it has to do it again)
if(swapped==0){
break;
}
}
printf("\nSorted List\n");
for(i=0; i<howMany; i++) {
printf("%d \n", goals[i]);
}
}
Comments
Post a Comment