From d0b5e27c17a63eaea266f6c19ee8860eba98b3c1 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 14 Nov 2018 18:06:41 -0300 Subject: [PATCH] clean up quick sort source files --- src/quick_sort.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/quick_sort.c b/src/quick_sort.c index aa56d78..95a0274 100644 --- a/src/quick_sort.c +++ b/src/quick_sort.c @@ -13,22 +13,30 @@ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +/** + * Usar el algoritmo de quick sort + * @param array El array a ordenar + * @param n El tamaƱo del array + */ void quick_sort(int *array, int n) { - if(n<2)return; + if(n < 2) { + return; + } int i; int j; int temp; - int pivote=array[n/2]; - for(i=0,j=n-1;;i++,j--){ - while(array[i] < pivote) i++; - while(array[j] > pivote) j--; - if(i>=j)break; - temp=array[i]; + int pivote = array[n / 2]; + for (i =0, j = n - 1;; i++, j--) { + while (array[i] < pivote) i++; + while (array[j] > pivote) j--; + if (i >= j) { + break; + } + temp = array[i]; array[i] = array[j]; - array[j] = temp; + array[j] = temp; } - quick_sort(array,i); - quick_sort(array+i,n-i); + quick_sort(array, i); + quick_sort(array + i, n - i); } -