You will invent two sequences of increments for program them into the code shell provided in the attachment. For examples, suppose Shell increments had not been invented, then you could invent them, and your sequence would be for a given sequence length, N,N/2, N/4, N/8 .... 1
You will program these into the functions provided and execute the code and it will produce BIg O graphs. You can use excel or DSWB to view these graphs and determine the Big O. You should try to beat Hibbard (you probably won't beat Sedgewick). In the shell you will see three functions shellsort_Rootsincrements(int data[], int len), shellsort_yyyincrements(int data[], int len), shellsort_zzzincrements(int data[], int len) shellsort_Rootsincrements(int data[], int len)is your model, don't change it. The other two are for you to modify. shellsort_Rootsincrements(int data[], int len) imlements an increment sequence I invented for you. It is not very good, it uses sqrt(N), cube(N), FourthRoot(N) and FivethRoot(N) and 1. Your sequences don't have to use a fixed number of increments, they can depend on N. You will get better results if your increments depend on N.
This is code from the shell
// ***************** YOUR FUNCTION - YOU WRITE THIS (this is a copy of
the shellsort_Rootsincrements that you can modify
***********************************************************
int shellsort_yyyincrements(int data[], int len)
{
int *a;
int ix = 5;
int j=2;// start at the square root (it is the largest increment)
a = new int[ix+1];
a[0] = 1; // last increment must always be 1 else you won't get a
complete sort
// increments must be in descending order (largest to smallest, with 1
being the last increment (stored at location a[0] )
for(int k = ix; k >= 1; k--)
a[k] = (int)pow((double)len, (double)1/(double)(j++));
int counter = insertion_style_sort(data, len, a, ix); // leave this code
in place
delete [] a;
return counter;
}
//
**************************************************************************
**
// ***************** YOUR FUNCTION - YOU WRITE THIS (this is a copy of
the shellsort_Rootsincrements that you can modify
***********************************************************
int shellsort_zzzincrements(int data[], int len)
{
int *a;
int ix = 5;
int j=2;// start at the square root (it is the largest increment)
a = new int[ix+1];
a[0] = 1; // last increment must always be 1 else you won't get a
complete sort
// increments must be in descending order (largest to smallest, with 1
being the last increment (stored at location a[0] )
for(int k = ix; k >= 1; k--)
a[k] = (int)pow((double)len, (double)1/(double)(j++));
int counter = insertion_style_sort(data, len, a, ix);
delete [] a;
return counter;
}
//
**************************************************************************
**
CODE:
#include "stdafx.h"
using namespace std;
#include <stdlib.h>
#include <math.h>
#include <fstream>
void quicksort(int [], int , int, int& );
void swap(int &, int &);
int insertionSort(int [], int, int);
int insertion_style_sort(int data[], int len, int a[], int
limit)
{
int counter=0, gap, i, j, temp;
for(int k = limit; k >= 0; k--)
{
gap =
a[k];
for( i = gap; i
< len; i++)
{
temp = data[i];
counter++;
for( j = i; j >=gap && temp <
data[j - gap]; j-=gap)
{
data[j] = data[ j -
gap];
counter++;
}
data[j] = temp;
}
if(gap ==
1)break;
}
return counter;
}
int insertionSort(int data[], int start, int stop)
{
int count;
int a[2];
a[0] = 1;// create the increments (which are just
1)
a[1] = 1;
count = insertion_style_sort(data, stop - start, a,
1);
return count;
}
int shellsort_Shellincrements(int data[], int len)
{
for(int gap = len/2; gap > 0; gap /= 2)
{
a[j--] = gap;
}
counter = insertion_style_sort(data, len, a,
limit);
return counter;
}
int shellsort_531increments(int data[], int len)
{
int a[]={1, 1, 3, 5};
int counter = insertion_style_sort(data, len, a,
3);
return counter;
}
int shellsort_Hibbardsincrements(int data[], int len)
{
int *a;
double x =
len(long);
x =
log10(x)/log10(2.0);
int ix =
(int)x;
a = new int[ix+1];
for(int k = ix; k
>= 1; k--)
a[k] = (int)pow(2.0, (double)k) - 1;
a[0]= 1;
int counter =
insertion_style_sort(data, len, a, ix);
delete [] a;
return counter;
}
int shellsort_Sedgewicksincrements int data[], int len)
{
int i, j, temp, gap, counter=1;// don't return
zero
double x, y;
i = 0;
sample (len)
while(true)
{
if( (j=(9*(int)pow(4.0, i) -
9*(int)pow(2.0, i) + 1)) > len)
break;
i++;
}
x = i;
i = 0;
while( true)
{
if( (j=((int)pow(4., i) - 3*(int)pow(2., i) + 1)) > len)
break;
i++;
}
y = i;
if(y > x)x = y;
int ix = (int)x;
int *a = new int[2*ix+2];
for(i = 0; i < ix; i++)
{
a[2*i] = (int)(9*(pow(4.0, (double)i) - pow(2.0,
(double)i)) + 1);
a[2*i+1] = (int)(pow(4., (double)i) - 3.*pow(2.,
(double)i) + 1);
if(a[2*i+1] < 1)a[2*i+1] = 1;
}
insertionSort(a, 0, 2*ix-1);
int limit = 2*ix-1;
counter = insertion_style_sort(data, len, a,
2*ix-1);
return counter;
}
void mycopy(int source[], int dest[],
int n)
{
for(int i=0; i < n; i++)dest[i] =
source[i];
}
void gendata(int array[], int length,
int modulus, int base)
{
for(int i=0; i < length; i++)
array[i] = rand()%modulus + base;
}
int mymain()
{
int *data, *save;
int counts [10] [15];
ofstream fpout;
ofstream fpout1;
fpout.open("edit1.txt");
if(fpout.fail())
{
cout << "Data file (data) did not open";
system("pause");
exit(0);
}
int nc = 8;
int np = 15;
fpout << nc << " " << np << endl; //
number of curves (nc) and number of points/curve (np)
fpout << "Sort Increments" << endl;
fpout << "Sample Size(N)" << endl;
fpout << "Operations" << endl;
fpout << "01234567890" << endl;
fpout << "01234567890" << endl;
fpout << "loglog" << endl;
int samples[15] = {8, 16, 32, 48, 64, 96, 128, 256, 512,
1024, 2048, 3072, 4096, 8192, 16384};
for(int i = 0; i < 10; i++)
for(int j=0; j < 15; j++)
counts[i][j] = 1;
}
int N;
for(int i = 0; i < 15; i++)
{
N = samples[i];
data = new int[N];
save = new int[N];
gendata(data, N, RAND_MAX, 0);
mycopy(data, save, N);
counts[0][i] = shellsort_Shellincrements(data, N);
mycopy(save, data, N);
counts[1][i] = shellsort_Hibbardsincrements(data, N);
mycopy(save, data, N);
counts[2][i] = shellsort_Sedgewicksincrements(data, N);
mycopy(save, data, N);
counts[3][i] = shellsort_Rootsincrements(data, N);
mycopy(save, data, N);
counts[4][i] = shellsort_yyyincrements(data, N);
mycopy(save, data, N);
counts[5][i] = shellsort_zzzincrements(data, N);
mycopy(save, data, N);
counts[6][i] = shellsort_531increments(data, N);
mycopy(save, data, N);
counts[7][i] = insertionSort(data, 0, N-1);
mycopy(save, data, N);
delete [] data;
}
for(int i = 0; i < 15; i++)
{
fpout << samples[i] << " ";
}
fpout << endl;
for(int i=0; i < nc; i++)
{
for(int k = 0; k < 15; k++)
{
fpout << counts[i][k] << " ";
}
fpout << endl;
}
fpout.close();
return 0;
You will invent two sequences of increments for program them into the code shell provided in...
Need to code the HeapSort Algorithm in java. Most of the code is already done just need to code the "heapify" part of the heapsort algorithm. My heapify code is in bold below called "sink", but when I run it it gives me 2 errors with the string compare part saying: WordCountHeap.java:61: error: cannot find symbol if(l < k && String.Compare(pq[l], pq[n]) < 0) ^ symbol: method Compare(String,String) location: class String Please fix the code and the "heapify"...
MULTIPLE CHOICE: 1. How would you best describe the purpose of the following code? f = 0; for (i = 1; i < N; ++i) if (a[i] >= a[f]) f = i; a. Rearrange the first N components of array a in descending order. b. Rearrange the first N components of array a in ascending order. c. Place the largest component of array a in position N. d. Compute the value of the largest component in array a. e. Determine...
c++ help Write a program that obtains the execution times for the following three code segments with the different n. code 1: for(int i = 0; i <= n; i++) { x = i; } code 2: for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { x = i + j; } } code 3: for (int i = 0; i <= n; i++) { for (int j =...
Hello, I want to check if my C++ code is correct and follows the
requeriments described thanks.
Requeriments:
Assignment Sorting
Benchmark each of the sorting methods listed below.
Insertion Sort
Bubble Sort
Selection Sort
Heap Sort.
Quick Sort.
Merge Sort.
Benchmark each of the above sorting methods for data sizes of
10000, 20000, 30000, 40000 and 50000. Display the results in a
table as shown below. The table should have rows and columns.
However, the rows and columns need not...
Please program in C++ and document the code as you go so I can understand what you did for example ///This code does~ Your help is super appreciated. Ill make sure to like and review to however the best answer needs. Overview You will revisit the program that you wrote for Assignment 2 and add functionality that you developed in Assignment 3. Some additional functionality will be added to better the reporting of the students’ scores. There will be 11...
Using the following Java program, modify the code so that every time you run your program, it generates random numbers for your array, and then prints it (insertion sort) import java.awt.Graphics; import java.applet.Applet; public class SortingProg extends Applet { int a[] = { 55, 25, 66, 45, 8, 10, 12, 89, 68, 37 }; public void paint(Graphics g) { print(g,"Data items in original order",a,25,25); sort(); print(g,"Data items in ascending order",a,25,55); } public void sort() {...
Below is the code for two programs in C language, I was trying to make proper comments within the code so that someone can understand it. Please review the comments within the code and let me know if it makes good sense. Let me know if I need to make any changes to the comments! Thank you. PROGRAM 1: /* program calculates harmonic mean for 10 numbers entered by the user */ /* if illegal value (x<0) is entered, the...
In the code shown above are
two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM
ON THE SAME CLASS BUT SO WE CAN RECALL them threw different
methods. Thank you.
1-First exercise
import java.util.Scanner;
public class first {
public static int average(int[] array){
int total = 0;
for(int x: array)
total += x;
return total / array.length;
}
public static double average(double[] array){
double total = 0;
for(double x: array)
total += x;
return total /...
PLEASE HELP!!! C PROGRAMMING CODE!!!
Please solve these functions using the prototypes provided. At
the end please include a main function that tests the functions
that will go into a separate driver file.
Prototypes int R_get_int (void); int R_pow void R Jarvis int start); void R_fill_array(int arrayll, int len); void R_prt_array (int arrayl, int len) void R-copy-back (int from[], int to [], int len); int R_count_num (int num, int arrayll, int len) (int base, int ex) 3. Build and run...