Sponsored Content
Full Discussion: qsort
Top Forums Programming qsort Post 302082450 by Corona688 on Wednesday 2nd of August 2006 10:14:03 AM
Old 08-02-2006
PLEASE put code in code tags! They are wonderful, magical things that make it possible to read your code without one's eyes spontaneously bleeding.
Code:
void qsort(int v[], int left, int right){
           int i, last;
           void swap(int v[], int i, int j);

           if(left >= right)
                 return;
           swap(v, left, (left + right)/2);
           last = left;
           for(i = left + 1; i <= right; i++)
                   if(v[i] < v[left])
                          swap(v, ++last,i);           swap(v, left, last);
           qsort(v , left, last - 1);
           qsort(v, last - 1, right);
}

Quote:
the highlighted portion does not make sense to me at the first go, b'cos it is trying to swap the same element. i find (i & last ) have the same value when swap is called in the for loop for the first time, plz explain....
Wow! That is an extremely tiny quicksort!

Have you tested it -- does it work? If so, my guess would be that a little redundancy was allowed for the sake of making it smaller.
 

We Also Found This Discussion For You

1. Programming

Qsort Error

I am looking at a small C program that uses qsort to sort an array of strings and an error occurs when it passes qsort a function to compare integers instead of strings. Why can't the compiler catch this? And what would happen if I was using qsort to sort integers and accidentally pass the string... (3 Replies)
Discussion started by: totoro125
3 Replies
COMPARE(3pub)						       C Programmer's Manual						     COMPARE(3pub)

NAME
cmp_set_offset, cmp_struct, cmp_char, cmp_short, cmp_int, cmp_long, cmp_float, cmp_double, cmp_long_double, cmp_schar, cmp_uchar, cmp_ushort, cmp_uint, cmp_ulong, cmp_charptr, cmp_chararr - comparison functions for qsort, bsearch, and others SYNOPSIS
#include <cmp.h> void cmp_set_offset(size_t offset, int (*)(const void *, const void *)); int cmp_struct(const void *, const void *); int cmp_char(const void *, const void *); int cmp_short(const void *, const void *); int cmp_int(const void *, const void *); int cmp_long(const void *, const void *); int cmp_float(const void *, const void *); int cmp_double(const void *, const void *); int cmp_long_double(const void *, const void *); int cmp_schar(const void *, const void *); int cmp_uchar(const void *, const void *); int cmp_ushort(const void *, const void *); int cmp_uint(const void *, const void *); int cmp_ulong(const void *, const void *); int cmp_charptr(const void *, const void *); int cmp_chararr(const void *, const void *); DESCRIPTION
The functions declared above, with the exception of cmp_set_offset, compare two array elements of the indicated type when given pointers to them. The functions are designed to work with qsort(3), bsearch(3), and a number of other library functions which all need the same type of comparison function. They all return negative if the value indicated by the first argument is less than the second, 0 if they are the same, and positive otherwise. cmp_struct compares elements of a structure. It needs to know the offset of the element, and a comparison function suitable for the type of the element. These are set with cmp_set_offset, which should be called before qsort(3). The settings stay in effect until changed. SEE ALSO
publib(3), qsort(3), bsearch(3), isort(3), lsearch(3), lfind(3) AUTHOR
Lars Wirzenius (lars.wirzenius@helsinki.fi) Publib C Programmer's Manual COMPARE(3pub)
All times are GMT -4. The time now is 04:50 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy