Question

The goal of this assignment is to reinforce sorting algorithms in C++. Specifically, the lab is to implement the shell sort.

*********************lab13.cpp********************

#include <cstdlib>

#include <cassert>

#include <ctime> // used in initialization of random number generator

using namespace std;

template <typename T>

bool is_sorted (T* a, size_t size);

// precondition: a is not NULL

// returns: whether array a is sorted

template <typename T>

void shell_sort (T* a, size_t size);

// precondition: a is not NULL

// postcondition: a is sorted in non-decreasing order

int* create_array (size_t size);

// returns an array with size random integers

int main ()

{

size_t size = 1000;

int* a = create_array (size);

shell_sort (a, size);

assert (is_sorted (a, size));

delete a;

return EXIT_SUCCESS;

}

--------Please implement the shell sort functions. Thank You

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

#include <cstdlib>

#include<iostream>

#include <cassert>

#include <ctime> // used in initialization of random number generator

using namespace std;

template <typename T>

bool is_sorted (T* a, size_t size)

// precondition: a is not NULL

// returns: whether array a is sorted

{

    int i;

   

    // traverse the array

    for( i = 0 ; i < size - 1 ; i++ )

    {

        // if the array is not sorted

        if( a[i] > a[i + 1] )

            return false;

    }

       

    // if the array is sorted  

    return true;

}

template <typename T>

void shell_sort (T* a, size_t size)

// precondition: a is not NULL

// postcondition: a is sorted in non-decreasing order

{

    int gap, i, x, y;

    T m;

    // a big gap in the beginning, then keep on reducing it

    for (gap = size / 2 ; gap > 0; gap /= 2)

    {

        for (x = gap; x < size ; x += 1)

        {

            m = a[x];

            // untill the appropriate position for i th element of a is

            // found, shift the elements

            for (y = x; y >= gap && a[y - gap] > m; y -= gap)

            {

                a[y] = a[y - gap];

            }

            // m is placed in the appropriate posotion

            a[y] = m;

        }

    }

}

int* create_array (size_t size)

// returns an array with size random integers

{

    // create a new array

    int *arr = new int[size];

   

    int i;

   

   for( i = 0 ; i < size ; i++ )

        arr[i] = rand() % 100;

       

    return arr;

}

int main ()

{

    size_t size = 1000;

   

    int* a = create_array (size);

   

    shell_sort<int>(a, size);

   

    assert (is_sorted<int>(a, size));

   

    delete a;

   

    return EXIT_SUCCESS;

}


Sample Output

Process exited after 0.95031 seconds with return valuee Press any key to continue . . .

Add a comment
Know the answer?
Add Answer to:
*********************lab13.cpp******************** #include <cstdlib> #include <cassert&...
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
  • //CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm....

    //CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm. #include <iostream> using std::cout; using std::endl; template<class T> void sort(T a[], int numberUsed); //Precondition: numberUsed <= declared size of the array a. //The array elements a[0] through a[numberUsed - 1] have values. //The assignment and < operator work for values of type T. //Postcondition: The values of a[0] through a[numberUsed - 1] have //been rearranged so that a[0] <= a[1] <=... <= a[numberUsed -...

  • Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify...

    Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify the class by adding sort member function (refer to Algorithm Analysis notes for sorting algorithms) Sample usage: a. sort(); Add the needed code in main to test your function. template <class T> 1/you can use keyword typename instead of class class Array { private: T *ptr; int size; public: Array(T arr[], int s); void print(); template <class T> Array<T>:: Array (T arr[], int s)...

  • c++ Implement Radix Sort Most sorting algorithms, like bubble, insertion, selection and shell follow similar implementations....

    c++ Implement Radix Sort Most sorting algorithms, like bubble, insertion, selection and shell follow similar implementations. Radix sort is a unique sorting algorithm. In this assignment, implement the Radix Sort algorithm, as explained the text book in chapter 2. Use the Numbers.txt file in the DataFiles folder for the numbers to sort. Extra credit is available for processing alphabetic strings instead of just numbers. Specification: * Using your Doubly-Linked list to create an dynamic array of Doubly-Linked lists (like lab1)....

  • Lab 10A Measure the program execution time One easy way to measure the program execution time is ...

    Lab 10A Measure the program execution time One easy way to measure the program execution time is encapsulate the useful timing functionality of C++ chrono library, This is illustrated inhttps://www.learncpp.com/cpp-tutorial/8-16-timing-your-code/ The example usingChronoTimer.cpp (Github/m10) is an example program using this chrono Timer class object to measure the Sorting on on an integer array of 10000 random integers based on the Bubble Sort vs. the C++ built-in Sort (an https://www.quora.com/Which-sorting-algorithm-does-STL-standard-template-library-use-in-c++. ) Please measure the performance of sorting the same array used...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion...

    Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion, Quick, Merge). Take help from lecture slides and welb . You will then create arrays of different sizes as instructed below, test each algorithm on each array and record the execution times of each individual algorithm on each array. . You will report these execution times in a table and then write a report explaining the execution times of the sorting algorithms according to...

  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • 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 }...

  • Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Jav...

    Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer {    // Please expand method main() to meet the lab requirements.       // You have the following sorting methods available:    // insertionSort(int[] a);    // selectionSort(int[] a);    // mergeSort(int[] a);    // quickSort(int[] a);    // The array will be in sorted order after the routines are called!   ...

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