Here you can find the source of quickSortInt(int[] data, int[] dataIdx, int len)
public static void quickSortInt(int[] data, int[] dataIdx, int len)
//package com.java2s; /*/*from w w w . j a va2 s . c o m*/ * Util.java * (FScape) * * Copyright (c) 2001-2015 Hanns Holger Rutz. All rights reserved. * * This software is published under the GNU General Public License v3+ * * * For further information, please contact Hanns Holger Rutz at * contact@sciss.de * * * Changelog: * 17-Jun-07 extended */ public class Main { public static void quickSortInt(int[] data, int[] dataIdx, int len) { int scanGt, scanLt, iterLen = len, k, l = 1; int[] stack = new int[65]; // >= 2 * ld N int stackIdx = 0; int dataElem, dataIdxElem, temp; int kDec, lDec = 0, iterLenDec = iterLen - 1; while (true) { // Insertion sort when subarray small enough. if (iterLen - l < 7) { // M = 7 for (scanGt = l; scanGt < iterLen; scanGt++) { dataElem = data[scanGt]; dataIdxElem = dataIdx[scanGt]; for (scanLt = scanGt - 1; scanLt + 1 >= l; scanLt--) { if (data[scanLt] <= dataElem) break; data[scanLt + 1] = data[scanLt]; dataIdx[scanLt + 1] = dataIdx[scanLt]; } data[scanLt + 1] = dataElem; dataIdx[scanLt + 1] = dataIdxElem; } if (stackIdx == 0) { // free_lvector(stack,1,NSTACK); return; } iterLen = stack[stackIdx]; // Pop stack and begin a new round of partitioning. iterLenDec = iterLen - 1; l = stack[stackIdx - 1]; lDec = l - 1; stackIdx -= 2; } else { // Choose median of left, center and right elements // as partitioning element dataElem. Also // rearrange so that dataElem[l] <= dataElem[l+1] <= dataElem[iterLen]. k = (l + iterLen) >> 1; kDec = k - 1; temp = data[kDec]; // SWAP(data[k],data[l+1]) data[kDec] = data[l]; data[l] = temp; temp = dataIdx[kDec]; // SWAP(dataIdx[k],dataIdx[l+1]) dataIdx[kDec] = dataIdx[l]; dataIdx[l] = temp; if (data[lDec] > data[iterLenDec]) { temp = data[lDec]; // SWAP(data[l],data[iterLen]) data[lDec] = data[iterLenDec]; data[iterLenDec] = temp; temp = dataIdx[lDec]; // SWAP(dataIdx[l],dataIdx[iterLen]) dataIdx[lDec] = dataIdx[iterLenDec]; dataIdx[iterLenDec] = temp; } if (data[l] > data[iterLenDec]) { temp = data[l]; // SWAP(data[l+1],data[iterLen]) data[l] = data[iterLenDec]; data[iterLenDec] = temp; temp = dataIdx[l]; // SWAP(dataIdx[l+1],dataIdx[iterLen]) dataIdx[l] = dataIdx[iterLenDec]; dataIdx[iterLenDec] = temp; } if (data[lDec] > data[l]) { temp = data[lDec]; // SWAP(data[l],data[l+1]) data[lDec] = data[l]; data[l] = temp; temp = dataIdx[lDec]; // SWAP(dataIdx[l],dataIdx[l+1]) dataIdx[lDec] = dataIdx[l]; dataIdx[l] = temp; } scanGt = l; // Initialize pointers for partitioning. scanLt = iterLenDec; dataElem = data[l]; // Partitioning element. dataIdxElem = dataIdx[l]; while (true) { // Beginning of innermost loop. do scanGt++; while (data[scanGt] < dataElem); // Scan up to find element > dataElem. do scanLt--; while (data[scanLt] > dataElem); // Scan down to find element < dataElem. if (scanLt < scanGt) break; // Pointers crossed. Partitioning complete. temp = data[scanGt]; // SWAP(data[scanGt],data[scanLt]) data[scanGt] = data[scanLt]; data[scanLt] = temp; temp = dataIdx[scanGt]; // SWAP(dataIdx[scanGt],dataIdx[scanLt]) dataIdx[scanGt] = dataIdx[scanLt]; dataIdx[scanLt] = temp; } // End of innermost loop. data[l] = data[scanLt]; // Insert partitioning element in both arrays. data[scanLt] = dataElem; dataIdx[l] = dataIdx[scanLt]; dataIdx[scanLt] = dataIdxElem; stackIdx += 2; scanGt++; // Push pointers to larger subarray on stack, process smaller subarray immediately. // if( stackIdx > NSTACK ) nrerror("NSTACK too small in sort2."); if (iterLen - scanGt >= scanLt - l) { stack[stackIdx] = iterLen; stack[stackIdx - 1] = scanGt; iterLen = scanLt; iterLenDec = iterLen - 1; } else { stack[stackIdx] = scanLt; stack[stackIdx - 1] = l; l = scanGt; lDec = l - 1; } } } } }