Question

In: Computer Science

I need a pseudocode and UML for this program. import java.util.Random; public class SortandSwapCount {   ...

I need a pseudocode and UML for this program.

import java.util.Random;

public class SortandSwapCount {

   public static void main(String[] args) {
       //Generate random array for test and copy that into 3 arrays
       int[] arr1=new int[20];
       int[] arr2=new int[20];
       int[] arr3=new int[20];
       int[] arr4=new int[20];
       Random rand = new Random();
       for (int i = 0; i < arr1.length; i++) {
           //Generate random array
             arr1[i] = rand.nextInt()%20;
             //Copy
             arr2[i]=arr3[i]=arr4[i]=arr1[i];
         }
       System.out.println("\n");
       //Display insertion sort array and swap count
       int swapCnt=insertionSort(arr1);
       for (int i = 0; i < arr1.length; i++) {
             System.out.print(arr1[i]+" ");
         }
       System.out.println("\nInsertion sort swap count = "+swapCnt);
       //Display bubble sort array and swap count
       swapCnt=bubbleSort(arr2);
       for (int i = 0; i < arr2.length; i++) {
             System.out.print(arr2[i]+" ");
         }
       System.out.println("\nBubble sort swap count = "+swapCnt);
       //Display merge sort array and swap count
       swapCnt=mergeSort(arr3,0,19);
       for (int i = 0; i < arr3.length; i++) {
             System.out.print(arr3[i]+" ");
         }
       System.out.println("\nMerge sort swap count = "+swapCnt);
       //Display quick sort array and swap count
       swapCnt=quickSort(arr4,0,19);
       for (int i = 0; i < arr4.length; i++) {
             System.out.print(arr4[i]+" ");
         }
       System.out.println("\nQuck sort swap count = "+swapCnt);
   }
   //Insertion sort implementation
   public static int insertionSort(int[] arr) {
       //For swap count
       int swapCnt=0;
       //Loop until end of the array
        for (int i = 1; i < arr.length; ++i) {
            int temp = arr[i];
            int j = i - 1;
           //Swapping test
            while (j >= 0 && arr[j] >temp) {
                arr[j + 1] = arr[j];
                //Increment swap count
                swapCnt++;
                j = j - 1;
            }
            arr[j + 1] = temp;
        }
        //Return swap count
        return swapCnt;
   }
   //Bubble sort implementation
   public static int bubbleSort(int[] arr) {
       int swapCnt=0;
       //Comparison loop
        for (int i = 0; i < arr.length-1; i++)
            for (int j = 0; j < arr.length-i-1; j++)
               //Compare and swap
                if (arr[j] > arr[j+1])
                {
                   //Swap and increment swap count
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                    swapCnt++;
                }
        return swapCnt;
   }
   //Mergesort implementation
   public static int mergeSort(int[] arr,int start,int end) {
            int mid,swapCnt=0;
            if(start<end)
            {
                mid = (start+end)/2;
               swapCnt= mergeSort(arr,start,mid);
                swapCnt=mergeSort(arr,mid+1,end);
                swapCnt+=merge(arr,start,mid,end);
            }
            return swapCnt;
   }
   //Merge 2 parts of the array
   public static int merge(int arr[], int start, int mid, int end)
   {
        int i=start,j=mid+1,k,index = start,swapCnt=0;
        int[] temp=new int[arr.length];
        while(i<=mid && j<=end)
        {
            if(arr[i]<arr[j])
            {
                temp[index] = arr[i];
                swapCnt++;
                i = i+1;
            }
            else
            {
                temp[index] = arr[j];
                j = j+1;
            }
            index++;
        }
        if(i>mid)
        {
            while(j<=end)
            {
                temp[index] = arr[j];
                index++;
                j++;
                swapCnt++;
            }
        }
        else
        {
            while(i<=mid)
            {
                temp[index] = arr[i];
                index++;
                i++;
                swapCnt++;
            }
        }
        k = start;
        while(k<index)
        {
            arr[k]=temp[k];
            swapCnt++;
            k++;
        }
        return swapCnt;
   }
   //Quick sort implemetation
    public static int quickSort(int arr[], int start, int end)
    {
       int swapCnt=0;
        if (start < end) {
           //Partition
            int p = arr[end];
            int i = (start - 1);
            for (int j = start; j <=end - 1; j++) {
                if (arr[j] <= p) {
                    i++;
                  //Swapping
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                    swapCnt++;
                }
            }
          //Swapping
            int temp = arr[i + 1];
            arr[i + 1] = arr[end];
            arr[end] = temp;
            swapCnt++;
            int pi= i + 1;
           //Recursion
            swapCnt+=quickSort(arr, start, pi - 1);
            swapCnt+=quickSort(arr, pi + 1, end);
        }
       return swapCnt;
    }
}

Output

Insertion sort swap count = 106
-18 -17 -16 -14 -13 -13 -9 -9 -7 -7 -3 -1 0 1 4 6 13 15 16 17
Bubble sort swap count = 106
-18 -17 -16 -14 -13 -13 -9 -9 -7 -7 -3 -1 0 1 4 6 13 15 16 17
Merge sort swap count = 58
-18 -17 -16 -14 -13 -13 -9 -9 -7 -7 -3 -1 0 1 4 6 13 15 16 17
Quick sort swap count = 49

Solutions

Expert Solution

Ans

Main Pseudocode :  

1. declear 5 array and pupose to sort  

call sort function

buble sort function Pseudocode

procedure bubbleSort( list : array of items )

loop = list.count;

for i = 0 to loop-1 do:
swapped = false
      for j = 0 to loop-1 do:
/* compare the adjacent elements */   
if list[j] > list[j+1] then
/* swap them */
swap( list[j], list[j+1] )         
swapped = true
end if
end for
    /*if no number was swapped that means
array is sorted now, break the loop.*/
if(not swapped) then
break
end if
end for
end procedure return list

quick Sort function Pseudo code

procedure quickSort(left, right)

   if right-left <= 0
      return
   else 
 leftPointer = left
   rightPointer = right - 1

   while True do
      while A[++leftPointer] < pivot do
         //do-nothing            
      end while
                
      while rightPointer > 0 && A[--rightPointer] > pivot do
         //do-nothing         
      end while
      if leftPointer >= rightPointer
         break
      else                
         swap leftPointer,rightPointer
      end if
   end while 
   swap leftPointer,right
   cnt=    quickSort(left,partition-1)
   cnt=    quickSort(partition+1,right)    
   end if               
end procedure

// I write while bescause in code has recusrsive call and then the function back its cursor so for understanding i write this //

mergersort code pseudo code

procedure mergesort( var a as array )
if ( n == 1 ) return a

var l1 as array = a[0] ... a[n/2]
var l2 as array = a[n/2+1] ... a[n]

l1 = mergesort( l1 )
l2 = mergesort( l2 )

return merge( l1, l2 )
end procedure

procedure merge( var a as array, var b as array )

var c as array
while ( a and b have elements )
if ( a[0] > b[0] )
add b[0] to the end of c
remove b[0] from b
else
add a[0] to the end of c
remove a[0] from a
end if
end while

while ( a has elements )
add a[0] to the end of c
remove a[0] from a
end while

while ( b has elements )
add b[0] to the end of c
remove b[0] from b
end while

return c

end procedure

Comment down if any mistake

Comment down if Any Query

Pleause Thumbs uo if you satisfy with Answer


Related Solutions

import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following:...
import java.util.Random; import java.util.Scanner; public class Compass { // You will need to do the following: // // 1.) Define a private instance variable which can // hold a reference to a Random object. // // 2.) Define a constructor which takes a seed value. // This seed will be used to initialize the // aforementioned Random instance variable. // // 3.) Define a static method named numberToDirection // which takes a direction number and returns a String // representing...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r =...
import java.util.Random; import java.util.Scanner; public class Compass { public Random r; public Compass(long seed){ r = new Random(seed); }    public static String numberToDirection(int a){ if(a==0) return "North";    if(a==1) return "NorthEast"; if(a==2) return "East"; if(a==3) return "Southeast"; if(a==4) return "South"; if(a==5) return "Southwest"; if(a==6) return "West";    if(a==7) return "Northwest";    return "Invalid Direction" ; } public String randomDirection(){ return numberToDirection(r.nextInt()% 4 + 1); } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter seed: ");...
How would I prepare pseudocode and a flowchart for this? import java.util.Scanner; public class HotDogsBuns {...
How would I prepare pseudocode and a flowchart for this? import java.util.Scanner; public class HotDogsBuns {    private static int HOTDOG_COUNT_PKG = 10;    private static int BUNS_COUNT_PKG = 8;       public static void main(String[] args) {        //Scanner object to get user input        Scanner keyboard = new Scanner(System.in);               // Get number of people        System.out.print("How many people will be competing in the contest?: ");        int noOfPeople = keyboard.nextInt();...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) {...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) { int N; Scanner keybd = new Scanner(System.in); int[] counts = new int[12];    System.out.print("Enter the number of trials: "); N = keybd.nextInt();    Random die1 = new Random(); Random die2 = new Random(); int value1, value2, sum; for(int i = 1; i <= N; i++) { value1 = die1.nextInt(6) + 1; value2 = die2.nextInt(6) + 1; sum = value1 + value2; counts[sum-1]++; }   ...
<<<<<<<<. I need the UML diagram for all classes.java below. >>>>> // Vehicle.java public class Vehicle...
<<<<<<<<. I need the UML diagram for all classes.java below. >>>>> // Vehicle.java public class Vehicle {    // data members declared as private    private String make;    private double weight;    private double height;    private double length;    private int maxSpeed;    private int noOfDoors;    private int numberSeats;    /**    * @param make    * @param weight    * @param height    * @param length    * @param maxSpeed    * @param noOfDoors    *...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task>...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task> list;//make private array public ToDoList() { //this keyword refers to the current object in a method or constructor this.list = new ArrayList<>(); } public Task[] getSortedList() { Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array //fills array with given values by using a for loop for (int i = 0; i < this.list.size(); i++) { sortedList[i] = this.list.get(i);...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts)...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts) { return quarts * 0.25; } public static double milesToFeet(double miles) { return miles * 5280; } public static double milesToInches(double miles) { return miles * 63360; } public static double milesToYards(double miles) { return miles * 1760; } public static double milesToMeters(double miles) { return miles * 1609.34; } public static double milesToKilometer(double miles) { return milesToMeters(miles) / 1000.0; } public static double...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {...
I need a java flowchart diagram for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");   ...
(Using Random Class) The following UML Class Diagram describes the java Random class: java.util.Random +Random() +Random(seed:...
(Using Random Class) The following UML Class Diagram describes the java Random class: java.util.Random +Random() +Random(seed: long) +nextInt(): int +nextInt(n: int): int +nextLong(): long +nextDouble(): double +nextFloat(): float +nextBoolean(): boolean Constructs a Random object with the current time as its seed. Constructs a Random object with a specified seed. Returns a random int value. Returns a random int value between 0 and n (exclusive). Returns a random long value. Returns a random double value between 0.0 and 1.0 (exclusive). Returns...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT