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

Like bubble sort, swap sort compares pairs of elements and swaps them when they

ID: 3629652 • Letter: L

Question

Like bubble sort,
swap sort compares pairs of elements and swaps them when they are out of
order, but swap sort looks at different pairs of elements than bubble sort does.
On pass i of swap sort, the element in position i of the array is compared with the
elements in all positions to the right of position i, and pairs of elements are
swapped as needed. At the end of pass i, the element in position i is where it
belongs in the sorted array.
For example, let's say that we have the following initial array:
15 8 20 5 12
On pass 0, swap sort compares the element in position 0 with the elements in
positions 1 through n – 1, where n is the number of elements in the array. First,
15 is compared to 8, and because 8 is less than 15, the two elements are swapped:
8 18 15 20 5 12
Next, 8 (which is now in position 0) is compared to 20, and no swap occurs
because 8 is already smaller than 20. Then 8 is compared with 5, and the two
elements are swapped:
5 15 20 8 12
Finally, 5 is compared to 12, and no swap occurs because 5 is already smaller
than 12. At this point, pass 0 is complete, and the element in position 0 (the 5) is
where it belongs in the sorted array.
On pass 1, swap sort compares the element in position 1 with the elements in
positions 2 through n – 1 and performs swaps when needed. The process
continues for passes 2 through n – 2, at which point the array is fully sorted.




swapSort() that
implements the swap sort algorithm described in written above. Its only
parameter should be a reference to an array of integers. Like the other
methods in this file, your swapSort method must make use of the compare(),
move(), and swap() helper methods so that you can keep track of the total
number of comparisons and moves that it performs. If you need to compare
two array elements, you should make the method call compare(comparison);
for example, compare(arr[0] < arr[1]). This method will return the
result of the comparison (true or false), and it will increment the count of
comparisons that the class maintains. If you need to move element j of arr
to position i, instead of writing arr[i] = arr

Explanation / Answer

public class SwapSort { //static variables to count number of //comparisions, moves and swaps //performed by swapSort algorithm public static int comparisions = 0; public static int moves = 0; public static int swaps = 0; public static void main(String []args){ //array to be sorted int arr[ ]={ 45, 56, 87, 15, 97, 65, 48, 35, 50, 85 }; //displaying array value before sorting System.out.println(" Array before sorting: "); for ( int element : arr ) System.out.print( element + " "); //calling swapSort method to sort the array elements swapSort ( arr ) ; //displaying array elements after sorting System.out.println(" After sorting using swapsort: "); for ( int element : arr ) System.out.print( element + " "); //displaying values of comparisions, swaps and moves System.out.println( " Number of comparisions: " + comparisions); System.out.println( "Number of swaps: " + swaps); System.out.println( "Number of moves: " + moves); } //sorts the array elements using swapsort algorithm public static void swapSort ( int arr[ ] ) { for(int i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote