Computer Applications
Given the following array :
Write a program to sort the above array using exchange selection sort. Give the array status after every iteration.
Java
Java Arrays
7 Likes
Answer
public class KboatSelectionSort
{
public static void main(String args[]) {
int X[] = {13, 7, 6, 21, 35, 2, 28, 64, 45, 3, 5, 1};
int n = X.length;
for (int i = 0; i < n - 1; i++) {
int idx = i;
for (int j = i + 1; j < n; j++) {
if (X[j] < X[idx])
idx = j;
}
int t = X[i];
X[i] = X[idx];
X[idx] = 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 a given ITEM in a given array X[n] using linear search technique. If the ITEM is found, move it at the top of the array. If the ITEM is not found, insert it at the end of the array.
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.
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.
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