HELP! Desending order

Hello!

I have the following code;

#pragma endregion

// Instance variables
Pen^ blackPen;
Graphics^ g1;
Graphics^ g2;
Brush^ blackBrush;
System::Drawing::Font^ arial8Font;

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
g1 = panel1->CreateGraphics();
g2 = panel2->CreateGraphics();
blackPen = gcnew Pen(Color::Black);
blackBrush = gcnew SolidBrush(Color::Black);
arial8Font = gcnew System::Drawing::Font("Arial",8);
}
private: void DrawArray(int arr[], int n, int panel)
{
// Declare variables
int x = 40; // x coordinate (40 pixels from left edge
int y = 0; // y coordinate (at top of the panel)
int height = 20; // bar height set to 20 pix
int width; // the width of the bar
Graphics^ g;

// Decide which panel to draw on
if (panel == 1)
{
panel1->Refresh();
g = g1;
}
else
{
panel2->Refresh();
g = g2;
}

// loop to draw the bars
for (int i = 0; i < n; i++)
{
y += height;
width = arr[i];
g->DrawString(arr[i].ToString(), arial8Font, blackBrush,x-30,y);
Rectangle bar(x, y, width, height);
g->DrawRectangle(blackPen, bar);
}
}
private: System::Void btnSort_Click(System::Object^ sender, System::EventArgs^ e) {
int data[] = {34,67,21,48,15,92,56,37,71,11};
int n = 10; // the size of the array
int i, j; // loop control variables
int smallIndex; // the index of the smallest value

DrawArray(data, n, 1); // draw unsorted array in panel1

for (i = 0; i < n-1; i++)
{
smallIndex = i;
for (j = i+1; j < n; j++)
if (data[j] < data[smallIndex]) smallIndex = j;
Swap(data[i], data[smallIndex]);
}

DrawArray(data, n, 2); // draw sorted array in panel2
}
// See page 420 Example 9-1
void Swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
};
}


How do I going about changing this code so if will draw in my graph in desending order?? The only seems to show me how to do ascending order.
Last edited on
closed account (zwA4jE8b)
1
2
3
4
5
6
7
for (i = 0; i < n-1; i++)
 {
 smallIndex = i;
 for (j = i+1; j < n; j++)
 if (data[j] < data[smallIndex]) smallIndex = j;
 Swap(data[i], data[smallIndex]);
 }


This portion here seems to sort the data from smallest to largest.
Just change this portion to sort it from largest to smallest.

HINT: It has to do with the '<' symbol. Also renaming a certain variable will be appropriate as well.
Last edited on
for (i = 0; i > j-1; i++)
{
largeIndex = i;
for (i = i+1; i > n; i++)
if (data[i] > data[largeIndex]) largeIndex = i;
Swap(data[j], data[largeIndex]);
}

Does this look right?
closed account (zwA4jE8b)
looks right. did you try it? did you get the results you wanted?
no, the biggest one is one top but they are not in order...I am not sure what I am missing....
Why not just make both loops run for the number of elements in the array? Is it an efficiency problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	int largeIndex = 0;
	int data[10] = { 0,9,3,5,2,1,7,4,8,6 };
	for (int i = 0; i < 10; i++)
	{
		largeIndex = i;
		for (int j = 0; j < 10; j++)
		{
			if (data[j] < data[largeIndex]) 
			{
				largeIndex = j;
				::swap(data[i], data[largeIndex]);
			}
		}
	}
if you want just sort your array why you dont use System::Array::sort() function!!
this is one exm how can do this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
ref class my : public System::Collections::Generic::IComparer<System::Int32> {
public:
	enum class mode {Normal, Reverse};

	my(mode m) : m_m( m )
	{ }

	virtual int Compare(Int32 x, Int32 y)
	{
		if ( m_m == mode::Reverse )
			return y.CompareTo( x );

		return x.CompareTo( y );
	}

private:
	mode m_m;
};

int main(array<System::String ^> ^args)
{	
	array<System::Int32>^ arr = {1, -1, 2, 5, 4};
	
	System::Array::Sort(arr, gcnew my( my::mode::Normal ));

	for each (System::Int32 x in arr)
		Console::Write( x.ToString() + " " );
	
	Console::ReadKey( true );
    return 0;
}
Last edited on
Topic archived. No new replies allowed.