Hi, so i have some code that I have. i am trying to take an insertion sort and t
ID: 3668374 • Letter: H
Question
Hi, so i have some code that I have. i am trying to take an insertion sort and then use H[] and hSize to sort it by whatever I call. So, for example if H = 5,3,1 == then it will sort it every fifth, every 3rd, and every one. So a sample would be :
Input = [2, 5, 6, 4, 10, 9, 8, 1, 10, 5] and H = [5, 3,1]
Output = [1, 2, 4, 5, 5, 6, 8, 9, 10, 10]
my code so far:
/*
* coolsort.cpp
*
*/
#include <iostream>
using namespace std;
void CoolSort(int [], int , int [], int);
void CoolSort(int H [] ,int hSize, int array[], int size)
{
//insertionsort(H , size);
for (int hIdx = 0; hIdx< hSize; hIdx++)
{
//
for (int arryIdx= 0; arryIdx<size; arryIdx++)
{
int i, j, temp;
for ( i = 1; i<size; i+=H[i])// maybe i = array[i]
{
j = i;
while ( j>0 && array[j-1] > array[j])//
{
temp = array[j];
array[j]= array[j-1];
array[j-1] = temp;
j--;
}
// use h to loop again through the big array to create
// the smaller array and pass smaller array to the insertion
// sort
}
}
}
}
// if get -'s means going over the array size. or if i am getting weird outputs.
int main()
{
const int size = 12;
int H= {5,3,1};
int hSize;
int array[size] = {62,83,18,53,7,17,95,86,47,69,25,28};
CoolSort(H, hSize, array, size);
Explanation / Answer
#include <iostream>
using namespace std;
void CoolSort(int [], int , int [], int);
void CoolSort(int H [] ,int hSize, int array[], int size)
{
//insertionsort(H , size);
for (int hIdx = 0; hIdx< hSize; hIdx++)
{
for (int arryIdx= 0; arryIdx<size; arryIdx++)
{
int i, j, temp;
for ( i = 1; i<size; i+=H[i])// maybe i = array[i]
{
j = i;
while ( j>0 && array[j-1] > array[j])//
{
temp = array[j];
array[j]= array[j-1];
array[j-1] = temp;
j--;
}
// use h to loop again through the big array to create
// the smaller array and pass smaller array to the insertion
// sort
}
}
}
}
// if get -'s means going over the array size. or if i am getting weird outputs.
int main()
{
int i=0;
const int size = 12;
int H[]= {5,3,1};
int hSize;
int array[size] = {62,83,18,53,7,17,95,86,47,69,25,28};
CoolSort(H, hSize, array, size);
for(i=0;i<size;i++)
{
cout<<array[i]<<" ";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.