Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,046 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,527 people online right now. Registration is fast and FREE... Join Now!




using qsort to sort a 2D array

 
Reply to this topicStart new topic

using qsort to sort a 2D array

ecresna
4 Aug, 2008 - 02:02 AM
Post #1

New D.I.C Head
*

Joined: 4 Aug, 2008
Posts: 1

Hi all.

I'm trying to sort a 2D array by using qsort ANSI C, but is doesn't work .

I have a 4096x4 numbers array (double type) and I want to sort it by columns (sorting each column). At the moment I have this:

///I "copy" first column of 4096x4 array in other double pointer called column that is an array of pointers (a pointer for column)

for (k=0;k<col;k++)
{
for (m = 0; m < fil; m++) // fil=4096 col=4
{
*(column[k]+m) = big_array[m+k*fil];
}
}

// later I use this column pointer into qsort:

qsort (column[0], 10, sizeof(double), compare);

when I check column[0] is disordered.
What is wrong????

Thanks
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: Using Qsort To Sort A 2D Array
4 Aug, 2008 - 04:35 AM
Post #2

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 797



Thanked: 51 times
Dream Kudos: 2175
My Contributions
QUOTE(ecresna @ 4 Aug, 2008 - 11:02 AM) *

Hi all.

I'm trying to sort a 2D array by using qsort ANSI C, but is doesn't work .

I have a 4096x4 numbers array (double type) and I want to sort it by columns (sorting each column). At the moment I have this:

///I "copy" first column of 4096x4 array in other double pointer called column that is an array of pointers (a pointer for column)

for (k=0;k<col;k++)
{
for (m = 0; m < fil; m++) // fil=4096 col=4
{
*(column[k]+m) = big_array[m+k*fil];
}
}

// later I use this column pointer into qsort:

qsort (column[0], 10, sizeof(double), compare);

when I check column[0] is disordered.
What is wrong????

Thanks

This is how I would do it in vb.net translating to C should be to hard.
vb

Dim Tmp(4096) as Double
For col=0 to 3
For q=0 to 4095
Tm( q)=BigArray(col,q)
Next q
QuickSort(Tmp)' I don't know syntax for Quicksort
For q=0 to 4095
BigArray(col,q)=Tmp(q)
next q
Next col


This post has been edited by AdamSpeight2008: 4 Aug, 2008 - 04:37 AM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Using Qsort To Sort A 2D Array
4 Aug, 2008 - 05:16 AM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Using qsort on multi-dimentional arrays is a bit tricky. You seem to be using a flat array that you have logically sectioned into columns, this is probably the easiest way to do it. Here is an example:
cpp
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

int compare (const void * a, const void * b ) {
return ( *(int*)a - *(int*)b );
}

const int Rows = 10;
const int Cols = 4;

int main() {
int array[Cols*Rows];
int i,j, colToSort;
srand(time(NULL));
//Get some random data... Each col should have larger numbers
for (i=0; i < Cols; ++i) {
for (j = 0; j < Rows; ++j) {
array[Rows*i + j] = rand() % (int)pow((double)10, i+1);
}
}
//Randomly select a column to sort
colToSort = rand() % Cols;

//Sort just that collumn...
qsort(&array[colToSort*Rows], Rows, sizeof(int), compare);

//Display results
for (i = 0; i < Cols; i ++) {
//Mark the state of the current column
cout << ((i == colToSort) ? "Sorted: " : "Unsorted: ");
for(j = 0; j < Rows; ++j) {
cout << array[i*Rows + j] << " ";
}
cout << endl;
}
return 0;
}
The program will randomly select a "column" to sort.

This post has been edited by NickDMax: 4 Aug, 2008 - 05:16 AM
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: Using Qsort To Sort A 2D Array
5 Aug, 2008 - 04:54 PM
Post #4

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
QUOTE(ecresna @ 4 Aug, 2008 - 03:02 AM) *


qsort (column[0], 10, sizeof(double), compare);

when I check column[0] is disordered.
What is wrong????



Even the first 10 numbers? You're only sorting the first 10 numbers. Other than that, what you're doing seems to be correct, provided that you intend to sort each column independently. Make sure your compare function is functioning correctly.

This post has been edited by perfectly.insane: 5 Aug, 2008 - 04:56 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 05:05PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month