Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I NEED THIS IN JAVA Merge-sort and quicksort, are two advanced sorting algorithms which can obtain an average running time complexity of O(n log n).

image text in transcribed

I NEED THIS IN JAVA

Merge-sort and quicksort, are two advanced sorting algorithms which can obtain an average running time complexity of O(n log n). Usually, the actual sorting time depends on the way the elements arranged themselves (We assume the sorting is in an ascending order in this question). Counting the inversions in an array is a way to measure the order. An inversion is a pair of elements (a_i, a_j) such that i a_j. For example, the array {2, 4, 1, 3, 5} has three inversions (2, 1), (4, 1), (4, 3). For a sorted array, the inversion number is 0. (a) Implement a method with time complexity of O(n^2) to count how many inversions there are for a given array. Then print out the number of inversions for array {47, 24, 83, 78, 36, 17, 96, 55}. (b) Through using the divide and conquer technique, you can improve the time complexity of counting inversions to O(n log n). Implement a method to count inversions by modifying the code of merge-sort. Then print out the number of inversions for array {96, 83, 78, 55, 47, 36, 24, 17}. (c) In Quicksort, the element, chosen as the pivot, can be any element in an array. Pick the first element as the pivot. For array {47, 24, 83, 78, 36, 17, 96, 55}, write the numbers in the array after each partitioning step. Taking the first, partitioning step for example, your answer should be 47 (as the pivot), {36, 24, 17, 47, 78, 83, 96, 55} (as the array after the partitioning step). (d) In the real world, our unsorted data is not necessarily in a random order. The data could be almost sorted (i.e., its inversion number is small). In this situation, using neither the first element nor the last element, as the pivot in Quicksort is a good choice. Because the two parts divided by the pivot element is quite uneven, this will make the Quicksort algorithm close to the worst case (O(n^2) in complexity). To avoid this situation, we can choose the pivot more wisely by picking a random element in the array. Implement a quicksort method by using a random element in the array as a pivot

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

Database Driven Web Sites

Authors: Joline Morrison, Mike Morrison

2nd Edition

? 061906448X, 978-0619064488

More Books

Students also viewed these Databases questions

Question

What is the most important part of any HCM Project Map and why?

Answered: 1 week ago