Computer Applications
For the same array mentioned above in previous question, write a program to sort the above array using bubble sort technique. Give the array-status after every iteration.
Java
Java Arrays
1 Like
Answer
public class KboatBubbleSort
{
public static void main(String args[]) {
int X[] = {13, 7, 6, 21, 35, 2, 28, 64, 45, 3, 5, 1};
int n = X.length;
//Bubble Sort
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (X[j] > X[j + 1]) {
int t = X[j];
X[j] = X[j+1];
X[j+1] = t;
}
}
System.out.println("Pass :" + (i + 1));
for(int k = 0; k < n; k++) {
System.out.print(X[k] + " ");
}
System.out.println();
}
System.out.println("Sorted Array:");
for (int i = 0; i < n; i++) {
System.out.print(X[i] + " ");
}
}
}Variable Description Table
Program Explanation
Output

Answered By
1 Like
Related Questions
Write a program to search for 66 and 71 in the following array :
Make use of binary search technique. Also give the intermediate results while executing this algorithm.
Given the following array :
Write a program to sort the above array using exchange selection sort. Give the array status after every iteration.
Write a program to input integer elements into an array of size 20 and perform the following operations:
- Display largest number from the array
- Display smallest number from the array
- Display sum of all the elements of the array
Write a program to input forty words in an array. Arrange these words in descending order of alphabets, using selection sort technique. Print the sorted array.