posted 14 years ago
Here is an example with a nested for loop Bubble Sort. This time with an array of 5 open elements is initialized, then filled with keyboard input. Note however, that the array elements are not the same as the input, "see Original array is: below". The Bubble Sort routine does sort it the right way, see "Sorted array is:" below. This is not the array I originally built.
This code also works when an array is initialized with five values, this is commented out, //int nums[] = {2, 3, 7, 8, 1}; but you have to comment out the "//building array" section in the code also.
Please enter your char: 5
Please enter your char: 4
Please enter your char: 3
Please enter your char: 2
Please enter your char: 1
Original array is: 53 52 51 50 49
a is:1 b is:4
nums[b-1]50 nums[b]49
a is:1 b is:3
nums[b-1]51 nums[b]49
a is:1 b is:2
nums[b-1]52 nums[b]49
a is:1 b is:1
nums[b-1]53 nums[b]49
a is:2 b is:4
nums[b-1]51 nums[b]50
a is:2 b is:3
nums[b-1]52 nums[b]50
a is:2 b is:2
nums[b-1]53 nums[b]50
a is:3 b is:4
nums[b-1]52 nums[b]51
a is:3 b is:3
nums[b-1]53 nums[b]51
a is:4 b is:4
nums[b-1]53 nums[b]52
Sorted array is: 49 50 51 52 53