Do not use any Array\'s for this program, everything should be coded and you sho
ID: 3740927 • Letter: D
Question
Do not use any Array's for this program, everything should be coded and you should be able to enter 10 integers.
Modify the bubble sort program in our lecture so that ten integers between 0 and 99 inclusively will be stored in an array and displayed on the screen and then the same set of integers will be sorted in decreasing order by bubble sort program and redisplayed on the screen sorted in decreasing order. Below is a sample run of your program.
10 numbers are generated and they are
72 13 31 20 28 59 87 21 51 32
10 numbers are sorted and they are
87 72 59 51 32 31 28 21 20 13
Explanation / Answer
BubbleSortArray.java
import java.util.Random;
public class BubbleSortArray {
public static void main(String[] args) {
int array[] = new int[10];
Random r = new Random();
for(int i=0;i<array.length;i++){
array[i]=r.nextInt(100);
}
System.out.println("10 numbers are generated and they are");
for(int i=0;i<array.length;i++){
System.out.print(array[i]+" ");
}
System.out.println();
bubbleSort(array);
System.out.println("10 numbers are sorted and they are");
for(int i=0;i<array.length;i++){
System.out.print(array[i]+" ");
}
System.out.println();
}
public static void bubbleSort(int[] array) {
int n = array.length;
int temp;
for(int i=0; i < n; i++){
int j=1;
while(j < (n-i)){
if(array[j-1] < array[j]){
//swap the elements!
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
}
j++;
}
}
}
}
Output:
10 numbers are generated and they are
7 47 14 10 24 60 74 43 85 90
10 numbers are sorted and they are
90 85 74 60 47 43 24 14 10 7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.