Question

In: Computer Science

Using Java compare how many swaps it takes a 1000 element array of integers. Use both...

Using Java compare how many swaps it takes a 1000 element array of integers. Use both quick-sort and heap sort. Only count when you swap values. Run the test in best case (array starts in sorted order), average case (array starts in random order), and in worst case array is sorted in reverse order. Print out the number of swaps for each sorting method's results.

Solutions

Expert Solution

//COMMENT IF YOU HAVE DOUBTS AND LIKE THE ANSWER.
class SortsSimulation
{
   public int quickSwapsCount = 0;
   public int heapSwapsCount = 0;
   public final int MAX_ELEMENTS = 1000;
   public int[] getSortedArray()
   {
       int[] array = new int[MAX_ELEMENTS];
       for(int i = 0;i<MAX_ELEMENTS;i++)
       {
           array[i] = i;
       }
       return array;
   }
   public int[] getReverseSortedArray()
   {
       int[] array = new int[MAX_ELEMENTS];
       for(int i = 0;i<MAX_ELEMENTS;i++)
       {
           array[i] = MAX_ELEMENTS-i;
       }
       return array;
   }
   public int[] getRandomSortedArray()
   {
       int[] array = new int[MAX_ELEMENTS];
       for(int i = 0;i<MAX_ELEMENTS/2;i++)
       {
           array[i] = MAX_ELEMENTS-i;
       }
       for(int i = MAX_ELEMENTS/2;i<MAX_ELEMENTS;i++)
       {
           array[i] = i;
       }
       return array;
   }
   public void quickSort(int[] A)
   {
       quickSwapsCount = 0;
       //call qSort()to sort the array A
       qSort(A,0,A.length-1);
       System.out.println("Total Number of Swaps: "+quickSwapsCount);
   }
   //qSort()function to recursively parition the array
   //and then sort
   public void qSort(int []A,int start,int stop){
       if(start < stop){
           //get pivot element
           int pivot = partition(A,start,stop);
           //call qSort()from start to pivot -1
           qSort(A,start,pivot-1);
           //call qSort()from pivot + 1 to stop
           qSort(A,pivot+1,stop);
       }
       return;
   }
   //function that sorts the given sub array
public int partition(int []array , int start, int stop){
       int ind = (start-1);// index of smaller element
       int pivot = array[stop];
       int temp;
       for (int j=start;j<stop;j++)
       {
           // If current element is smaller than the pivot
           if (array[j] < pivot)
           {
               quickSwapsCount++;
               ind++;
               temp = array[ind];
               array[ind] = array[j];
               array[j] = temp;
           }
       }
       temp = array[ind+1];
       array[ind+1] = array[stop];
       array[stop] = temp;
       quickSwapsCount++;
       return ind+1;
   }
   public void printArray(int [] array)
   {
       System.out.println("\n------ Total Array Elements : " + array.length );
       System.out.print("\n[ ");
       for(int i = 0;i<array.length;i++)
       {
           System.out.print(array[i]);
           if(i < array.length-1)
           {
               System.out.print(" , ");
           }
       }
       System.out.print("]\n");
   }
   public void heapSort(int array[])
   {
       heapSwapsCount = 0;
       int n = array.length;
       for (int i = n / 2 - 1;i >= 0;i--)
       {
           heapify(array, n, i);
       }
       int temp;
       for (int i=n-1;i>=0;i--)
       {
           // Move current root to end
           temp = array[0];
           array[0] = array[i];
           array[i] = temp;
           heapSwapsCount ++;
           // call max heapify on the reduced heap
           heapify(array, i, 0);
       }
       System.out.println("Total Number of Swaps: "+heapSwapsCount);
   }
  
// To heapify a subtree rooted with node ind which is
// an index in array[]. n is size of heap
   void heapify(int array[], int n, int ind)
   {
       int largest = ind;
       int left= 2*ind + 1;
       int right = 2*ind + 2;

       if (left< n && array[left] > array[largest])
       {
           largest = left;
       }

       if (right < n && array[right] > array[largest])
       {
           largest = right;
       }

       // If largest inds not root
       int swap;
       if (largest != ind)
       {
           heapSwapsCount ++;
           swap = array[ind];
           array[ind] = array[largest];
           array[largest] = swap;
           // Recursindvely heapify the affected sub-tree
           heapify(array, n, largest);
       }
}
   public static void main(String[] args)
   {
       SortsSimulation tester = new SortsSimulation();
       int qOne[] = tester.getSortedArray();
       int hOne[] = tester.getSortedArray();
      
       int qTwo[] = tester.getRandomSortedArray();
       int hTwo[] = tester.getRandomSortedArray();
      
       int qThree[] = tester.getReverseSortedArray();
       int hThree[] = tester.getReverseSortedArray();
      
       System.out.println("\n-------- Sorted Order ---------\n");
       System.out.println("Quick Sort: ");
       tester.quickSort(qOne);
       System.out.println("\nHeap Sort: ");
       tester.heapSort(hOne);
      
       System.out.println("\n-------- Random Sorted Order ---------\n");
       System.out.println("Quick Sort: ");
       tester.quickSort(qTwo);
       System.out.println("\nHeap Sort: ");
       tester.heapSort(hTwo);
      
       System.out.println("\n-------- Reverse Sorted Order ---------\n");
       System.out.println("Quick Sort: ");
       tester.quickSort(qThree);
       System.out.println("\n\nHeap Sort: ");
       tester.heapSort(hThree);
      
   }
}


Related Solutions

use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an...
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class HomeworkA { public static...
in java language Write a method called findNums that takes a two-dimension array of integers and...
in java language Write a method called findNums that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class Question2 {   ...
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Using Java, Given an array A[0 ... n-1], where each element of the array represent a...
Using Java, Given an array A[0 ... n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps...
Using Java programming, Write a LinkedList method swap() that takes two ints as arguments and swaps the elements at those two positions. The method should not traverse the list twice to find the elements, and it should not create or destroy any nodes.
***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array...
***USE JAVA AND NO IMPORTS*** Method editString takes a string and breaks it into an array of single        words which are then are edited based on the command        Possible Commands:        remove: for the sent words, remove those from the text        replace: replace the word in an even element of words with a word in an odd element of words        add: add the word given word in the index indicated after...
Find the K'th smallest element in an unsorted array of integers. Find the K'th largest element...
Find the K'th smallest element in an unsorted array of integers. Find the K'th largest element in an unsorted array of integers. please make two separate methods in java
Using Java please You are given an array of integers arr. Your task is to count...
Using Java please You are given an array of integers arr. Your task is to count the number of contiguous subarrays, such that each element of the subarray appears at least twice. E.g For arr = [0, 0, 0], the output should be duplicatesOnSegment(arr) = 3.
JAVA Write a method that removes the duplicate elements from an array list of integers using...
JAVA Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList list) Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is a sample run: Enter ten integers: 10 20 30 20 20 30 50 60 100 9 The distinct integers are: [10, 20, 30, 50, 60, 100, 9]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT