Answered step by step
Verified Expert Solution
Question
1 Approved Answer
(JAVA) Does anybody could help me? Problem 1 Write down the bugs in the following code. 01 public void 3Sorts(E[] data, Comparator c) { 02
(JAVA) Does anybody could help me?
Problem 1
- Write down the bugs in the following code.
01 public void 3Sorts(E[] data, Comparatorc) { 02 for (int i = 0; i <= data.length; i++) { 03 for (int j = i; j < data.length; j++) 04 int temp = data[j]; 05 if (data[j] > data[j + 1]) { 06 swap(data[j], data[j + 1]; 07 } 08 } 09 } 10 } 11 12 13 private E getMinimum(E[] data, Comparator c) 14 int min = data[0]; 15 for (int i = 0; i < data.length; i += 2) { 16 if (c.compare(min, data[i]) < 0) { 17 min = data[i]; 18 } 19 } 20 return min; 21 }
Problem 2
Fix the bubble sort so that it has BigO(n) when it runs on a sorted array.
public void bubbleSort(E[] data, Comparatorc) { for (int i = 0; i < data.length; i++) { for (int j = 0; j < data.length - i; j++) { if (c.compare(data[j], data[j + 1]) > 0) { swap(data[j], data[j + 1]); } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started