Hello,
I use this algorithm to sort an array "v" in the descending order.
void quickSort(float v[], int lo0, int hi0)
{
int lo = lo0;
int hi = hi0;
if (lo >= hi) return;
float mid = v[(lo + hi) / 2];
while (lo < hi)
{
while (lo<hi && v[lo] > mid) lo++;
while (lo<hi && v[hi] < mid) hi--;
if (lo < hi) swap(v,lo, hi);
}
if (hi < lo) swap(v, hi, lo);
quickSort(v, lo0, lo);
quickSort(v, lo == lo0 ? lo+1 : lo, hi0);
}
I would like to have an array "index" to order another array "z".
Example:
v = [ 5, 9, 7 ]
index = [ 1, 2, 0 ]
z = [ 1, 2, 3 ]
so...
for (i = 0; i<=2; i++)
cout<<z[index[i]];
Output: [ 2, 3, 1 ]
I would like to have index = [ 1, 2, 0 ]. How?
Thanks to everybody.
Luigi Napolitano 7 3731
Luigi Napolitano wrote: v = [ 5, 9, 7 ]
z = [ 1, 2, 3 ]
I would like to have index = [ 1, 2, 0 ]. How?
There's nothing you've written
which suggests an order of {1, 2, 0}.
Do you mean v = [ 7, 9, 5 ] ?
--
pete
pete wrote: Luigi Napolitano wrote:
v = [ 5, 9, 7 ]
z = [ 1, 2, 3 ]
I would like to have index = [ 1, 2, 0 ]. How?
There's nothing you've written which suggests an order of {1, 2, 0}. Do you mean v = [ 7, 9, 5 ] ?
This what I came up with, in case you did:
/* BEGIN order.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if /**/ 0 /*/ 1 /**/
#define E_TYPE int
#define string "%d, "
#else
#define E_TYPE float
#define string "%.0f, "
#endif
#define NELM(X) (sizeof X / sizeof *X)
typedef E_TYPE e_type;
int compar(const void *key, const void *base);
void order(const void *key, void *base, size_t *index,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
int main(void)
{
e_type v[] = {7, 9, 5};
e_type z[] = {1, 2, 3};
e_type buff[NELM(v)];
size_t index[NELM(v)];
size_t i;
order(v, buff, index, NELM(v), sizeof *v, compar);
printf("Output: [ ");
for (i = 0; i != NELM(v); ++i) {
printf(string, z[index[i]]);
}
puts("\b\b ]");
return 0 ;
}
void order(const void *key, void *base, size_t *index,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
char *const b = base;
const char *k = key;
size_t n = nmemb;
memcpy(base, key, nmemb * size);
qsort(base, nmemb, size, compar);
while (nmemb-- != 0) {
*index++ = ((char *)bsearch(k, b, n, size, compar) - b) / size;
k += size;
}
}
int compar(const void *key, const void *base)
{
e_type k = *(e_type *)key;
e_type b = *(e_type *)base;
return b > k ? -1 : k > b;
}
/* END order.c */
--
pete
pete wrote: Luigi Napolitano wrote:
v = [ 5, 9, 7 ]
z = [ 1, 2, 3 ]
I would like to have index = [ 1, 2, 0 ]. How?
There's nothing you've written which suggests an order of {1, 2, 0}. Do you mean v = [ 7, 9, 5 ] ?
I believe the index set represents the element number
of v should it be sorted descending.
One approach would be to create an array of pointers
that point to each element in v. Sort descending the
array of pointers and then calculate offset.
#include <stdio.h>
#include <stdlib.h>
int **CreatePointers(int *array, size_t nelems);
int cmp(const void *v1, const void *v2);
int main(void)
{
int v[] = {5 ,9 ,7};
size_t i , nelems = sizeof v / sizeof *v;
int **index = CreatePointers(v, nelems);
if(index)
{
qsort(index, nelems, sizeof *index, cmp);
printf("index = [ ");
for (i = 0; i < nelems; ++i)
printf(" %d", index[i] - v);
puts(" ]");
free(index);
}
else puts("Unable to create the pointer array");
return 0;
}
int **CreatePointers(int *array, size_t nelems)
{
int **parray;
size_t i;
if((parray = malloc(nelems * sizeof *parray)) != NULL)
for(i = 0; i < nelems; i++) parray[i] = &array[i];
return parray;
}
int cmp(const void *v1, const void *v2)
{
const int *i1 = *(const int **)v1;
const int *i2 = *(const int **)v2;
return *i1>*i2?-1:*i1!=*i2;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email) http://www.geocities.com/abowers822/
pete <pf*****@mindspring.com> writes:
[...] #if /**/ 0 /*/ 1 /**/ #define E_TYPE int #define string "%d, " #else #define E_TYPE float #define string "%.0f, " #endif
Without commenting on the rest of the code, that "#if" hurts my eyes.
I suppose you can change it from
#if /**/ 0 /*/ 1 /**/
to
#if /*/ 0 /**/ 1 /**/
if you want to enable the other block of code, but what's wrong with
#if 0
or
#if 1
?
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson wrote: pete <pf*****@mindspring.com> writes: [...] #if /**/ 0 /*/ 1 /**/
Without commenting on the rest of the code, that "#if" hurts my eyes.
I read Al Bowers' post and stole all of his ideas and rewrote it.
/* BEGIN order.c */
#include <stdio.h>
#include <stdlib.h>
#define E_TYPE float
#define STRING "%.0f, "
#define NELM(X) (sizeof X / sizeof *X)
typedef E_TYPE e_type;
int comparison(const void *key, const void *base);
void order(const void *key, void *base, size_t *index,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
int main(void)
{
e_type v[] = {5, 9, 7};
e_type z[] = {1, 2, 3};
void *buff[NELM(v)];
size_t index[NELM(v)];
size_t i;
order(v, buff, index, NELM(v), sizeof *v, comparison);
printf("Output: [ ");
for (i = 0; i != NELM(v); ++i) {
printf(STRING, z[index[i]]);
}
puts("\b\b ]");
return 0 ;
}
void order(const void *key, void *base, size_t *index,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
const char *k = key;
char const **b = base;
size_t n;
for (n = 0; n != nmemb; ++n) {
b[n] = n * size + k;
}
qsort(base, nmemb, size, compar);
while (n-- != 0) {
*index++ = (*b++ - k) / size;
}
}
int comparison(const void *key, const void *base)
{
const e_type k = **(const e_type **)key;
const e_type b = **(const e_type **)base;
return k > b ? -1 : k != b;
}
/* END order.c */
--
pete
pete wrote: Keith Thompson wrote: pete <pf*****@mindspring.com> writes: [...] #if /**/ 0 /*/ 1 /**/ Without commenting on the rest of the code, that "#if" hurts my eyes.
I read Al Bowers' post and stole all of his ideas and rewrote it.
void *buff[NELM(v)];
order(v, buff, index, NELM(v), sizeof *v, comparison);
void order(const void *key, void *base, size_t *index, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) {
qsort(base, nmemb, size, compar);
That's a bug. Should be "sizeof(void*)" instead of "size"
*index++ = (*b++ - k)
And you never know about ptrdiff_t expressions like (*b++ - k),
so I rewrote it again:
/* BEGIN order.c */
#include <stdio.h>
#include <stdlib.h>
#define E_TYPE long double
#define STRING "%.0lf, "
#define NELM(array) (sizeof array / sizeof array[0])
struct ptr_index {
const void *pointer;
size_t index;
};
typedef E_TYPE e_type;
void order(const void *key, struct ptr_index *base, size_t *index,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
int comparison(const void *key, const void *base);
int main(void)
{
e_type v[] = {5, 9, 7};
e_type z[] = {1, 2, 3};
size_t index[NELM(v)];
size_t i;
struct ptr_index buff[NELM(v)];
order(v, buff, index, NELM(v), sizeof *v, comparison);
printf("Output: [ ");
for (i = 0; i != NELM(v); ++i) {
printf(STRING, z[index[i]]);
}
puts("\b\b ]");
return 0 ;
}
void order(const void *key, struct ptr_index *base, size_t *index,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *))
{
const char *const k = key;
size_t n, diff;
for (n = diff = 0; n != nmemb; ++n) {
base[n].index = n;
base[n].pointer = diff + k;
diff += size;
}
qsort(base, nmemb, sizeof *base, compar);
while (n-- != 0) {
index[n] = base[n].index;
}
}
int comparison(const void *key, const void *base)
{
const e_type k
= *(const e_type *)(((struct ptr_index *)key ) -> pointer);
const e_type b
= *(const e_type *)(((struct ptr_index *)base) -> pointer);
return k > b ? -1 : k != b;
}
/* END order.c */
--
pete
pete wrote:
#define E_TYPE long double #define STRING "%.0lf, "
That should be
#define STRING "%.0Lf, "
--
pete This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ken R. |
last post by:
Hello all,
I am relatively new to python but I am having an issue with custom
sort functions..
I am trying to sort a list of lists or tuples with arbitrary ascending
or descending sorts. For...
|
by: Trevor Best |
last post by:
I have a unique index based on the following columns:
ProjectID (int)
MaterialCatalogID (int)
Material catalogues are pretty much static but projects are dynamic and
people are most likely to...
|
by: Petterson Mikael |
last post by:
Hi,
I have the following package names ( in an xml) that I will transform to
html. I need to sort them.
<package name="se.company.product.subproduct.boam.fpx.testsignals">
<package...
|
by: phony |
last post by:
I have a subform attached to a main form. The linkage is from the main Id
which is also a field in the table from which the subform is built. The table
contains four fields, the main id, a date,...
|
by: Angus Comber |
last post by:
Hello
Here is my code so far. Is this correct/incorrect/along the right
lines/other?
#include <stdio.h>
#include <string.h>
#include <search.h>
struct mystruct
{
|
by: reiks |
last post by:
I have a datatable . My requirement is to sort the table
rows in ascending and descending order basing on the
columns which I give it.
I've used the following expression
...
|
by: M Harvey |
last post by:
I have an arraylist that contains datetime values. What is the best way
to sort this arraylist by date ascending?
Thanks,
Matt
|
by: Alan T |
last post by:
Does the ListView supports sort?
I want to have a up/down arrow/triangle that show it is sorted asc or desc
on the column headers when I click the column header.
May be I need a third-party...
|
by: bcochofel |
last post by:
Hi, I want to use a variable to sort elements. That var his passed with
query string (I'm using Perl CGI to generate XML).
Here's a sample of my output:...
|
by: Chazam |
last post by:
What I'm Trying to do is sort By Date. I came up whith the idea of
splite the date in year, month and day then sort them individualy.
this work perfect in xsl
<xsl:sort order ="descending"...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |