Question

In: Computer Science

I have this program, it sorts a file using shell sort and quick sort then prints...

I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the bubble sort algorithm. Here is the code. The language is Java.

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Sort
{
   public static int numOfComps = 0,numOfSwaps = 0;

    public static void main(String[] args)
   {
    
   try{
       Scanner scanner = new Scanner(new File("a.txt"));//your text file here
      
       int [] values = new int [100];
       int i = 0;
       while(scanner.hasNextInt())
       {
           values[i++] = scanner.nextInt();
       }
       int n=i;
       int arr[]=new int[n];
              for(i=0;i<n;i++)
               arr[i]=values[i];


      
               System.out.println("\n\nQuick Sort:");
           // Display the array's contents.
              System.out.println("\nOriginal order: ");
              for(i=0;i<n;i++)
               System.out.print(arr[i] + " ");

              // Sort the array.
              quickSort(arr,n);

              // Display the array's contents.
              System.out.println("\nSorted order: ");
              for(i=0;i<n;i++)
               System.out.print(arr[i] + " ");


              System.out.println("\n\nNumber of comps = " + numOfComps);
               System.out.println("Number of swaps = " + numOfSwaps);
              
           numOfComps = 0;
           numOfSwaps = 0;
          
           System.out.println("\n\nShell Sort:");
           // Display the array's contents.
              System.out.println("\nOriginal order: ");
              for(i=0;i<n;i++)
               System.out.print(values[i] + " ");

          
              // Sort the array.
              shellSort(values,n);

              // Display the array's contents.
              System.out.println("\nSorted order: ");
              for(i=0;i<n;i++)
               System.out.print(values[i] + " ");


              System.out.println("\n\nNumber of comps = " + numOfComps);
              System.out.println("Number of swaps = " + numOfSwaps);
          
              System.out.println();
      

      }
       catch (FileNotFoundException e) {
           e.printStackTrace();
       }

   }

   public static void quickSort(int array[],int n )
   {
      doQuickSort(array, 0, n- 1);
   }

   private static void doQuickSort(int array[], int start, int end)
   {
      int pivotPoint;

      if (start < end)
      {
         //numOfComps++;

         // Get the pivot point.
         pivotPoint = partition(array, start, end);

         // Sort the first sub list.
         doQuickSort(array, start, pivotPoint - 1);

         // Sort the second sub list.
         doQuickSort(array, pivotPoint + 1, end);
      }
   }

   private static int partition(int array[], int start, int end)
   {
      int pivotValue;    // To hold the pivot value
      int endOfLeftList; // Last element in the left sub list.
      int mid;           // To hold the mid-point subscript

      // Find the subscript of the middle element.
      // This will be our pivot value.
      mid = (start + end) / 2;

      // Swap the middle element with the first element.
      // This moves the pivot value to the start of
      // the list.
      swap(array, start, mid);

      // Save the pivot value for comparisons.
      pivotValue = array[start];

      // For now, the end of the left sub list is
      // the first element.
      endOfLeftList = start;

      // Scan the entire list and move any values that
      // are less than the pivot value to the left
      // sub list.
      for (int scan = start + 1; scan <= end; scan++)
      {

         if (array[scan] < pivotValue)
         {
            endOfLeftList++;
            swap(array, endOfLeftList, scan);

                numOfSwaps ++;
         }
         numOfComps++;
      }

      // Move the pivot value to end of the
      // left sub list.
      swap(array, start, endOfLeftList);

      // Return the subscript of the pivot value.
      return endOfLeftList;
   }

   private static void swap(int[] array, int a, int b)
   {
      int temp;

         temp = array[a];
         array[a] = array[b];
         array[b] = temp;
    }
  
  
//shell sort

    public static void SegmentedInsertionSort (int[] array, int N, int gap)
    {
        for (int index = gap ; index < N ; index++)
        {
            int temp;
            int j = index - gap;
            while (j >= 0)
            {
                  numOfComps++;
                  if (array[j]>array[j+gap])
                  {
                       temp = array[j];
                       array[j] = array[j + gap];
                       array[j + gap] = temp;
                       j = j - gap;
                       numOfSwaps++;
                  }
                  else j = -1;
            }
        }
    }

    public static void shellSort (int[] array,int n)
    {
        int N = n;
        int gap = N/2;
        while (gap > 0)
        {
            SegmentedInsertionSort(array, N, gap);
            gap = gap / 2;
        }
    }
}

Solutions

Expert Solution

Program: In this program, we add the bubble sort function, which keeps count of the total number of comparisons and swaps made during the sorting. The total number of comparisons made in a bubble sort is (n*(n-1))/2.

Below is the implementation:

Code:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Sort
{
    public static int numOfComps = 0,numOfSwaps = 0;

    public static void main(String[] args)
    {

        try{
            Scanner scanner = new Scanner(new File("a.txt"));//your text file here

            int [] values = new int [100];
            int i = 0;
            while(scanner.hasNextInt())
            {
                values[i++] = scanner.nextInt();
            }
            int n=i;
            int arr[]=new int[n];
            int array[] = new int[n];
            for(i=0;i<n;i++) {
                arr[i] = values[i];
                array[i] = values[i];
            }


            System.out.println("\n\nQuick Sort:");
            // Display the array's contents.
            System.out.println("\nOriginal order: ");
            for(i=0;i<n;i++)
                System.out.print(arr[i] + " ");

            // Sort the array.
            quickSort(arr,n);

            // Display the array's contents.
            System.out.println("\nSorted order: ");
            for(i=0;i<n;i++)
                System.out.print(arr[i] + " ");


            System.out.println("\n\nNumber of comps = " + numOfComps);
            System.out.println("Number of swaps = " + numOfSwaps);

            numOfComps = 0;
            numOfSwaps = 0;

            System.out.println("\n\nShell Sort:");
            // Display the array's contents.
            System.out.println("\nOriginal order: ");
            for(i=0;i<n;i++)
                System.out.print(values[i] + " ");


            // Sort the array.
            shellSort(values,n);

            // Display the array's contents.
            System.out.println("\nSorted order: ");
            for(i=0;i<n;i++)
                System.out.print(values[i] + " ");


            System.out.println("\n\nNumber of comps = " + numOfComps);
            System.out.println("Number of swaps = " + numOfSwaps);

            numOfComps = 0;
            numOfSwaps = 0;

            System.out.println("\n\nBubble Sort:");
            // Display the array's contents.
            System.out.println("\nOriginal order: ");
            for(i=0;i<n;i++)
                System.out.print(array[i] + " ");

            // Sort the array.
            bubbleSort(array);

            // Display the array's contents.
            System.out.println("\nSorted order: ");
            for(i=0;i<n;i++)
                System.out.print(array[i] + " ");


            System.out.println("\n\nNumber of comps = " + numOfComps);
            System.out.println("Number of swaps = " + numOfSwaps);

        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    public static void quickSort(int array[],int n )
    {
        doQuickSort(array, 0, n- 1);
    }

    private static void doQuickSort(int array[], int start, int end)
    {
        int pivotPoint;

        if (start < end)
        {
            //numOfComps++;

            // Get the pivot point.
            pivotPoint = partition(array, start, end);

            // Sort the first sub list.
            doQuickSort(array, start, pivotPoint - 1);

            // Sort the second sub list.
            doQuickSort(array, pivotPoint + 1, end);
        }
    }

    private static int partition(int array[], int start, int end)
    {
        int pivotValue;    // To hold the pivot value
        int endOfLeftList; // Last element in the left sub list.
        int mid;           // To hold the mid-point subscript

        // Find the subscript of the middle element.
        // This will be our pivot value.
        mid = (start + end) / 2;

        // Swap the middle element with the first element.
        // This moves the pivot value to the start of
        // the list.
        swap(array, start, mid);

        // Save the pivot value for comparisons.
        pivotValue = array[start];

        // For now, the end of the left sub list is
        // the first element.
        endOfLeftList = start;

        // Scan the entire list and move any values that
        // are less than the pivot value to the left
        // sub list.
        for (int scan = start + 1; scan <= end; scan++)
        {

            if (array[scan] < pivotValue)
            {
                endOfLeftList++;
                swap(array, endOfLeftList, scan);

                numOfSwaps ++;
            }
            numOfComps++;
        }

        // Move the pivot value to end of the
        // left sub list.
        swap(array, start, endOfLeftList);

        // Return the subscript of the pivot value.
        return endOfLeftList;
    }

    private static void swap(int[] array, int a, int b)
    {
        int temp;

        temp = array[a];
        array[a] = array[b];
        array[b] = temp;
    }


//shell sort

    public static void SegmentedInsertionSort (int[] array, int N, int gap)
    {
        for (int index = gap ; index < N ; index++)
        {
            int temp;
            int j = index - gap;
            while (j >= 0)
            {
                numOfComps++;
                if (array[j]>array[j+gap])
                {
                    temp = array[j];
                    array[j] = array[j + gap];
                    array[j + gap] = temp;
                    j = j - gap;
                    numOfSwaps++;
                }
                else j = -1;
            }
        }
    }

    public static void shellSort (int[] array,int n)
    {
        int N = n;
        int gap = N/2;
        while (gap > 0)
        {
            SegmentedInsertionSort(array, N, gap);
            gap = gap / 2;
        }
    }

    // Bubble Sort
    public static void bubbleSort(int[] array)
    {
        int n = array.length;

        // Create a for loop till array.length - 1
        for (int i = 0; i < n-1; i++)
        {
            // Compare each element with its next element
            for (int j = 0; j < n-i-1; j++)
            {
                // Increment number of Comparisons
                numOfComps++;
                if(array[j] > array[j+1])
                {
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;

                    // If a swap is made then increment number of swaps
                    numOfSwaps++;
                }
            }
        }
    }
}

Output:



Related Solutions

I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the insertion algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here          ...
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort,...
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort, and selection sort. It must show how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718 3492 5068 9674 8578 8323...
Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge...
2 real-time examples on the Insertion sort, Bubble sort, Selection sort, Quick sort, Shell sort, Merge sort, Radix sort, Bucket sort, and Counting sort.
JAVA - Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like...
JAVA - Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like below and must use a Quick sort Algorithm ================ 6 10 4 19 10 12 8 6 0 1 2 3 ================ The first number "6" represents the index of the element to return after sort the second number on the top "10" represents the number of elements or size of array. The following numbers and lines are the elements that need to go...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the...
Write a program that computes and prints the average of numbers in a text file. I...
Write a program that computes and prints the average of numbers in a text file. I created a text file integers.txt that has the numbers 5,4,3,2,1. I need to define the average function Define the main function which will include the following things 1. prompt user for input of text file name 2. open and read input file, can be done before or inside high order functions 3. use two high order functions 4.calculate and display averages and original ist...
Write a MIPS program that uses an implementation of quick sort to sort an array of...
Write a MIPS program that uses an implementation of quick sort to sort an array of numbers (Translate the following code into (Mars Assembly language). Quicksort Implementation - C int partition(int arr [] , int left , int right) { int i=left, j=right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr [ i ] < pivot) i ++; while (arr [ j ] > pivot) j −−; if (i <= j)...
The quick sort algorithm always divides the list into two equal sized sublists, then sorts each...
The quick sort algorithm always divides the list into two equal sized sublists, then sorts each sublists, and then combines both sublists.. True of False
Create a quick and merge sort algorithm that sorts 6 9 8 12 3 1 7...
Create a quick and merge sort algorithm that sorts 6 9 8 12 3 1 7 In java please.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT