Question

In: Computer Science

Design a program in JAVA that allows you to experiment with different sort algorithms. The algorithms...

Design a program in JAVA that allows you to experiment with different sort algorithms. The algorithms are shell sort and quick sort. Assume that input data is stored in a text file.

Experimenting with a prototype data (integers from 1 to 10) to ensure that your implementation works correctly and the results match expectations. The program will sort what is in the text file and print the amount of comparisons and exchanges for both algorithms.

Solutions

Expert Solution

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;
        }
    }
}

------------------------------------------------------------------------------------------------------

a.txt

------------------------------------------------------------------

5
2
3
4
1

=================================

output

-------------------------------------------------------

Quick Sort:

Original order:
5 2 3 4 1
Sorted order:
1 2 3 4 5

Number of comps = 6
Number of swaps = 2


Shell Sort:

Original order:
5 2 3 4 1
Sorted order:
1 2 3 4 5

Number of comps = 8
Number of swaps = 3

==================================


Related Solutions

In this project you will implement and experiment with three different sorting algorithms: Insertion Sort, Selection...
In this project you will implement and experiment with three different sorting algorithms: Insertion Sort, Selection sort and Bubble Sort. The objective of this project is to understand the relation between the theoretical asymptotic complexities (Big-O) computed in class and the actual running times for different kinds of input. The enclosed source file main.cpp contains a program that reads two inputs: a letter specifying which sorting algorithm to use (I for InsertionSort , S for SelectionSort and B for BubbleSort),...
Java - Design and implement an application that creates a histogram that allows you to visually...
Java - Design and implement an application that creates a histogram that allows you to visually inspect the frequency distribution of a set of values. The program should read in an arbitrary number of integers that are in the range 1 to 100 inclusive; then produce a chart similar to the one below that indicates how many input values fell in the range 1 to 10, 11 to 20, and so on. Print one asterisk for each value entered. Sample...
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.
C++ Question- Write function templates for 5 sort algorithms: - Quick Sort Apply these algorithms to...
C++ Question- Write function templates for 5 sort algorithms: - Quick Sort Apply these algorithms to 10 different arrays of integers. Each array has 100 integer elements. The arrays are filled with random integer numbers.
Comparing (Sort Algorithms) Both of the two sorting algorithms will do "sort" on arrays which would...
Comparing (Sort Algorithms) Both of the two sorting algorithms will do "sort" on arrays which would contain x randomly generated integers where the value of x would be 10000, 20000, 40000 and 80000 (inputs). The parts of the program should be followed as..... 1. Set x = 10000, randomly generate x integers. Call qsort function to sort these integers and get the execution time. 2. Randomly generate another x integers. Call your own sorting algorithm and get the execution time....
Description: The purpose of the program is to create a Java ticket purchasing program that allows...
Description: The purpose of the program is to create a Java ticket purchasing program that allows for users to log in, purchase tickets, keep records of tickets purchased, and keep information about the user. Program Requirements: The following are the program requirements: Must be fully functioning including registration, log-in, view events, and purchase tickets Tickets should be purchased using “points”, no information should be provided via the user for payment method. Default each user to an allotted number of points...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java...
Write a program that implements the follow disk scheduling algorithms. You can use C or Java for this assignment. FCFS SSTF SCAN C-SCAN LOOK C-LOOK Your program will service a disk with 5000 cylinders (numbered 0 to 4999). Your program will generate a random initial disk head position, as well as a random series of 1000 cylinder requests, and service them using each of the 6 algorithms listed above. Your program will report the total amount of head movement required...
Program in java 1- Implement an algorithms to calculate the sum of all numbers in an...
Program in java 1- Implement an algorithms to calculate the sum of all numbers in an array.   2- Implement an algorithm to determine if an array has all unique integer values. 3- Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of nums. Example 1: Input: nums = [1,2,3,4] Output: [1,3,6,10] Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS...
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN Your program will service a disk with 5,000 cylinders numbered 0 to 4,999. The program will generate a random series of 50 requests and service them according to each of the algorithms you chose. The program will be passed the initial position of the disk head as a parameter on the command line and report the total amount of head...
Design an experiment in which you would perform some sort of hypothesis test about frequencies. For...
Design an experiment in which you would perform some sort of hypothesis test about frequencies. For your chosen experiment, explain your entire experimental design. What is your population of interest? How are you going to randomly sample the pop? How many replicates will you collect? What will be the form of the data you collect and what test will you perform? What is your null and alternative hypothesis?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT