Thread: How to invert order in QuickSort

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    1

    How to invert order in QuickSort

    Hello! Iam trying to reverse my order of quicksort in C from asc order (1-99) that is the original way of the algoritm, to descending (99-1). I've tried so many ways that iam a kind of lost now!:s

    The original quicksort is:
    Code:
    void swap(int* a, int* B) {
      int tmp;
      tmp = *a;
      *a = *b;
      *b = tmp;
    }
     
    int partition(int vec[], int left, int right) {
      int i, j;
     
      i = left;
      for (j = left + 1; j <= right; ++j) {
        if (vec[j] < vec[left]) {
          ++i;
          swap(&vec[i], &vec[j]);
        }
      }
      swap(&vec[left], &vec[i]);
     
      return i;
    }
     
    void quickSort(int vec[], int left, int right) {
      int r;
     
      if (right > left) {
        r = partition(vec, left, right);
        quickSort(vec, left, r - 1);
        quickSort(vec, r + 1, right);
      }
    }
    Thanks for all! Good work!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > if (vec[j] < vec[left])
    What does this do?

    More importantly, WHY is it < ?

    When you know that, you should know the answer to your question.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scalability problems with Quicksort
    By hbejkosa in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2008, 11:26 PM
  2. Laplace Expansion
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 11:26 PM
  3. Embedded SQL Order By
    By cjohnman in forum C Programming
    Replies: 12
    Last Post: 04-15-2008, 03:45 PM
  4. How do you order your game code?
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 02-05-2006, 06:26 PM
  5. what is the significance of low order and high order bits
    By Shadow12345 in forum Windows Programming
    Replies: 1
    Last Post: 11-16-2002, 11:46 AM