PolteRGeisT
10-24-2002, 10:41 PM
A textbook explained the concept and I created the algorithm, however, the book's code for the sort was a bit more complicated than mine. Here is mine (including a driver program):
#include <iostream.h>
inline void swap(int &, int &);
void shell_sort(int *, int);
int main(void)
{
int *list;
int i, size;
cout << "Enter the size of the array : ";
cin >> size;
list = new int[size];
cout << "\nEnter the array now: " << endl;
for(i=0; i<size; i++) {
cout << i+1 << ":";
cin >> list[i];
}
shell_sort(list, size);
for(i=0; i<size; i++)
cout << list[i] << endl;
return 0;
}
inline void swap(int &a, int &b)
{
a ^= b;
b ^= a;
a ^= b;
}
void shell_sort(int list[], int size)
{
int gap[5] = { 9, 5, 3, 2, 1 };
register int i, j;
for(i=0; i<5; i++) {
for(j=0; (j + gap[i]) < size; j++)
if(list[j] > list[j + gap[i]])
swap(list[j], list[j + gap[i]]);
}
}
Can anyone tell me if I got it right?
#include <iostream.h>
inline void swap(int &, int &);
void shell_sort(int *, int);
int main(void)
{
int *list;
int i, size;
cout << "Enter the size of the array : ";
cin >> size;
list = new int[size];
cout << "\nEnter the array now: " << endl;
for(i=0; i<size; i++) {
cout << i+1 << ":";
cin >> list[i];
}
shell_sort(list, size);
for(i=0; i<size; i++)
cout << list[i] << endl;
return 0;
}
inline void swap(int &a, int &b)
{
a ^= b;
b ^= a;
a ^= b;
}
void shell_sort(int list[], int size)
{
int gap[5] = { 9, 5, 3, 2, 1 };
register int i, j;
for(i=0; i<5; i++) {
for(j=0; (j + gap[i]) < size; j++)
if(list[j] > list[j + gap[i]])
swap(list[j], list[j + gap[i]]);
}
}
Can anyone tell me if I got it right?