Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changes between Version 4 and Version 5 of BubbleSort

Show
Ignore:
Author:
garth (IP: 165.72.200.11)
Timestamp:
02/14/07 15:51:43 (17 years ago)
Comment:

Using foreach loop as proposed by last update

Legend:

Unmodified
Added
Removed
Modified
  • BubbleSort

    v4 v5  
    1010 
    1111/** 
    12 *      BubbleSort implementation in D 
     12*   BubbleSort implementation in D 
    1313**/ 
    1414class BubbleSort { 
    15          
    16         int[]   tobsorted; 
    17         public: 
    18          
    19         /** 
    20         * Constructor 
    21         **/ 
    22         this(int[] array) { 
    23                 tobsorted                       = array; 
    24         } 
    25          
    26         int[] sort() { 
    27                 // initialize variables 
    28                 // x+y are used to iterate through the array using a for-loop 
    29                 int     x, y, temp; 
    3015 
    31                 // Iterate through every element in array 
    32                 for(x = 0; x < tobsorted.length; x++) { 
     16    int[]   tobsorted; 
     17    public: 
    3318 
    34                         // 2nd iteration through array, comparing elements with each other 
    35                         // y may not be equal or bigger than array.length (throws exception) 
    36                          
    37                         for(y=0; y < (tobsorted.length -1); y++) { 
     19    /** 
     20    * Constructor 
     21    **/ 
     22    this(int[] array) { 
     23        tobsorted           = array; 
     24    } 
    3825 
    39                                 // comare elements to each other 
    40                                 // If element y < x 
    41                                 // swap elements 
    42                                 if(tobsorted[x] < tobsorted[y]) { 
     26    int[] sort() { 
    4327 
    44                                         temp            = tobsorted[x]; 
    45                                         tobsorted[x]    = tobsorted[y]; 
    46                                         tobsorted[y]    = temp; 
    47                                 } 
    48                         } 
     28        // Iterate through every element in array 
     29        foreach( inout int x; tobsorted) { 
    4930 
    50                         // Output current array to console 
    51                         foreach(int i; tobsorted) { 
    52                                 writef("%02d ",i); 
    53                         } 
     31            // 2nd iteration through array, comparing elements with each other 
     32            foreach(inout int y; tobsorted) { 
    5433 
    55                         // append newline 
    56                         writefln(); 
    57                 } 
     34                // comare elements to each other 
     35                // If element x < y 
     36                // swap elements 
     37                if(x < y) { 
     38                    debug writefln("swap(%d,%d)", x,y); 
     39                    typeof(x) temp = x; 
     40                                        x = y; 
     41                    y    = temp; 
     42                } 
     43            } 
    5844 
    59                 // return sorted array 
    60                 return tobsorted; 
    61         } 
     45            // Output current array to console 
     46            foreach(int i; tobsorted) { 
     47                writef("%02d ",i); 
     48            } 
     49 
     50            // append newline 
     51            writefln(); 
     52        } 
     53 
     54        // return sorted array 
     55        return tobsorted; 
     56    } 
    6257} 
    63  
    6458 
    6559/** Explains itself 
    6660*   Initialize int-array with 15 elements 
    67 *      Then calls BubbleSort and outputs the sorted array       
    68 **/  
     61*   Then calls BubbleSort and outputs the sorted array 
     62**/ 
    6963int main(char[][] args) { 
    70          
    71         // initialize array 
    7264 
    73         static int[]    array   = [15,4,3,14,2,5,1,8,7,19,15,13,9,12,6,11]; 
     65    // initialize array 
    7466 
    75         writefln("BubbleSort in D"); 
    76         writefln("---------------"); 
    77         writef("Sorting following numbers:\t"); 
    78         foreach(int i; array) { 
    79                 writef("%02d ",i); 
    80         } 
    81         writefln("\nStarting now..."); 
    82         writefln(); 
     67    static int[]    array   = [15,4,3,14,5,2,1,8,7,19,15,13,9,12,6,11]; 
    8368 
    84         BubbleSort      s       = new BubbleSort(array); 
    85         array                   = s.sort(); 
     69    writefln("BubbleSort in D"); 
     70    writefln("---------------"); 
     71    writef("Sorting following numbers:\t"); 
     72    foreach(int i; array) { 
     73        writef("%02d ",i); 
     74    } 
     75    writefln("\nStarting now..."); 
     76    writefln(); 
    8677 
    87         writefln(); 
    88         writefln("I finished!"); 
    89         writef("Here's your sorted array:\t"); 
    90         foreach(int i; array) { 
    91                 writef("%02d ",i); 
    92         } 
     78    BubbleSort  s   = new BubbleSort(array); 
     79    array           = s.sort(); 
    9380 
    94         return 0; 
     81    writefln(); 
     82    writefln("I finished!"); 
     83    writef("Here's your sorted array:\t"); 
     84    foreach(int i; array) { 
     85        writef("%02d ",i); 
     86    } 
     87 
     88    return 0; 
    9589} 
     90 
    9691}}}