/ 2. ***** student writes this method /** Sorts arr in ascending order using the
ID: 3841542 • Letter: #
Question
/ 2. ***** student writes this method /** Sorts arr in ascending order using the bubble sort algorithm */ public void bubbleSort() { // Note: To animate the algorithm, put this method call as the // last statement in your innermost for loop // animate(i, j); // where i is the value of the outer loop counter // and j is the value of the inner loop counter, // or the index of the current array element // Part 2 student code starts here: // Part 2 student code ends here. } // end of bubbleSort i need someone to make a bubble sort algorithm for this part of my project. Also can you provide the raw code
Explanation / Answer
Bubble sort : this sorting algorithm sorts by moving the largest element of the array to its correct position in first iteration. In second iteration, it places the next largest element in the rest n-1 elements of the array in its correct position.
Code :
public void bubbleSort(int[] arr){
int len = arr.length;
int temp;
for(int i=0; i<len; i++)
{
for(int j=0; j<len-i-1; j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.