In: Computer Science
Show the contents of the array after the fourth iteration of selectionSort
Your answer should be 4 lines
The array values horizontally for iteration 1
The array values horizontally for iteration 2
The array values horizontally for iteration 3
The array values horizontally for iteration 4
class SelectionSort {
void sort(int arr[]) {
int n = arr.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n - 1; i++) {
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first
// element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
printArray(arr,i+1);
}
}
// Prints the array
void printArray(int arr[],int itr) {
int n = arr.length;
System.out.print(" iteration "+itr+": ");
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver code to test above
public static void main(String args[]) {
SelectionSort ob = new SelectionSort();
int arr[] = {44,36,46,32,38,52,30,26};
ob.sort(arr);
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME