/* ************************************************************************** * Program name : 055_Shell_sort(Version_A) (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/06/30 - first version * ************************************************************************** * Description : (a) Input a list of numbers * * (b) Sort the list by Shell sort * * (c) Try again ? * ************************************************************************** */ #include #include #include void Shellsort(int [], const int ); int main() { // part 1 : declaration const int Arraysize=15; // Maximum no. of element 15 int Number[Arraysize]; // Define the array Number int Index; // loop counter char Again; // Try again ? do { // part 2 : Input 15 real numbers cout << "\n\t\tShell_sort (Version 1.00)\n"; cout << "\nPlease input 15 integer numbers (1 - 99), then this program" << "\nwill printed the list in both unsorted and sorted orders.\n\n"; for (Index=1; Index<=Arraysize; Index++) do { cout << "The #" << Index << " number is : "; cin >> Number[Index-1]; } while ((Number[Index-1]<1) || (Number[Index-1]>99)); // part 3 : List in unsorted order and call the function to sort the list cout << "\n\n Sorting : "; for (Index=0; Index> Again; cout << "\n"; }while (Again=='Y' || Again=='y'); cout << "\n" << endl; system("PAUSE"); return 0; } void Shellsort(int Number[], const int Arraysize) // Note (1) { int Index, Index_1, Index_2; // Loop counters. unsigned int Gap; // Holds the value of gap int Temp; // Holds valus while swaping int Pass = 1; for (Gap = (Arraysize / 2); Gap > 0; Gap = Gap==2 ? 1 : Gap/2.2) // Note (2) { for (Index_1 = Gap; Index_1 < Arraysize; Index_1++) { Temp = Number[Index_1]; for (Index_2 = Index_1; Index_2 >= Gap && Temp < Number[Index_2-Gap]; Index_2-=Gap) Number[Index_2] = Number[Index_2 - Gap]; Number[Index_2] = Temp; } cout << "\n Pass #" << Pass++ << " : "; for (Index=0; Index 0; Gap = Gap==2 ? 1 : Gap/2.2) or for (Gap = (Arraysize / 2); Gap > 0; Gap /= 2) (b) The gap between elements to be compared. After each pass of gap insertion sort is executed, the elements separated by a distance of Gap in the array are sorted. When Gap is 1, the loop is identical to insertion sort. */