Question

In: Computer Science

Consider an array of 10 elements sorted in ascending order. Assume that you sort the table...

Consider an array of 10 elements sorted in ascending order. Assume that you sort the table in descending order by running a sorting algorithm.

a) Count the exact number of changes made to the array (i.e. the number of operations of assignment T [...]=) if the bubble sort is applied (Assuming two assignments per swap).

b) Count the exact number of changes made to the array (i.e. the number of operations of assignment T [...]=) if heap-sort is applied (Assuming two assignments per swap).

Solutions

Expert Solution

solution:

PROGRAM::

import java.util.*;
public class Main
{
   public static void main(String[] args) {
       int arr[] = new int[]{1,2,3,4,5,6,7,8,9,10};
       int len = arr.length;
       int temp=0;
       int changes=0;
       //apply bubble sort in descending order..
       for(int i=0;i<len-1;i++)
       {
       for(int j=0;j<len-i-1;j++)
       {
       if(arr[j]<arr[j+1])
       {
       temp = arr[j];
       arr[j] = arr[j+1];
       arr[j+1] = temp;
       changes += 2;
       }
       }
       }
       System.out.print("Using bubble sort printing Elements in Descending order: ");
       for(int i=0;i<len;i++)
       System.out.print(arr[i]+" ");
       System.out.println("\nNumber of Changes made to array using Bubble sort: "+changes);
       changes =0;
       Arrays.sort(arr); //sort the array again in ascending order..
       //apply heap sort in descending order..
       changes = heapSort(arr, len, changes);
System.out.print("Using heap sort printing Elements in Descending order: ");
printArray(arr, len);
System.out.print("Number of Changes made to array using Heap sort: "+changes);
   }
   public static int heapify(int arr[], int n, int i, int changes)
{
int smallest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
  
if (l < n && arr[l] < arr[smallest])
smallest = l;
  

if (r < n && arr[r] < arr[smallest])
smallest = r;
  
if (smallest != i) {
int temp = arr[i];
arr[i] = arr[smallest];
arr[smallest] = temp;
changes += 2;

changes = heapify(arr, n, smallest, changes);
}
return changes;
}
public static int heapSort(int arr[], int n, int changes)
{
int c1=0,c2=0;
for (int i = n / 2 - 1; i >= 0; i--)
c1 = heapify(arr, n, i, changes);
changes = c1+changes;
for (int i = n - 1; i >= 0; i--) {
  
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
changes += 2;
c2 = heapify(arr, i, 0, changes);
}
return changes;
}
public static void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
}

SCREENSHOTS:

please give me thumb up


Related Solutions

What is the running time of Quicksort when the input array is sorted in ascending order?...
What is the running time of Quicksort when the input array is sorted in ascending order? (please explain in detail)
Written in MIPS assembly language If given an array which is sorted in ascending order, as...
Written in MIPS assembly language If given an array which is sorted in ascending order, as well as its length and a target value. You are asked to find the index of this target value in the array using binary search algorithm. Here is an example: array 1, 4, 6, 8, 10, 12 length 6 target 8 2 In this case, the returned result should be assigned to be 3, as array[3] is equal to target. (Note: The target will...
1a .Write a program that perform insertion sort (ascending order) on an input array and print...
1a .Write a program that perform insertion sort (ascending order) on an input array and print the total number of comparisons that have been made. You can implement by using any programming languages such as C/C++. For example, in the case of array B = [ 30 , 10 , 20 , 40 ], there are 4 needed comparisons to perform insertion sort (30 vs 10, 20 vs 30, 20 vs 10, and 40 vs 30). 1b. Write a program...
// This program uses a bubble sort to arrange an array of integers in // ascending...
// This program uses a bubble sort to arrange an array of integers in // ascending order (smallest to largest). It then display the array // before the sorting and after the sorting. Modify the program so it orders // integers in descending order (largest to smallest). Then add some code // to display the array at each step of the algorithm. You don't have to // modify anything in the main() function. All modification are inside // the bubbleSortArray()...
Language Javascript Implement Insertion Sort 1. Non-increasing order, 2. At least an array of 10 elements.,...
Language Javascript Implement Insertion Sort 1. Non-increasing order, 2. At least an array of 10 elements., 3. You can use a static array.
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending...
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. I need this in java language.
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending...
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. I need this in java language. Radix Sort assignment (CISP430 ignore this part) This is only for those who are using Java How many classes do I need? A: Node, Queue, Radix, Driver...
Given the following array of 8 elements, show the steps to sortthe elements in ascending...
Given the following array of 8 elements, show the steps to sort the elements in ascending order using a radix sort. Fill in each of the following blanks.81   546   677   9   97   12   53   22Adjust with 0s:Buckets for ones digit:Buckets for tens digit:Final Result:
You are provided with an array of Strings and a list of Strings. Sort the elements...
You are provided with an array of Strings and a list of Strings. Sort the elements (1) in natural order, (2) in reverse natural order and (3) by the length of each String. You can fill in the details by using the following stub file: import java.util.Arrays; import java.util.Collections; import java.util.List; public class Midterm01 { public static void main(String[] args) { String[] arrayOfCities = { "Atlanta", "Savannah", "New York", "Dallas", "Rio" }; List<String> listOfCities = Arrays.asList("Atlanta", "Savannah", "New York", "Dallas",...
Excel sorting In excel how do you sort a column of months in ascending order and...
Excel sorting In excel how do you sort a column of months in ascending order and not alphabetical when the month is in word format?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT