Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can i add amount of exchanges that take place within this quick sort algorithm? Could you please help? Here is the code where exchanges

How can i add amount of exchanges that take place within this quick sort algorithm?

Could you please help? Here is the code where exchanges counter needs to be added.

Then I need to display how many exchanges took place.

import jeliot.io.*;

import java.io.*;

import java.util.*;

public class QuickSort {

public static void main(String[] args) {

int[] arr = {4,15,55,64,97,521,19,43,83,57,21,96,44,16,62,37,04,276,332,112,345,6,21, 5, 1, 2, 3, 3};

quickSort(arr, 0, arr.length-1);

System.out.println(Arrays.toString(arr));

}

public static void quickSort(int[] arr, int start, int end){

int partition = partition(arr, start, end);

if(partition-1>start) {

quickSort(arr, start, partition - 1);

}

if(partition+1

quickSort(arr, partition + 1, end);

}

}

public static int partition(int[] arr, int start, int end){

int pivot = arr[end];

for(int i=start; i

if(arr[i]

int temp= arr[start];

arr[start]=arr[i];

arr[i]=temp;

start++;

}

}

int temp = arr[start];

arr[start] = pivot;

arr[end] = temp;

return start;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions