Question

In: Computer Science

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 descending order

  /// subject to the differential between ups and downs using bubble

  /// sort

  /// @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[] sortBubble(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


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[] 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...
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.
How would I make a bubble sort and an optimized bubble sort with the code given?...
How would I make a bubble sort and an optimized bubble sort with the code given? I also need to implement a timer into each sort and display runtime with the sorts. NODE.H _______________________________________________________________________________________________________ /* node.h */ /* two classes 1: node.h 2. singlylinkedlist.h nod1 (value + pointer) ---> node2 ---> node3 ---> |||| <--- node.h ^ | singlylinkedlist ----------------*node head; */ #ifndef NODE_H #define NODE_H #include <iostream> using namespace std; class Node {    friend class singlyLinkedList; public:   ...
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.
For this assignment, find out how to do a bubble sort, selection sort, or insertion sort...
For this assignment, find out how to do a bubble sort, selection sort, or insertion sort in Java. You have the option to choose but you must label (with comments) the algorithm you choose to implement. Convert that algorithm to a generic algorithm and constraint it to only using numerics. Your method should accept an array as a parameter and sort the content of the array. If you wish, you can throw an exception if the contents of the array...
Bubble and Selection Sort For this assignment, you are to consider bubble and selection sort. Both...
Bubble and Selection Sort For this assignment, you are to consider bubble and selection sort. Both are O(n^2) however it may be possible to classify one algorithm as being more efficient than the other. You are to discuss which algorithm you feel is the most efficient and in what cases it will be more efficient. Provide any relevant test cases and code to support your belief. Submit a pdf containing your findings and test results along with any relevant code...
I need to write a method that sorts a provided Linked list with bubble sort and...
I need to write a method that sorts a provided Linked list with bubble sort and using ONLY Java List Iterator Methods (Link Below) https://docs.oracle.com/javase/8/docs/api/java/util/ListIterator.html     public static <E extends Comparable<? super E>> void bubbleSort(List<E> c) throws Exception {     // first line to start you off     ListIterator<E> iit = c.listIterator(), jit;     /**** Test code: do not modify *****/     List cc = new LinkedList(c);     Collections.sort(c);     ListIterator it1 = c.listIterator(), it2 = cc.listIterator(); while (it1.hasNext()) { if (!it1.next().equals(it2.next()))         throw new Exception("List not sorted");...
(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".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT