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

60.1K

Verified Solution

Question

Programming

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 testand 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++) {
           //Generaterandom array
            arr1[i] = rand.nextInt()%20;
            //Copy
            arr2[i]=arr3[i]=arr4[i]=arr1[i];
         }
       System.out.println(\"\n\");
       //Display insertion sort array andswap count
       intswapCnt=insertionSort(arr1);
       for (int i = 0; i < arr1.length;i++) {
            System.out.print(arr1[i]+\" \");
         }
       System.out.println(\"\nInsertionsort swap count = \"+swapCnt);
       //Display bubble sort array andswap count
       swapCnt=bubbleSort(arr2);
       for (int i = 0; i < arr2.length;i++) {
            System.out.print(arr2[i]+\" \");
         }
       System.out.println(\"\nBubble sortswap count = \"+swapCnt);
       //Display merge sort array and swapcount
       swapCnt=mergeSort(arr3,0,19);
       for (int i = 0; i < arr3.length;i++) {
            System.out.print(arr3[i]+\" \");
         }
       System.out.println(\"\nMerge sortswap count = \"+swapCnt);
       //Display quick sort array and swapcount
       swapCnt=quickSort(arr4,0,19);
       for (int i = 0; i < arr4.length;i++) {
            System.out.print(arr4[i]+\" \");
         }
       System.out.println(\"\nQuck sortswap count = \"+swapCnt);
   }
   //Insertion sort implementation
   public static int insertionSort(int[] arr) {
       //For swap count
       int swapCnt=0;
       //Loop until end of thearray
        for (int i = 1; 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 swapcount
        return swapCnt;
   }
   //Bubble sort implementation
   public static int bubbleSort(int[] arr) {
       int swapCnt=0;
       //Comparison loop
        for (int i = 0; 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,intend) {
            intmid,swapCnt=0;
           if(start            {
               mid =(start+end)/2;
              swapCnt=mergeSort(arr,start,mid);
              swapCnt=mergeSort(arr,mid+1,end);
              swapCnt+=merge(arr,start,mid,end);
            }
            returnswapCnt;
   }
   //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=newint[arr.length];
        while(i<=mid &&j<=end)
        {
           if(arr[i]           {
              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        {
           arr[k]=temp[k];
           swapCnt++;
           k++;
        }
        return swapCnt;
   }
   //Quick sort implemetation
    public static int quickSort(int arr[], intstart, 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

Answer & Explanation Solved by verified expert
4.1 Ratings (504 Votes)
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 listcount for i 0 to loop1 do swapped false for j 0 to loop1 do compare the adjacent elements if listj listj1 then swap them swap listj listj1 swapped true    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students