Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can anybody tell me how to do count number of inversions with quick sort? import java.util.ArrayList; import java.util.Collections; public class Quicksort { int inversioncount =

Can anybody tell me how to do count number of inversions with quick sort?

import java.util.ArrayList;

import java.util.Collections;

public class Quicksort {

int inversioncount = 0;

public int partition(ArrayList arr, int low, int high)

{

int pivot = arr.get(high);

int i = (low-1); // index of smaller element

for (int j=low; j

{

// If current element is smaller than or

// equal to pivot

if (arr.get(j) <= pivot)

{

i++;

//swap elements

Collections.swap(arr, i, j);

}

}

//swap elements

Collections.swap(arr, i+1, high);

return i+1;

}

//ArrayList arr --> Array to be sorted,

//low --> Starting index,

//high --> Ending index

public int sort(ArrayList arr, int low, int high)

{

if (low < high)

{

/* pi is partitioning index, arr[pi] is

now at right place */

int pi = partition(arr, low, high);

// Recursively sort elements before

// partition and after partition

sort(arr, low, pi-1);

sort(arr, pi+1, high);

}

return inversioncount;

}

}

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

Recommended Textbook for

Data Mining Concepts And Techniques

Authors: Jiawei Han, Micheline Kamber, Jian Pei

3rd Edition

0123814790, 9780123814791

More Books

Students also viewed these Databases questions

Question

Question Can any type of stock or securities be used in an ESOP?

Answered: 1 week ago

Question

Question Can a self-employed person adopt a money purchase plan?

Answered: 1 week ago