Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include <math.h>
- int main()
- {
- int i, temp, swapped;
- int howMany = 10;
- int goals[howMany];
- //create an array of random numbers
- for(i=0; i<howMany; i++){
- goals[i] = ( rand()%24 )+1;
- }
- printf("Original List\n");
- for(i=0; i<howMany; i++){
- printf("%d \n", goals[i]);
- }
- while(1){
- swapped = 0;
- //comparing each number to number after it
- //last number won’t have a number after, so howMany -1
- for(i=0; i<howMany-1; i++){
- //if first number bigger, swap them
- if(goals[i]>goals[i+1]){
- int temp = goals[i]; //we are going to replace it
- goals[i] = goals[i+1]; //replace first with second
- goals[i+1] = temp; //replace second with first
- swapped = 1;
- }
- }
- if(swapped==0){
- break;
- }
- }
- //print out the sorted list
- printf("\nSorted List\n");
- for(i=0; i<howMany; i++){
- printf("%d \n", goals[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement