What array will best fit this array after seven passes of the bubble sort.
5 7 9 1 4 2 6 8 3 0
A. [1,2,0,3,4,5,6,7,8,9]
B. [1,2,4,5,3,0,6,7,8,9]
C. [1,4,2,5,6,3,0,7,8,9]
D. [4,1,6,2,5,7,3,0,8,9]
SOLUTION TO THE ABOVE QUESTION
ANSWER
A. [1,2,0,3,4,5,6,7,8,9]
JUSTIFICATION JAVA PROGRAM FOR SEVEN PASS BUBBLE SORT
package com.company;
// Java program for implementation of Bubble Sort
class BubbleSort
{
void bubbleSort(int arr[])
{
int n = arr.length;
for (int i = 0; i < 7; i++)
for (int j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
{
// swap arr[j+1] and arr[j]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
/* Prints the array */
void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver method to test above
public static void main(String args[])
{
BubbleSort ob = new BubbleSort();
int arr[] = {5, 7, 9, 1, 4, 2, 6, 8, 3, 0};
ob.bubbleSort(arr);
System.out.println("\noutput for seven passes of bublle sort: ");
ob.printArray(arr);
}
}
PROGRAM OUTPUT
Comments
Leave a comment