TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Željko Perić
415
3.8k
221.3k
Multidimensional array sort code optimization
Oct 5 2018 2:11 PM
Hello,
I have created this simple program for sorting multidimensional Array :
using
System;
namespace
Multidimensional_array_sort
{
class
Program
{
public
static
void
Main(
string
[] args)
{
//
Console.WriteLine();
Console.WriteLine(
" Hello World!"
);
Console.WriteLine();
//
int
[,] Array;
//
Console.Write(
" Enter number of elements : n = "
);
//
int
n =
int
.Parse(Console.ReadLine());
//
Array =
new
int
[n,4];
//
Console.WriteLine();
Console.WriteLine(
" Element data entry :"
);
Console.WriteLine(
" *********************"
);
Console.WriteLine();
//
for
(
int
i=
0
; i<n; i++)
{
Array[i,0]=i+1;
Console.WriteLine(
" "
+(i+1).ToString() +
". element :"
);
Console.WriteLine(
" *********************"
);
Console.Write(
" length : "
);
Array[i,1]=
int
.Parse(Console.ReadLine());
Console.Write(
" width : "
);
Array[i,2]=
int
.Parse(Console.ReadLine());
Array[i,3]=Array[i,1]*Array[i,2];
Console.WriteLine(
" *********************"
);
Console.WriteLine();
}
//
for
(
int
k=
0
; k<n-
1
; k++)
{
for
(
int
j=k+
1
; j<n; j++)
{
if
(Array[k,3]>Array[j,3])
goto
next;
if
(Array[k,3]
goto
swap;
if
(Array[k,1]>Array[j,1])
goto
next;
if
(Array[k,1]
goto
swap;
if
(Array[k,2]>Array[j,2])
goto
next;
if
(Array[k,2]
goto
swap;
if
(Array[k,0]
goto
next;
if
(Array[k,0]>Array[j,0])
goto
swap;
swap:
{
int
m = Array[j,0];
Array[j,0]=Array[k,0];
Array[k,0]=m;
m = Array[j,1];
Array[j,1]=Array[k,1];
Array[k,1]=m;
m = Array[j,2];
Array[j,2]=Array[k,2];
Array[k,2]=m;
m = Array[j,3];
Array[j,3]=Array[k,3];
Array[k,3]=m;
}
next:
{
}
}
}
//
// Show sorted array
// element index, element entry index, value a, value b, value c
//
Console.WriteLine();
Console.WriteLine(
" Sorted array :"
);
Console.WriteLine(
" *******************************"
);
for
(
int
i=
0
; i<n; i++)
{
Console.WriteLine(
"{0,3}{1,3}{2,5}{3,5}{4,12}"
,
(i+1),
Array[i,0],
Array[i,1],
Array[i,2],
Array[i,3]);
}
Console.WriteLine(
" *******************************"
);
Console.WriteLine();
Console.Write(
" Press any key to continue . . . "
);
Console.ReadKey(
true
);
}
}
}
Array example :
**************************************************************************
element element element element
index value 1 value 2 value 1 * value 2
1 50 400 20000
2 100 100 10000
3 100 100 10000
4 50 200 10000
5 200 50 10000
6 400 50 20000
7 400 50 20000
**************************************************************************
Sorted Array
by column 4
then by column 2
then by column 3
then by column 1
in descending sort order :
*************************************************************************
element element element element
index value 1 value 2 value 1 * value 2
6 400 50 20000
7 400 50 20000
1 50 400 20000
5 200 50 10000
2 100 100 10000
3 100 100 10000
4 50 200 10000
*************************************************************************
Program works fine but I need sugestions for code optimization. I would prefer program code without GOTO statements. Perhaps there is different algorithm for this kind of array sort.
All the best,
Željko Peric
Reply
Answers (
1
)
in need to access key and values from json
Access Data from Delegate.Target