Task: Sorting algorithms_Shell sort


C++: #include <time.h>
#include <iostream>


using namespace std;


const int MAX = 126;
class shell
{
public:
    shell() 
    { _gap[0] = 1750; _gap[1] = 701; _gap[2] = 301; _gap[3] = 132; _gap[4] = 57; _gap[5] = 23; _gap[6] = 10; _gap[7] = 4; _gap[8] = 1; }

    void sort( int* a, int count )
    {
	_cnt = count;
	for( int x = 0; x < 9; x++ )
	    if( count > _gap[x] )
	    { _idx = x; break; }

	sortIt( a );
    }

private:	
    void sortIt( int* arr )
    {
	bool sorted = false;
	while( true )
	{
	    sorted = true;
	    int st = 0;
	    for( int x = _gap[_idx]; x < _cnt; x += _gap[_idx] )
	    {
		if( arr[st] > arr[x] )
		{ swap( arr[st], arr[x] ); sorted = false; }
		st = x;
	    }
	    if( ++_idx >= 8 ) _idx = 8;
	    if( sorted && _idx == 8 ) break;
	}
    }

    void swap( int& a, int& b ) { int t = a; a = b; b = t; }

    int _gap[9], _idx, _cnt;
};

int main( int argc, char* argv[] )
{
    srand( static_cast<unsigned int>( time( NULL ) ) ); int arr[MAX];
    for( int x = 0; x < MAX; x++ )
	arr[x] = rand() % MAX - rand() % MAX;

    cout << " Before: \n=========\n";
    for( int x = 0; x < 7; x++ )
    {
	for( int a = 0; a < 18; a++ )
	{ cout << arr[x * 18 + a] << " "; }
	cout << endl;
    }
    cout << endl; shell s; s.sort( arr, MAX );
	
    cout << " After: \n========\n";
    for( int x = 0; x < 7; x++ )
    {
	for( int a = 0; a < 18; a++ )
	{ cout << arr[x * 18 + a] << " "; }
	cout << endl;
    }
    cout << endl << endl; return system( "pause" );
}









COBOL:       *******************************************************           
       IDENTIFICATION DIVISION.                                         
      *******************************************************           
       PROGRAM-ID.      SHELLSRT.                                       
      ************************************************************      
      *** SHELLSORT                                           ****      
      ************************************************************      
       ENVIRONMENT DIVISION.                                            
       DATA DIVISION.                                                   
       WORKING-STORAGE SECTION.                                         
       01 II                        PIC S9(008) COMP-5.                 
       01 IJ                        PIC S9(008) COMP-5.                 
       01 IZ                        PIC S9(008) COMP-5.                 
       01 IA                        PIC S9(008) COMP-5.                 
       01 STRT1                     PIC S9(008) COMP-5.                 
       01 STRT2                     PIC S9(008) COMP-5.                 
       01 LGT                       PIC S9(008) COMP-5.                 
       01 ORG                       PIC S9(008) COMP-5.                 
       01 DST                       PIC S9(008) COMP-5.                 
      *                                                                 
       01 GAP                       PIC S9(008) COMP-5.                 
       01 NEGAP                     PIC S9(008) COMP-5.                 
       01 TEMP                      PIC X(32768).                       
       77 KEY-RESULT                PIC X.                              
      *                                                                 
       LINKAGE SECTION.                                                 
       01 SRT-ARRAY                 PIC  X(1000000).                    
       01 NUM-ITEM                  PIC  9(008) COMP-5.                 
       01 SRT-DATA.                                                     
          03 LGT-ITEM               PIC  9(004) COMP-5.                 
          03 SRT-KEYS.                                                  
             05 SRT-KEY OCCURS 10.                                      
                07 K-START         PIC S9(004) COMP-5.                  
                07 K-LENGTH        PIC S9(004) COMP-5.                  
                07 K-ASC           PIC X.                               
      *                                                                 
      *    P R O C E D U R E      D I V I S I O N                       
      *                                                                 
       PROCEDURE DIVISION USING SRT-ARRAY NUM-ITEM SRT-DATA.                
                                                                        
           COMPUTE GAP = NUM-ITEM / 2.                                  
           PERFORM UNTIL GAP < 1                                        
              COMPUTE NEGAP = GAP * -1                                  
              PERFORM VARYING II FROM GAP BY 1                          
                        UNTIL II GREATER  NUM-ITEM                      
                 MOVE ' ' TO KEY-RESULT                                 
                 COMPUTE ORG = (II - 1) * LGT-ITEM + 1                  
                 MOVE SRT-ARRAY(ORG:LGT-ITEM) TO TEMP(1:LGT-ITEM)       
                 PERFORM VARYING IJ FROM II BY NEGAP                    
                           UNTIL IJ NOT GREATER  GAP                    
                              OR (KEY-RESULT NOT EQUAL '<' AND ' ')     
                    COMPUTE IA = IJ - GAP                               
                    IF IA < 1                                           
                       MOVE 1 TO IA                                     
                    END-IF                                              
                    PERFORM COMPARE-KEYS                                
                    IF KEY-RESULT = '<'                                 
                       COMPUTE ORG = (IA - 1) * LGT-ITEM + 1            
                       COMPUTE DST = (IJ - 1) * LGT-ITEM + 1            
                       MOVE SRT-ARRAY(ORG:LGT-ITEM)                     
                         TO SRT-ARRAY(DST:LGT-ITEM)                     
                       COMPUTE DST = (IA - 1) * LGT-ITEM + 1            
                       MOVE TEMP(1:LGT-ITEM) TO SRT-ARRAY(DST:LGT-ITEM) 
                    END-IF                                              
                 END-PERFORM                                            
              END-PERFORM                                               
              IF GAP = 2                                                
                 MOVE 1 TO GAP                                          
              ELSE                                                      
                 COMPUTE GAP = GAP / 2.2                                
              END-IF                                                    
           END-PERFORM.                                                 
           GOBACK.                                                      
      *                                                                 
       COMPARE-KEYS.                                                    
           MOVE ' ' TO KEY-RESULT                                       
           PERFORM VARYING IZ FROM 1 BY 1                               
                     UNTIL IZ GREATER 10                                
                        OR (KEY-RESULT NOT EQUAL '=' AND ' ')           
              IF SRT-KEY(IZ) GREATER LOW-VALUES                         
                 COMPUTE STRT1 = (IJ - 1) * LGT-ITEM + K-START(IZ)      
                 COMPUTE STRT2 = (IA - 1) * LGT-ITEM + K-START(IZ)      
                 MOVE K-LENGTH(IZ) TO LGT                               
                 IF SRT-ARRAY(STRT1:LGT) > SRT-ARRAY(STRT2:LGT) AND     
                    K-ASC(IZ) EQUAL 'A'                                 
                 OR SRT-ARRAY(STRT1:LGT) < SRT-ARRAY(STRT2:LGT) AND     
                    K-ASC(IZ) EQUAL 'D'                                 
                    MOVE '>' TO KEY-RESULT                              
                 END-IF                                                 
                 IF SRT-ARRAY(STRT1:LGT) < SRT-ARRAY(STRT2:LGT) AND     
                    K-ASC(IZ) EQUAL 'A'                                 
                 OR SRT-ARRAY(STRT1:LGT) > SRT-ARRAY(STRT2:LGT) AND     
                    K-ASC(IZ) EQUAL 'D'                                 
                    MOVE '<' TO KEY-RESULT                              
                 END-IF                                                 
              END-IF                                                    
           END-PERFORM.                                                 
           IF KEY-RESULT = ' '                                          
              MOVE '=' TO KEY-RESULT                                    
           END-IF.