Browse Source

Merge branch 'cleanquicksort' of UBB/sort into master

tags/1.0.0
Chris Cromer 11 months ago
parent
commit
0f6c3f3b53
1 changed files with 19 additions and 11 deletions
  1. 19
    11
      src/quick_sort.c

+ 19
- 11
src/quick_sort.c View File

@@ -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);
}


Loading…
Cancel
Save