// /*--------------------------------------------------------------------*\ // | Main Program: What can happen with repeated arguments? // |*--------------------------------------------------------------------*| // | Date: 20 June 2003 // | Author: Bruce W. Weide // | // | Brief User's Manual: // | Allows user to test a simple array sorting algorithm. In // | Sort, there are no repeated argument violations, so this code // | works with any correct implementation of Array_Kernel. // | // \*--------------------------------------------------------------------*/ ///--------------------------------------------------------------------- /// Global Context ----------------------------------------------------- ///--------------------------------------------------------------------- #include "RESOLVE_Foundation.h" #include "CT/Array/Kernel_2_C.h" #include "CT/Array/Are_In_Order_At_1_C.h" #include "CT/Array/Exchange_At_1_C.h" #include "CI/Text/Are_In_Order_1.h" ///--------------------------------------------------------------------- /// Interface ---------------------------------------------------------- ///--------------------------------------------------------------------- concrete_instance class Array_Of_Text_Base : instantiates Array_Kernel_2_C {}; concrete_instance class Array_Of_Text_Are_In_Order : instantiates Array_Are_In_Order_At_1_C < Text, Text_Are_In_Order_1, Array_Of_Text_Base > {}; concrete_instance class Array_Of_Text : instantiates Array_Exchange_At_1_C < Text, Array_Of_Text_Are_In_Order > {}; //---------------------------------------------------------------------- //---------------------------------------------------------------------- // Global operations global_procedure Sort (alters Array_Of_Text& a); /*! ensures [a (thought of as a string) is a permutation of #a, and its entries are sorted in non-decreasing order] !*/ //---------------------------------------------------------------------- //---------------------------------------------------------------------- // Global operation bodies global_procedure_body Sort (alters Array_Of_Text& a) { object Integer i = a.Lower_Bound (); while (i < a.Upper_Bound ()) { object Integer index_of_min = i, j = i + 1; while (j <= a.Upper_Bound ()) { if (a.Are_In_Order_At (j, index_of_min)) { index_of_min = j; } j++; } a.Exchange_At (i, index_of_min); i++; } } //---------------------------------------------------------------------- //---------------------------------------------------------------------- program_body main () { object Character_IStream input; object Character_OStream output; object Integer n_items, i; object Array_Of_Text a; // Open input and output streams input.Open_External (""); output.Open_External (""); // Get number of items to be sorted input >> n_items; // Set size of array a.Set_Bounds (0, n_items - 1); // Get individual items to be sorted into array while (i < n_items) { input >> a[i]; i++; } // Sort array Sort (a); // Output sorted array output << "Sorted Text values are:\n"; i = 0; while (i < n_items) { output << a[i] << "\n"; i++; } // Close input and output streams input.Close_External (); output.Close_External (); }