1 17 package org.alfresco.web.data; 18 19 import java.util.List ; 20 21 31 public final class QuickSort extends Sort 32 { 33 41 public QuickSort(List data, String column, boolean bForward, String mode) 42 { 43 super(data, column, bForward, mode); 44 } 45 46 47 50 53 public void sort() 54 { 55 if (this.data.size() != 0) 56 { 57 qsort(this.data, 0, this.data.size() - 1); 58 } 59 } 60 61 62 65 72 private void qsort(final List v, final int lower, final int upper) 73 { 74 int sliceLength = upper - lower + 1 ; 75 if (sliceLength > 1) 76 { 77 if (sliceLength < 7) 78 { 79 for (int i=lower; i<=upper; i++) 81 { 82 if (this.bForward == true) 83 { 84 for (int j=i; j > lower && getComparator().compare(this.keys.get(j - 1), this.keys.get(j)) > 0; j--) 85 { 86 swap(this.keys, j - 1, j); 88 swap(v, j - 1, j); 89 } 90 } 91 else 92 { 93 for (int j=i; j > lower && getComparator().compare(this.keys.get(j - 1), this.keys.get(j)) < 0; j--) 94 { 95 swap(this.keys, j - 1, j); 97 swap(v, j - 1, j); 98 } 99 } 100 } 101 } 102 else 103 { 104 int pivotIndex = partition(v, lower, upper); 105 qsort(v, lower, pivotIndex); 106 qsort(v, pivotIndex + 1, upper); 107 } 108 } 109 } 110 111 127 private int partition(final List v, int lower, int upper) 128 { 129 List keys = this.keys; 130 Object pivotValue = keys.get((upper + lower + 1) >> 1) ; 131 132 int size = keys.size(); 133 134 while (lower <= upper) 135 { 136 if (this.bForward == true) 137 { 138 while (getComparator().compare(keys.get(lower), pivotValue) < 0) 139 { 140 lower++; 141 } 142 while (getComparator().compare(pivotValue, keys.get(upper)) < 0) 143 { 144 upper--; 145 } 146 } 147 else 148 { 149 while (getComparator().compare(keys.get(lower), pivotValue) > 0) 150 { 151 lower++; 152 } 153 while (getComparator().compare(pivotValue, keys.get(upper)) > 0) 154 { 155 upper--; 156 } 157 } 158 if (lower <= upper) 159 { 160 if (lower < upper) 161 { 162 swap(keys, lower, upper); 163 swap(v, lower, upper); 164 } 165 lower++; 166 upper--; 167 } 168 } 169 170 return upper; 171 } 172 173 } | Popular Tags |