Shell Sort w/ fibonacci numbers...HELP!!! Due Friday

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2006
Posts: 4
Reputation: darylesha is an unknown quantity at this point 
Solved Threads: 0
darylesha darylesha is offline Offline
Newbie Poster

Shell Sort w/ fibonacci numbers...HELP!!! Due Friday

 
0
  #1
Apr 5th, 2006
Hey ya'll, I am working on this assignment that has me that is as follows:

/*
Modify the following shell sort to use the fibonacci sequence, instead of
the sequence h = 3h+1.

Hint use dynamic programming to do this.


*/

#include <iostream>
using namespace std;
#include <stdlib.h>
#include <fstream>
#include <string>
#include <time.h>

long comparisons = 0;
long exchanges = 0;
ofstream fout("output.txt");

void ran(int a[], int N)
{
int i;
srand((unsigned)time(NULL)); //Give us a random array that will be fixed for all sorts
for(i = 0; i<N; i++)
a[i] = int(1000*(1.0*rand()/RAND_MAX));
}

void initial(int a[], int b[], int N)
{
int i;
for(i =0 ; i < N; i++) a[i] = b[i];
}

void output( int a[], int l, int r)
{
int i;
for( i = l; i <= r; i++) fout << a[i] << " ";
fout << endl;
}
void shellsort(int a[], int l, int r)
{
int i,j,h, v;
// for(h = 1; h <= (r)/9; h = 3*h+1);
// for( ; h>0; h/=3)
// for(i = l+h; i <= r; i++)
{
j =i;
v = a[i];
while(j >= l+h && v < a[j-h])
{
a[j] = a[j-h];
j -= h;
}
a[j] = v;
}

}
int main(int argc, char *argv[])
{
int i, N = atoi(argv[1]), sw = atoi(argv[2]);
int *a = new int[N], *b = new int[N];
string filename;
int choice = 0;
if(sw)
ran(a, N);
else
{
cout << "enter file name" << endl;
cin >> filename;
ifstream fin(filename.c_str());
for(i = 0; i < N; i++) fin >> a[i];
}
for( i = 0; i < N; i++)
{
fout << a[i] << " ";
}
fout << endl;
shellsort(a, 0 , N-1);
fout << "Shell Sort " << endl;
output(a, 0, N-1);
return 0;
}

Any Help would be greatly appreciated. I know how the fibonacci sequence works, but I do not know how to add it to this program.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Shell Sort w/ fibonacci numbers...HELP!!! Due Friday

 
0
  #2
Apr 5th, 2006
hmmm,

Perhaps this link may answer your question. Not sure though.

http://www.answers.com/topic/shell-sort

  1. /**
  2.  * Performs an insertion sort on elements of a[] with the given gap.
  3.  * If gap == 1, performs an ordinary insertion sort.
  4.  * If gap >= length, does nothing.
  5.  * Based on a C implementation from the article Insertion sort implementations
  6.  */
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. void shellSortPhase(int a[], size_t length, size_t gap) {
  11. int i;
  12.  
  13. for (i = gap; i < length; ++i) {
  14. int value = a[i];
  15. int j;
  16. for (j = i - gap; j >= 0 && a[j] > value; j -= gap) {
  17. a[j + gap] = a[j];
  18. }
  19. a[j + gap] = value;
  20. }
  21. }
  22.  
  23. void shellSort(int a[], size_t length) {
  24. /*
  25.   * gaps[] should approximate a geometric progression.
  26.   * The following gaps are every other Fibonacci number,
  27.   * which gives r=2.616.
  28.   */
  29. static const int gaps[] = {
  30. 1, 2, 5, 13, 34, 89, 233, 610, 1597, 4181, 10946
  31. };
  32. int sizeIndex;
  33.  
  34. for (sizeIndex = sizeof(gaps)/sizeof(gaps[0]) - 1;
  35. sizeIndex >= 0;
  36. --sizeIndex)
  37. shellSortPhase(a, length, gaps[sizeIndex]);
  38. }
  39. int main()
  40. {
  41. int a[] = {7,34,6,3,2,7,8,12,3,5};
  42.  
  43. shellSort(a,10);
  44. for(int i=0; i<10; i++)
  45. {
  46. cout<<a[i]<<" ";
  47. }
  48. cin.get();
  49. return 0;
  50. }
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 4
Reputation: darylesha is an unknown quantity at this point 
Solved Threads: 0
darylesha darylesha is offline Offline
Newbie Poster

Re: Shell Sort w/ fibonacci numbers...HELP!!! Due Friday

 
0
  #3
Apr 5th, 2006
I'm not fully understanding what that program does. If u could try to explain it to me, it would be greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Shell Sort w/ fibonacci numbers...HELP!!! Due Friday

 
0
  #4
Apr 5th, 2006
[edit]****[/edit]
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC