Changes between Version 4 and Version 5 of BubbleSort
- Timestamp:
- 02/14/07 15:51:43 (17 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
BubbleSort
v4 v5 10 10 11 11 /** 12 * 12 * BubbleSort implementation in D 13 13 **/ 14 14 class BubbleSort { 15 16 int[] tobsorted;17 public:18 19 /**20 * Constructor21 **/22 this(int[] array) {23 tobsorted = array;24 }25 26 int[] sort() {27 // initialize variables28 // x+y are used to iterate through the array using a for-loop29 int x, y, temp;30 15 31 // Iterate through every element in array 32 for(x = 0; x < tobsorted.length; x++) { 16 int[] tobsorted; 17 public: 33 18 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 } 38 25 39 // comare elements to each other 40 // If element y < x 41 // swap elements 42 if(tobsorted[x] < tobsorted[y]) { 26 int[] sort() { 43 27 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) { 49 30 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) { 54 33 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 } 58 44 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 } 62 57 } 63 64 58 65 59 /** Explains itself 66 60 * Initialize int-array with 15 elements 67 * Then calls BubbleSort and outputs the sorted array68 **/ 61 * Then calls BubbleSort and outputs the sorted array 62 **/ 69 63 int main(char[][] args) { 70 71 // initialize array72 64 73 static int[] array = [15,4,3,14,2,5,1,8,7,19,15,13,9,12,6,11]; 65 // initialize array 74 66 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]; 83 68 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(); 86 77 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(); 93 80 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; 95 89 } 90 96 91 }}}