Question

Please include all .cpp/.h files if neccesary, this is in C++ Sort an array of 10,000...

Please include all .cpp/.h files if neccesary, this is in 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 fist,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 sub list reduces to less than 20 ,sort the sublist using an insertion sort .

d-Sort the using pivot as the median of the fist,last and middle elements of the array .When the size of any sublist reduces to less than 20 ,sort the sublist using an insertion sort.

e-Calculate the print the CPU time for each of the preceding four steps .


To find the current CPU time , declare a variable say x, of the type clock_t . The statement x= clock(); store the current CPU time in x .You can check the current CPU time before and after a particular phase of the program , subtract the before time from the after time .Moreover you must include the header file ctime to use the date type clock_t and the function clock . use a random generator to initially fill the array.

Any help is greatly appreciated, thank you.


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

#include<iostream>
#include<ctime>
#include<cstdlib>

using namespace std;


// insertion sort

void insertionSort(int array[], int first, int size) {
   int i, j, key;

   for (i = 1 + first; i < size + first; i++) {
       key = array[i];
       j = i - 1;

       while (j >= first && array[j]>key) {
           array[j + 1] = array[j];
           j--;
       }

       array[j + 1] = key;
   }
}


// partition for quicksort
int partition(int array[], int first, int last) {

   // check partition size

if ((last - first) < 20) {
      
       insertionSort(array, first, (last - first + 1));
       // return middle
       return((last + first) / 2);
   }

   else {

       // using pivot
       int pivot = array[last];
       int index = (first - 1);

       for (int j = first; j<last; j++) {
           if (array[j]<pivot) {
               index++;

               int tmp = array[index];
               array[index] = array[j];
               array[j] = tmp;
           }
       }

       int temp = array[index + 1];
       array[index + 1] = array[last];
       array[last] = temp;

       return(index + 1);

   }

}

// quick sort calling

void quickSort(int array[], int first, int last) {
   if (first<last) {
       int pi = partition(array, first, last);

       quickSort(array, first, pi - 1);
       quickSort(array, pi + 1, last);
   }
}

// this function for print array

void print(int array[], int n) {

   for (int i = 0; i < n; i++) {
       if (i % 20 == 0) {
           cout << endl;
       }
       cout << array[i] << " ";
   }

}

int main() {

   int ary[10000];

   srand(time(0));

  
   // initialize by random numbers to array

   for (int i = 0; i<10000; i++) {
       ary[i] = rand();
   }

   //print(ary, 10000);
   //cout << endl << endl;

   clock_t start_ = clock();

   quickSort(ary, 0, 9999);

   clock_t end_ = clock();

   // cpu time
   cout << "Elapsed time in milliseconds : "
       << (end_ - start_)<< " ms" << endl;

   cout << endl << endl;

   //print(ary, 10000);

   system("pause");
   return(0);
}

c:\users\debojyoty chakravort documents\visual studio 2015 Projects\sortquick Debug\sortquick.exe Elapsed time in milliseconds : 1 ms Press any key to continue

Add a comment
Know the answer?
Add Answer to:
Please include all .cpp/.h files if neccesary, this is in C++ Sort an array of 10,000...
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
  • 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....

  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • Write a C++ program that does the following : Accepts a positive integer ( n )...

    Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following 1. In the first function: display elements of the array. Display the first 20 elements If the size is > 20 2. In the second function : Using recursion, Search for...

  • Problem: Write a C++ program that does the following : Accepts a positive integer ( n...

    Problem: Write a C++ program that does the following : Accepts a positive integer ( n ) from the keyboard . Create an character array of size n. Using a random number generator, populate the array with characters between 33 – 126. Create 7 individual functions and perform the following In the first function: display elements of the array. Display the first 20 elements If the size is > 20 In the second function : Using recursion, Search for a...

  • And the related algorithms: (20 points) Consider the following strategy for choosing a pivot element for...

    And the related algorithms: (20 points) Consider the following strategy for choosing a pivot element for the Partition subroutine of QuickSort, applied to an array A. .Let n be the number of elements of the array A. If n 24, perform an Insertion Sort of A and return. Otherwise: Choose 2n/2)| elements at random from n; let S be the new list with the chosen elements. Sort the list S using Insertion Sort and use the median m of S...

  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • graph binary search for size and time c++ //System Libraries #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <iomanip> #include <alg...

    graph binary search for size and time c++ //System Libraries #include <iostream> #include <string> #include <cstdlib> #include <ctime> #include <iomanip> #include <algorithm> using namespace std; //User Libraries //Global Constants, no Global Variables are allowed //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc... //Function Prototypes //Execution Begins Here! int main(int argc, char** argv) { int n, i, arr[50], search, first, last, middle,count=0,count_in,tot; clock_t start, end; float duration; cout<<"Enter total number of elements :"; cin>>n; cout<<"Enter numbers"; for (i=0; i<n;i++) cin>>arr[i]; cout<<"Enter a...

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size,...

    sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size, c;     float median;     printf("Enter the array size:\n");     scanf("%d", &size);     array = (int*) malloc(size * sizeof(int));     printf("Enter %d integers:\n", size);     for (c = 0; c < size; c++)         scanf("%d", &array[c]);     sort(array, size);     printf("Array sorted in ascending order:\n");     for (c = 0; c < size; c++)         printf("%d ", array[c]);     printf("\n");     median = find_median(array,...

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