1. Write a function to draw an int array using stars. Assume values in the range
ID: 3624630 • Letter: 1
Question
1. Write a function to draw an int array using stars. Assume values in the range 0-20.
For example, for the array { 2, 4, 2, 1, 7 } you would draw:
**
****
**
*
*******
1.1. Each line is one element of the array and the number of stars is the same as
the value.
2. Call this function after every insertion operation in insertion sort (It should display the
array n-1 times).
2.1. You should use an array of size 16 with randomly generated data between
0-20.
2.2. Call the function once before any sorting also.
3. Visualize shell sort in the same manner after each h-sort.
4. Visualize shell sort as a grid before and after each h-sort. In this case, youll print
the data in h columns.
4.1. Use Shells increment sequence (start at h = n/2, divide by 2 every time)
4.2. Show shell sort on the same array as you used for insertion sort.
4.3. Also show shell sort on a worst-case n2 array. There are many such arrays;
think about it a little and make one (of size 16).
I got parts 1-2, but could not get 3 to 4.3.
here's ONE of the shell shorts given to us, though this sorts performance is slow because our array size is a power of 2.
template
void shellsort_shell(T* data, int size)
{
int j;
for (int h = size / 2; h > 0; h /= 2)
{
cout << "h = " << h << endl;
for (int i = h; i < size; i++)
{
// insert element i into the slice data[i-h], data[i-2h], ...
T temp = data[i];
for (j = i; j-h >= 0 && temp < data[j-h]; j -= h)
data[j] = data[j-h];
data[j] = temp;
}
}
}
Explanation / Answer
#include
using namespace std;
//function prototypes
void DrawStars(int array[],int size);
void insertionSort(int arr[],int size);
void main()
{
int arr[]= { 2, 4, 2, 1, 7 };
insertionSort(arr,5);
DrawStars(arr,5);
system("pause");
}
void DrawStars(int array[],int size)
{
for(int i=0;i
{
for(int j=0;j
cout<<"*";
cout<
}
}
void insertionSort(int arr[],int size)
{
int first,location;
int temp;
for(first=1;first
if(arr[first]
{
temp=arr[first];
location=first;
do
{
arr[location]=arr[location-1];
location--;
}while(location>0&&arr[location-1]>temp);
arr[location]=temp;
}
}
void
shellSort(int arr[],int n)
{
{
{
temp=arr[i];
{
arr[j] = arr[j-increment];
}
arr[j] = temp;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.