Question

In: Computer Science

How is this method coded in Java? int[] sortQuicksort(int[][] view) sortQuicksort performs a quicksort sort on...

How is this method coded in Java?

int[] sortQuicksort(int[][] view)

sortQuicksort performs a quicksort sort on a view (a referential view of the master set of posts). sortQuicksort returns an array of profile information as detailed in the Data Structures section above. You may implement additional functions to support this sorting operation; however, take care that the profile information is accurately maintained and accounted for over any subsequent function calls.

/// Sorts (shallow) a set of references to posts in descending order

  /// subject to the differential between ups and downs using a quicksort.

  /// @param view A shallow copy of a set of posts

  /// @return an array of profile information of 3 buckets with the

  ///     respective buckets containing a count of 0: allocations,

  ///     1:comparisons, and 2: swaps

  public static int[] sortQuicksort(int[][] view) {

    // profile[0:allocs, 1:comparisons, 2:swaps]

    int[] profile = new int[3];

    // TODO : Implement Here

    return profile;

  }

Posts Data:

0001 505361 236677

0002 629710 727941

0003 414780 842861

0004 702957 401854

0005 862865 315572

0006 930013 609421

0007 326101 220500

0008 732457 404837

0009 878259 411163

0010 227645 638470

Solutions

Expert Solution

Answer:


public class BubbleSort {
        public static void main(String[] args) {
                int arr[][] = { { 11, 505361, 236677 }, { 2, 629710, 727941 }, { 3, 414780, 842861 }, { 4, 702957, 401854 },
                                { 5, 862865, 315572 }, { 6, 930013, 609421 }, { 7, 326101, 220500 }, { 8, 732457, 404837 },
                                { 9, 878259, 411163 }, { 10, 227645, 638470 } };
                int[] profile=sortBubble(arr);
                System.out.println(profile[0]+":allocations");
                System.out.println(profile[1]+":comparisons");
                System.out.println(profile[2]+":swaps");
                for (int[] is : arr) {
                        for (int i : is) {
                                System.out.print(i+" ");
                        }
                        System.out.println();
                }
        }

        public static int[] sortBubble(int[][] view) {
                int profile[] = new int[3];
                int n = view.length;
                int temp = 0;
                for (int i = 0; i < n; i++) {
                        for (int j = 1; j < (n - i); j++) {
                                if (view[j - 1][0] > view[j][0] ||  (view[j - 1][0] == view[j][0] && view[j - 1][1] > view[j][1]) || (view[j - 1][0] == view[j][0] && view[j - 1][1] == view[j][1] && view[j - 1][2] > view[j][2]) ) {
                                        // swap elements
                                        temp = view[j - 1][0];
                                        view[j - 1][0] = view[j][0];
                                        view[j][0] = temp;
                                        temp = view[j - 1][1];
                                        view[j - 1][1] = view[j][1];
                                        view[j][1] = temp;
                                        temp = view[j - 1][2];
                                        view[j - 1][2] = view[j][2];
                                        view[j][2] = temp;
                                        profile[2]+=3;
                                        if(view[j - 1][0] == view[j][0]){
                                                profile[1]+=3;
                                        }else if(view[j - 1][0] == view[j][0] && view[j - 1][1] == view[j][1]){
                                                profile[1]+=6;
                                        }else{
                                                profile[1]+=1;
                                        }
                                        profile[0]+=9;
                                }
                        }
                }
                return profile;
        }
}


Related Solutions

How is this method coded in Java? int[] sortBubble(int[][] view) sortBubble performs a bubble sort on...
How is this method coded in Java? int[] sortBubble(int[][] view) sortBubble performs a bubble sort on a view (a referential view of the master set of posts). sortBubble returns an array of profile information as detailed in the Data Structures section above. You may implement additional functions to support this sorting operation; however, take care that the profile information is accurately maintained and accounted for over any subsequent function calls. /// Sorts (shallow) a set of references to posts in...
How is this method coded in java? int differential(int[] post) differential computes the virtual differential for...
How is this method coded in java? int differential(int[] post) differential computes the virtual differential for a single post record and returns the differential. The differential is simply the difference between ups and downs. differential is virtual because it is not stored in a post record. /// Compute the differential between "ups" (at index 1) and "downs"   /// (at index 2). The differential is not maintained in the data but is a   /// virtual field derived by the calculation performed...
How is this method coded in Java?
How is this method coded in Java?
Write a program in Java which performs the sort operation. The main method accepts ten numbers...
Write a program in Java which performs the sort operation. The main method accepts ten numbers in an array and passes that to the method sort. The method sort accepts and sorts the numbers in ascending and descending order. The method display shows the result. You can use integers or floating point numbers.
from partition import partition def quicksort(a: list, l: int, u: int) -> None: '''Sort the given...
from partition import partition def quicksort(a: list, l: int, u: int) -> None: '''Sort the given list a in non-descending order. Precondition: 0 <= l and u < len(a)''' if l < u: mid = (l + u) // 2 three = [a[l], a[mid], a[u]] three.sort() if three[1] == a[l]: pivot_loc = l elif three[1] == a[u]: pivot_loc = u else: pivot_loc = mid a[u], a[pivot_loc] = a[pivot_loc], a[u] pivot = a[u] i = partition(a, l, u - 1, pivot)...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are that it returns an array of all the elements of the int[] array numbers which are coprime with x } Note that the array that is returned may be an empty array--you will have to count how many times gcf(x, numbers[i]) == 1. ASSUME numbers is not null and not empty.
Can someone implement the following in Java? Quicksort with switching to Insertion sort when the number...
Can someone implement the following in Java? Quicksort with switching to Insertion sort when the number of elements in the subarray is less than or equal to 2% of the original number Requirements: 1) functions from standard libraries implementing Quicksort are NOT allowed;
give a good explanation of Bubble sort, Insertion sort, Selection sort, and Quicksort.
give a good explanation of Bubble sort, Insertion sort, Selection sort, and Quicksort.
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method receives the following arrangement: [1,2,3,4,5] The invert method returns the array [5,4,3,2,1] Example 2: the print method receives the following array: [5,4,3,2,1] The print method prints: 5,4,3,2,1 (data separated by commas). TIP: for the print method use System.out.print () without the "ln".
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT