Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Modify the fragment of pseudocode for i = 1 to A.length - 1; for j = A.length

ID: 639097 • Letter: 1

Question

1. Modify the fragment of pseudocode for i = 1 to A.length - 1; for j = A.length down to i + 1; if A[ j ] < A [j - 1]; exchange A[ j ] and A[j - 1]; so that it will implement bubble sorting algorithm with counting swaps.

2. Given the following array of characters:    REALITYSHOW

Show how this array will be sorted with the help of : (a) insertion sort; (b) selection sort; (c) bubble sort with swaps counting; (d) bubble sort without swaps counting.

Show the progress of each algorithm in the table

  

Explanation / Answer

import org.junit.Assert;
import org.junit.Test;

public class SortingTest {

@Test
public void bubbleSortingTest() {
BubbleSorting bubbleSorting = new BubbleSorting(10);
bubbleSorting.fill();
bubbleSorting.sort();
assertSortedArray(bubbleSorting.getArray());
bubbleSorting.print();
}

@Test
public void selectionSertingTest() {
SelectionSorting selectionSorting = new SelectionSorting(10);
selectionSorting.fill();
selectionSorting.sort();
assertSortedArray(selectionSorting.getArray());
selectionSorting.print();
}

@Test
public void insertionSortingTest() {
InsertionSorting insertionSorting = new InsertionSorting(10);
insertionSorting.fill();
insertionSorting.sort();
assertSortedArray(insertionSorting.getArray());
insertionSorting.print();
}



private void assertSortedArray(long[] a) {
for (int i = 0; i < a.length; i++) {
if (i + 1 == a.length) return;
Assert.assertTrue(a[i] <= a[i + 1]);
}
}
}