Quicksort.cpp
Go to the documentation of this file.
00001 // ****************************************************************************
00002 // This file is part of the Integrating Vision Toolkit (IVT).
00003 //
00004 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
00005 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
00006 //
00007 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
00008 // All rights reserved.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are met:
00012 //
00013 // 1. Redistributions of source code must retain the above copyright
00014 //    notice, this list of conditions and the following disclaimer.
00015 //
00016 // 2. Redistributions in binary form must reproduce the above copyright
00017 //    notice, this list of conditions and the following disclaimer in the
00018 //    documentation and/or other materials provided with the distribution.
00019 //
00020 // 3. Neither the name of the KIT nor the names of its contributors may be
00021 //    used to endorse or promote products derived from this software
00022 //    without specific prior written permission.
00023 //
00024 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
00025 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
00028 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00033 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 // ****************************************************************************
00035 // ****************************************************************************
00036 // Filename:  Quicksort.cpp
00037 // Author:    Kai Welke
00038 // Date:      2005
00039 // ****************************************************************************
00040 
00041 
00042 // ****************************************************************************
00043 // Includes
00044 // ****************************************************************************
00045 
00046 #include <new> // for explicitly using correct new/delete operators on VC DSPs
00047 
00048 #include "Quicksort.h"
00049 
00050 
00051 
00052 // ****************************************************************************
00053 // Functions
00054 // ****************************************************************************
00055 
00056 // quicksort elements of float array by exchanging the elements
00057 void Quicksort::Quicksort(float *pValues, int nLow, int nHigh)
00058 {
00059         int i = nLow;
00060         int j = nHigh;
00061         
00062         const float fX = pValues[(nLow + nHigh) / 2];
00063 
00064         while (i <= j)
00065         {    
00066                 while (pValues[i] < fX) i++; 
00067                 while (pValues[j] > fX) j--;
00068         
00069                 if (i <= j)
00070                 {
00071                         float fTemp = pValues[i];
00072                         pValues[i] = pValues[j];
00073                         pValues[j] = fTemp;
00074                 
00075                         i++; 
00076                         j--;
00077                 }
00078         }
00079 
00080         if (nLow < j) Quicksort(pValues, nLow, j);
00081         if (i < nHigh) Quicksort(pValues, i, nHigh);
00082 }
00083 
00084 
00085 // quicksort elements of float array in inverse order by exchanging the elements
00086 void Quicksort::QuicksortInverse(float *pValues, int nLow, int nHigh)
00087 {
00088         int i = nLow;
00089         int j = nHigh;
00090         
00091         const float fX = pValues[(nLow + nHigh) / 2];
00092 
00093         while (i <= j)
00094         {    
00095                 while (pValues[i] > fX) i++; 
00096                 while (pValues[j] < fX) j--;
00097         
00098                 if (i <= j)
00099                 {
00100                         float fTemp = pValues[i];
00101                         pValues[i] = pValues[j];
00102                         pValues[j] = fTemp;
00103                 
00104                         i++; 
00105                         j--;
00106                 }
00107         }
00108 
00109         if (nLow < j) QuicksortInverse(pValues, nLow, j);
00110         if (i < nHigh) QuicksortInverse(pValues, i, nHigh);
00111 }
00112 
00113 
00114 void Quicksort::QuicksortWithMeta(float *pValues, void **ppMeta, int nLow, int nHigh)
00115 {
00116         int i = nLow;
00117         int j = nHigh;
00118                 
00119         const float fX = pValues[(nLow + nHigh) / 2];
00120 
00121         while (i <= j)
00122         {    
00123                 while (pValues[i] < fX) i++; 
00124                 while (pValues[j] > fX) j--;
00125             
00126                 if (i <= j)
00127                 {
00128                         const float fTemp = pValues[i];
00129                         pValues[i] = pValues[j];
00130                         pValues[j] = fTemp;
00131                 
00132                         void *pMetaTemp = ppMeta[i];
00133                         ppMeta[i] = ppMeta[j];
00134                         ppMeta[j] = pMetaTemp;
00135                 
00136                         i++;
00137                         j--;
00138                 }
00139         }
00140 
00141         if (nLow < j) QuicksortWithMeta(pValues, ppMeta,  nLow, j);
00142         if (i < nHigh) QuicksortWithMeta(pValues, ppMeta,  i, nHigh);
00143 }
00144 
00145 void Quicksort::QuicksortInverseWithMeta(float *pValues, void **ppMeta, int nLow, int nHigh)
00146 {
00147         int i = nLow;
00148         int j = nHigh;
00149         
00150         const float fX = pValues[(nLow + nHigh) / 2];
00151 
00152         while (i <= j)
00153         {    
00154                 while (pValues[i] > fX) i++; 
00155                 while (pValues[j] < fX) j--;
00156         
00157                 if (i <= j)
00158                 {
00159                         float fTemp = pValues[i];
00160                         pValues[i] = pValues[j];
00161                         pValues[j] = fTemp;
00162                         
00163                         void *pMetaTemp = ppMeta[i];
00164                         ppMeta[i] = ppMeta[j];
00165                         ppMeta[j] = pMetaTemp;
00166                 
00167                         i++; 
00168                         j--;
00169                 }
00170         }
00171 
00172         if (nLow < j) QuicksortInverseWithMeta(pValues, ppMeta, nLow, j);
00173         if (i < nHigh) QuicksortInverseWithMeta(pValues, ppMeta, i, nHigh);
00174 }
00175 
00176 
00177 // quicksort vectors (float* in an array) due to the size of the size of the nSortByDimension'th element
00178 // by exchanging the pointers
00179 void Quicksort::QuicksortByElementOfVector(float **ppValues, int nLow, int nHigh, int nSortByDimension)
00180 {
00181         int i = nLow;
00182         int j = nHigh;
00183         
00184         const float fX = ppValues[(nLow + nHigh) / 2][nSortByDimension];
00185 
00186         while ( i <= j )
00187         {    
00188                 while (ppValues[i][nSortByDimension] < fX) i++; 
00189                 while (ppValues[j][nSortByDimension] > fX) j--;
00190             
00191                 if (i <= j)
00192                 {
00193                         float *pfTemp = ppValues[i];
00194                         ppValues[i]=ppValues[j];
00195                         ppValues[j]=pfTemp;
00196                 
00197                         i++; 
00198                         j--;
00199                 }
00200         }
00201 
00202         if (nLow < j) QuicksortByElementOfVector(ppValues, nLow, j, nSortByDimension);
00203         if (i < nHigh) QuicksortByElementOfVector(ppValues, i, nHigh, nSortByDimension);
00204 }


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Thu Jun 6 2019 21:46:58