Question

Generate a random list NUMBERS of size 100. Sort NUMBERS using QUICKSORT until the sublist has...

Generate a random list NUMBERS of size 100. Sort NUMBERS using QUICKSORT until the sublist has size 15 or less; then use INSERTIONSORT on the sublist. Generate 10 random lists of 100 numbers. Sort each list using QUICKSORT and then sort the same list using the combination of QUICKSORT and INSERTIONSORT as described above. Compare the times for each of the two algorithms. You can measure the duration of the time which each algorithm takes as follows: Instant first = Instant.now(); // sort all the lists using QUICKSORT Instant second = Instant.now(); Duration duration = Duration.between(first, second); Instant first = Instant.now(); // Sort all the lists using the combined QUICKSORT and INSERTIONSORT Instant second = Instant.now(); Duration duration = Duration.between(first, second);

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ClassName:QuickSort

public class QuickSort {
   /* This function takes last element as pivot,
places the pivot element at its correct
position in sorted array, and places all
smaller (smaller than pivot) to left of
pivot and all greater elements to right
of pivot */
   int partition(int arr[], int low, int high)
   {
       int pivot = arr[high];
       int i = (low-1); // index of smaller element
       for (int j=low; j<high; j++)
       {
           // If current element is smaller than or
           // equal to pivot
           if (arr[j] <= pivot)
           {
               i++;

               // swap arr[i] and arr[j]
               int temp = arr[i];
               arr[i] = arr[j];
               arr[j] = temp;
           }
       }

       // swap arr[i+1] and arr[high] (or pivot)
       int temp = arr[i+1];
       arr[i+1] = arr[high];
       arr[high] = temp;

       return i+1;
   }


   /* The main function that implements QuickSort()
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
   void sort(int arr[], int low, int high)
   {
       if (low < high)
       {
           /* pi is partitioning index, arr[pi] is
now at right place */
           int pi = partition(arr, low, high);

           // Recursively sort elements before
           // partition and after partition
           sort(arr, low, pi-1);
           sort(arr, pi+1, high);
       }
   }

   /* A utility function to print array of size n */
   static void printArray(int arr[])
   {
       int n = arr.length;
       for (int i=0; i<n; ++i)
           System.out.print(arr[i]+" ");
   }
}

**********************************************************************************************************************************************
Classname:InsertionSort

public class InsertionSort {
   /*Function to sort array using insertion sort*/
   void sort(int arr[])
   {
       int n = arr.length;
       for (int i = 1; i < n; ++i) {
           int key = arr[i];
           int j = i - 1;

           /* Move elements of arr[0..i-1], that are
   greater than key, to one position ahead
   of their current position */
           while (j >= 0 && arr[j] > key) {
               arr[j + 1] = arr[j];
               j = j - 1;
           }
           arr[j + 1] = key;
       }
   }

   /* A utility function to print array of size n*/
   static void printArray(int arr[])
   {
       int n = arr.length;
       for (int i = 0; i < n; ++i)
           System.out.print(arr[i] + " ");

       System.out.println();
   }

}

*********************************************************************************************************************************************

ClassName:SortingTestor

import java.time.Duration;
import java.time.Instant;
import java.util.Random;

public class SortingTestor {
   public static int[] generateList(Integer n) {
       int[] arr=new int[n];
       int value=0;
       Random random=new Random();
       for(int i=0;i<n;i++) {
           value=random.nextInt();
           arr[i]=value;
       }
       return arr;
   }
  
   public static void main(String[] args) {
       for(int i=0;i<10;i++) {
           int[] arr=generateList(100);
           QuickSort qObj=new QuickSort();
           System.out.println("\n>> List before sorting using Quick sort ");
           qObj.printArray(arr);
           Instant qfirst = Instant.now();
           qObj.sort(arr, 0, 99);
           Instant qsecond = Instant.now();
           Duration duration = Duration.between(qfirst, qsecond);
           System.out.println("\n Time taken in quick sort : "+duration.getNano());
           System.out.println("\n>> List after sorting using Quick sort");
           qObj.printArray(arr);
           InsertionSort iObj=new InsertionSort();
           System.out.println("\n>> List before sorting using Insertion Sort");
           iObj.printArray(arr);
           Instant ifirst = Instant.now();
           iObj.sort(arr);
           Instant isecond = Instant.now();
           Duration time = Duration.between(ifirst, isecond);
           System.out.println("\n Time taken in quick sort : "+time.getNano());
           System.out.println("\n>> List after sorting using Insertion Sort ");
           iObj.printArray(arr);
       }
   }

}

**********************************************************************************************************************************************

Here time difference is in seconds change it to milliseconds for seeing the exact difference

>> List before sorting using Quick sort -1566800969 1375617284 914172075 1319444316 -473126938 -661843509 654125783 519741757

Add a comment
Know the answer?
Add Answer to:
Generate a random list NUMBERS of size 100. Sort NUMBERS using QUICKSORT until the sublist has...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement and compare sorting algorithms. The task is to sort a list of integers using 5...

    Implement and compare sorting algorithms. The task is to sort a list of integers using 5 sorting algorithms: selection sort insertion sort merge sort heap sort quicksort Your program should include 5 separate sorting methods, though it is fine for them to call some common methods (like "swap") if needed. Each sorting method should also count the number of comparison operations and assignment operations on the array elements during the sorting process. In the main program, two types of array...

  • Sort the list of numbers: 35, 12, 6, 23, 18 using Selection sort. Work through each...

    Sort the list of numbers: 35, 12, 6, 23, 18 using Selection sort. Work through each step of the algorithm. The algorithm for selection sort is as follows: i. Find the smallest item in a list. ii. Swap this value with the value currently at the front of the list. iii. Repeat Steps 1 and 2 with the current size of the list minus one (list size = list size-1)

  • PYTHON Generate a list of 50 random numbers ranging from 1 to 100 & use a...

    PYTHON Generate a list of 50 random numbers ranging from 1 to 100 & use a function that will remove duplicates from this list, compact it, and print the original and resulting lists. It MUST be well commented code with a description of the behavior.

  • You want to sort (in increasing order) the following array of integers using quicksort as we...

    You want to sort (in increasing order) the following array of integers using quicksort as we have described it and used it in class. You are asked to specifically show your steps and the resulting array after one pass of quicksort. Show and explain each of your steps. Note 1: in case you are not using the algorithm presented and traced in class, you are expected to show all your steps accompanied with algorithm instructions and variables' values. Note 2:...

  • Using C++, sort an array of 10,000 elements using the quick sort algorithm as follows: a....

    Using C++, sort an array of 10,000 elements using the quick sort algorithm as follows: a. Sort the array using pivot as the middle element of the array. b. Sort the array using pivot as the median of the first, last, and middle elements of the array. c. Sort the array using pivot as the middle element of the array. However, when the size of any sublist reduces to less than 20, sort thesublis t using an insertion sort. d....

  • Can I get some help with this question for c++ if you can add some comments...

    Can I get some help with this question for c++ if you can add some comments too to help understand that will be much appreciated. Code: #include <cstdlib> #include <getopt.h> #include <iostream> #include <string> using namespace std; static long comparisons = 0; static long swaps = 0; void swap(int *a, int *b) {     // add code here } void selectionSort(int *first, int *last) {     // add code here } void insertionSort(int *first, int *last) {     // add code here }...

  • Java We will look at the behavior of three different sorts on randomly generated lists of...

    Java We will look at the behavior of three different sorts on randomly generated lists of different sizes The three sorts are bubblesort, insertion sort, and quicksort. Randomly generate integers in the range 0-99 for your random numbers. You may choose your own random number generation technique (this is an exception to the no-outside-help requirement for this assignment. You must document the source of your random number generation in the code. ° Here is what your code should do: 1....

  • Directions: Problem 1: Write (using pen-and-paper rather than code) the list after each pass of quick...

    Directions: Problem 1: Write (using pen-and-paper rather than code) the list after each pass of quick and merge sort for the following list of numbers. Assume that you are sorting the numbers into ascending order. For quick sort, assume that the first number from the sublist is chosen as the pivot. 54 17 21 18 4 7 19 41 Problem 2: Write the list after each pass of the quick sort algorithm for the following list of numbers, using the...

  • Consider the following python code that will sort the list of numbers using the Bubble Sort...

    Consider the following python code that will sort the list of numbers using the Bubble Sort algorithm def bubbleSort(alist): print(alist) for j in range (len(alist) - 1, 8, -1): for i in range(): if alist[i] > alist[i + 1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp print(alist) alist = [52, 25, 94, 17, 25, 52] bubbleSort (alist) print(alist) Sort the following series of values into ascending order using the Bubble Sort algorithm. Write out the complete row of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT