How to invert order in QuickSort


 
Thread Tools Search this Thread
Top Forums Programming How to invert order in QuickSort
# 1  
Old 05-16-2010
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!

Last edited by rafazz; 05-16-2010 at 03:21 PM.. Reason: forget to mention the language
# 2  
Old 05-16-2010
You should be using the qsort library function in stdlib.h If you can't use that then this is probably homework, which means this should be reposted over there in the homework forum.

For qsort there needs to be a comparison function, let's call it compar()
it returns: 0 on equal, +1 when a is larger, -1 when b is larger:
Code:
int compar(const void *a, const void *b)
{
    const int *a1=(const int *)a;
    const int *b1=(const int *)b;
    return *b1 - *a1;
}

This one sorts 99 -> 1, in reverse order.
flip the position of *a1 and *b1 in the subtraction statement to sort 1->99
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Order by 1

select to_char(cre_on, 'ddmm hh24'), proc_flg, count(*) from ib_prd.tb_ibmh_msg_log where message_type = 'DECHECK_OUT' and cre_on > to_date('27/08/2016 00:00:00', 'dd/MM/yyyy hh24:mi:ss') group by to_char(cre_on, 'ddmm hh24'), proc_flg order by 1; 1) what does order by 1 means 2) group by... (1 Reply)
Discussion started by: houmingc
1 Replies

2. UNIX for Dummies Questions & Answers

Increasing order

Hi , I have around 1000000 odd lines in a file in random order. The file looks like this: >string102 >string10437183 >string514 >string10435771 >string10437259 >string1049931 >string1342 I want to arrange it in increasing order: >string102 >string514 >string1342... (3 Replies)
Discussion started by: qwerty193
3 Replies

3. Shell Programming and Scripting

Using grep or something similar to invert search.

Hi guys, I have a script in which it simply do a grep of strings for the var/adm/messages file: NEWDATE=`TZ=GMT+1 date +%b" "%d" "%H` getalarm1=`grep "sent to primary BE" /var/adm/messages* | grep "$NEWDATE" | wc -l` getalarm2=`grep "CC-Request-Type 3 received in state Idle"... (5 Replies)
Discussion started by: matcam79
5 Replies

4. Shell Programming and Scripting

Column not in order

Hi, I was trying to have an output as below : 1 | 2 | 3 aaa | bbb | ccc | ccc | ccc ... (2 Replies)
Discussion started by: scottralf
2 Replies

5. Ubuntu

Boot order?

Hi I have Ubuntu 10.10 installed. I want to change boot order. Can anyone tell where is menu.lst file located. I searched it but didn't get it. Please tell me what file I need to edit for same? (3 Replies)
Discussion started by: nixhead
3 Replies

6. Shell Programming and Scripting

Change order

Good evening I have a file as below and want to change the order, as in the second column, sed awk Pearl Thanks aaaaaaaaaa bbbbbbbbb cccccccc aaaaaaaaaa bbbbbbbbb cccccccc aaaaaaaaaa cccccccc bbbbbbbbb aaaaaaaaaa cccccccc bbbbbbbbb (8 Replies)
Discussion started by: Novice-
8 Replies

7. Shell Programming and Scripting

Invert Matrix of Data - Perl

I have columnar data in arrays perl, Example - @a = (1,2,3); @array1 = (A,B,C); @array2 = (D,E,F); @array3 = (I,R,T); I want the data to be formatted and printed as 1 A D I 2 B E F 3 C F T and so on... (8 Replies)
Discussion started by: dinjo_jo
8 Replies

8. Shell Programming and Scripting

Printing the invert of the last field of awk

in csh set x = "/home/usr/dir1/file1" if i do: echo $x | awk -F\/ '{print $NF}' will result to: "file1" how do i invert the output to: "/home/usr/dir1" :confused: (2 Replies)
Discussion started by: jehrome_rando
2 Replies

9. UNIX for Dummies Questions & Answers

Order sorting

How do you sort text in order using sed? :confused: For example 01 B D A C to 01 ABCD (3 Replies)
Discussion started by: evoGage
3 Replies

10. AIX

Where to Order 5.1L Cds

Anyone know where you can purchase a 5.1 cd set? IBM no longer ships this out and do not have a set. I have a burned copy, but would be nice to have the originals. I'd like to send my copies offsite for DR once i get an original set. Thanks! (2 Replies)
Discussion started by: slacker
2 Replies
Login or Register to Ask a Question