Željko Perić

Ž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 :
 
  1. using System;  
  2.   
  3. namespace Multidimensional_array_sort  
  4. {  
  5.     class Program  
  6.     {  
  7.         public static void Main(string[] args)  
  8.         {  
  9.             //  
  10.             Console.WriteLine();  
  11.             Console.WriteLine(" Hello World!");  
  12.             Console.WriteLine();              
  13.             //  
  14.             int [,] Array;  
  15.             //  
  16.             Console.Write(" Enter number of elements : n = ");  
  17.             //  
  18.             int n = int.Parse(Console.ReadLine());  
  19.             //  
  20.             Array = new int[n,4];  
  21.             //  
  22.             Console.WriteLine();  
  23.             Console.WriteLine(" Element data entry :");  
  24.             Console.WriteLine(" *********************");  
  25.             Console.WriteLine();  
  26.             //  
  27.             for(int i=0; i<n; i++)
  28.             {  
  29.                 Array[i,0]=i+1;  
  30.                 Console.WriteLine(" " +(i+1).ToString() + ". element :");  
  31.                 Console.WriteLine(" *********************");  
  32.                 Console.Write(" length : ");  
  33.                 Array[i,1]=int.Parse(Console.ReadLine());  
  34.                 Console.Write(" width  : ");  
  35.                 Array[i,2]=int.Parse(Console.ReadLine());  
  36.                 Array[i,3]=Array[i,1]*Array[i,2];  
  37.                 Console.WriteLine(" *********************");  
  38.                 Console.WriteLine();  
  39.             }  
  40.             //  
  41.             for(int k=0; k<n-1; k++)
  42.             {  
  43.                 for(int j=k+1; j<n; j++)
  44.                 {  
  45.                     if(Array[k,3]>Array[j,3]) goto next;  
  46.                     if(Array[k,3]goto swap;  
  47.   
  48.                     if(Array[k,1]>Array[j,1]) goto next;  
  49.                     if(Array[k,1]goto swap;  
  50.   
  51.                     if(Array[k,2]>Array[j,2]) goto next;  
  52.                     if(Array[k,2]goto swap;  
  53.   
  54.                     if(Array[k,0]goto next;  
  55.                     if(Array[k,0]>Array[j,0]) goto swap;  
  56.                       
  57.                       
  58.                 swap:  
  59.                     {  
  60.                         int m = Array[j,0];  
  61.                         Array[j,0]=Array[k,0];  
  62.                         Array[k,0]=m;  
  63.                           
  64.                         m = Array[j,1];  
  65.                         Array[j,1]=Array[k,1];  
  66.                         Array[k,1]=m;  
  67.                           
  68.                         m = Array[j,2];  
  69.                         Array[j,2]=Array[k,2];  
  70.                         Array[k,2]=m;  
  71.                           
  72.                         m = Array[j,3];  
  73.                         Array[j,3]=Array[k,3];  
  74.                         Array[k,3]=m;  
  75.                     }  
  76.                       
  77.                 next:  
  78.                     {  
  79.                     }  
  80.                 }  
  81.             }             
  82.             //  
  83.             // Show sorted array  
  84.             // element index, element entry index, value a, value b, value c      
  85.             //  
  86.             Console.WriteLine();  
  87.             Console.WriteLine(" Sorted array :");  
  88.             Console.WriteLine(" *******************************");  
  89.             for(int i=0; i<n; i++)
  90.             {  
  91.                 Console.WriteLine("{0,3}{1,3}{2,5}{3,5}{4,12}",  
  92.                                   (i+1),  
  93.                                   Array[i,0],  
  94.                                   Array[i,1],  
  95.                                   Array[i,2],  
  96.                                   Array[i,3]);  
  97.             }  
  98.             Console.WriteLine(" *******************************");  
  99.             Console.WriteLine();  
  100.             Console.Write(" Press any key to continue . . . ");  
  101.             Console.ReadKey(true);  
  102.         }  
  103.     }  

 
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
 

Answers (1)