public class Group3{int sort(int arr[], int low, int high){int p = arr[high];int i = (low-1);for (int j=low; j<high; j++){
if (arr[j] <= p){i++;int temp = arr;arr = arr[j];arr[j] = temp;}}
int temp = arr[i+1];arr[i+1] = arr[high];arr[high] = temp;
return i+1;}
void XYZ(int arr[], int low, int high){
if (low < high){int pi = sort(arr, low, high);
XYZ(arr, low, pi-1);XYZ(arr, pi+1, high);}}
static void print(int arr[]){int n = arr.length;for (int i=0; i<n; ++i)System.out.print(arr+" ");System.out.println();}
public static void main(String args[]){int arr[] = {45, 64, 87, 90, 8, 98, 30, 9 };int n = arr.length;
System.out.println("Before sort");print(arr);Group3 ob = new Group3();ob.XYZ(arr, 0, n-1);
System.out.println("After sort");print(arr);}}a. Add a variable named countSwap of type integer with initialvalue 0.
b. Count the number of swaps the above sorting algorithm takes.Please note the swaps are not the same as the number ofcomparisons.c. Explain briefly when the above algorithm performance becomeinefficient.
public class Group3 { int sort(int arr[], int low, int high) { int p = arr[high]; int i = (low-1); for (int j=low; j
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am