/** ********************* * Insertion sort. data[0] does not store a valid data. data[0].key should be * smaller than any valid key. ********************* */ publicvoidinsertionSort() { DataNode tempNode; int j; for (inti=2; i < length; i++) { tempNode = data[i];
// Find the position to insert. // At the same time, move other nodes. for (j = i - 1; data[j].key > tempNode.key; j--) { data[j + 1] = data[j]; } // Of for j
// Insert. data[j + 1] = tempNode;
System.out.println("Round " + (i - 1)); System.out.println(this); } // Of for i }// Of insertionSort
/** ********************* * Shell sort. We do not use sentries here because too many of them are needed. ********************* */ publicvoidshellSort() { DataNode tempNode; int[] tempJumpArray = { 5, 3, 1 }; int tempJump; int p; for (inti=0; i < tempJumpArray.length; i++) { tempJump = tempJumpArray[i]; for (intj=0; j < tempJump; j++) { for (intk= j + tempJump; k < length; k += tempJump) { tempNode = data[k]; // Find the position to insert. // At the same time, move other nodes. for (p = k - tempJump; p >= 0; p -= tempJump) { if (data[p].key > tempNode.key) { data[p + tempJump] = data[p]; } else { break; } // Of if } // Of for p
// Insert. data[p + tempJump] = tempNode; } // Of for k } // Of for j System.out.println("Round " + i); System.out.println(this); } // Of for i }// Of shellSort