-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tony Soul
authored and
Tony Soul
committed
Sep 1, 2016
1 parent
785186e
commit ae20056
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
/* Program that sorts a list of 10 numbers */ | ||
int main() { | ||
int ctr, inner, outer, didSwap, temp; | ||
int nums[10]; /* will hold the 10 numbers */ | ||
|
||
/* Fills array with random numbers form 1 to 100 */ | ||
for (ctr = 0; ctr < 10; ctr++) { | ||
nums[ctr] = (rand() % 99) + 1; | ||
} | ||
|
||
/* Prints the list before it is sorted */ | ||
puts("\nHere is the list before the sort:"); | ||
for (ctr = 0; ctr < 10; ctr++) { | ||
printf("%d\n", nums[ctr]); | ||
} | ||
|
||
/* Sorts the array */ | ||
for (outer = 0; outer < 9; outer++) { | ||
didSwap = 0; /* Become 1 (true) if list is not yet order */ | ||
for (inner = outer; inner < 10; inner++) { | ||
if (nums[inner] < nums[outer]) { | ||
temp = nums[inner]; | ||
nums[inner] = nums[outer]; | ||
nums[outer] = temp; | ||
didSwap = 1; /* true because a swap took place */ | ||
} | ||
} | ||
if (didSwap == 0) | ||
break; | ||
} | ||
|
||
/* Prints the list after it is sorted */ | ||
printf("\nHere is the list after the sort: "); | ||
for (ctr = 0; ctr < 10; ctr++) | ||
printf("%d\n", nums[ctr]); | ||
return 0; | ||
} |