Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
1.3k views
in Information Technology by (25 points)
edited by

Select the appropriate code that performs bubble sort.

(a)

for(int j=arr.length-1; j>=0; j--)
{
	for(int k=0; k<j; k++)
	{
		if(arr[k] > arr[k+1])
		{
			int temp = arr[k];
			arr[k] = arr[k+1];
			arr[k+1] = temp;
		}
	}
}

(b)

for(int j=arr.length-1; j>=0; j--)
{
	for(int k=0; k<j; k++)
	{
		if(arr[k] < arr[k+1])
		{
			int temp = arr[k];
			arr[k] = arr[k+1];
			arr[k+1] = temp;
		}
	}
}

(c)

for(int j=arr.length; j>=0; j--)
{
	for(int k=0; k<j; k++)
	{
		if(arr[k] > arr[k+1])
		{
			int temp = arr[k];
			arr[k] = arr[k+1];
			arr[k+1] = temp;
		}
	}
}

(d)

for(int j=arr.length; j>=0; j--)
{
	for(int k=0; k<j; k++)
	{
		if(arr[k] > arr[k+2])
		{
			int temp = arr[k];
			arr[k] = arr[k+1];
			arr[k+1] = temp;
		}
	}
}

Please log in or register to answer this question.

2 Answers

0 votes
by (52.8k points)

(a)

The outer loop keeps count of number of iterations, and the inner loop checks to see if swapping is necessary.

0 votes
by (24.8k points)
for(int j=arr.length-1; j>=0; j--)
{
 for(int k=0; k<j; k++)
 {
  if(arr[k] > arr[k+1])
  {
   int temp = arr[k];
   arr[k] = arr[k+1];
   arr[k+1] = temp;
  }
 }
}

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...