In (**Figure-2)** you can see a part of the output of the algorithm, and for a better understanding of the algorithm, I will explain the important lines of code, and in the algorithm of part 3 and the algorithm of part 4, the codes that have already been explained, I will avoid explaining and explaining it again.

**2-1-** **Definition of variables**

int** low = -15;**

The low variable is the low range to start the random number generation interval.

int** high = 15;**

The hight variable is the high range for starting the random number generation interval and for creating the array interval and simulating the x-axis.

string**[] Number_Random_Array;**

** Number_Random_Array = **newstring**[high];**

The Number Random Array stores unordered random numbers

string**[] X_Axis;**

** X_Axis = **newstring**[2 * high + 1];**

The X_Axis array simulates the x axis and the generated random numbers are placed on it.

string**[] Result_Sort_Array;**

**Result_Sort_Array = **newstring**[high];**

The Result Sort Array stores the results of sorting numbers.

int**[] count_element_x_axis;**

**count_element_x_axis = **newint**[2 * high + 1];**

The count_element_x_axis array keeps the number of repetitions of the same numbers or the frequency of numbers, and since the x-axis array is simulated, once the number is placed on the x-axis array, and if the same numbers are repeated, the frequency array helps to Store the same number of repeated numbers.

double** sum = 0;**

A sum variable is generated for the first sigma to sum all random numbers.

double** sum2 = 0;**

The sum2 variable is for the second sigma for the sum of the numbers placed on the x-axis.

int** index = 0;**

The index variable is for addressing the numbers in the arrays and causes the sorted numbers placed on the x-axis array to be placed in the sorting results array.

int** index2 = 0;**

The index2 variable is for addressing the numbers in the arrays and causes the sorted numbers to be displayed along with the frequency of the numbers in the output.

double** len = 0;**

The len variable stores half the length of the x-axis array and creates the offset variable.

Int** offset = 0;**

The offset variable helps the generated random number to be placed in the correct position of the simulated x-axis array.

**2-2-** **Initializing arrays**

These few lines of code are only for showing the start of algorithm operation and for information purposes, it can be deleted.

Console.ForegroundColor = ConsoleColor.Cyan;

Console.WriteLine(" X-Axis Sorting Algorithm"**);**

Console.ForegroundColor = ConsoleColor.White;

Console.WriteLine();

Console.WriteLine(" Init X Values in Array "**);**

By entering the for loop, the initial values with the letter x are placed on the simulated array of the x axis, later you will know the reason why the array is of string type and why the initial value "X" was used.

for** (**long** i = 0; i < X_Axis.Length; i++)**

**{**

** X_Axis[i] = **"X"**;**

** count_element_x_axis[i] = 0;**

The number frequency array is also initialized with zero.

** }**

By entering the next for loop, the values of the array of random numbers named Number_Random_Array and the array of sorted results named Result_Sort_Array are also of string type and filled with "X" values, the reasons for the string arrays and "X" values in I will explain further.

for** (**long** i = 0; i < Result_Sort_Array.Length; i++)**

**{**

** Number_Random_Array[i] = **"X"**;**

** Result_Sort_Array[i] = **"X"**;**

**}**

**2-3-** **Generate random numbers**

Console.WriteLine();

Console.WriteLine();

Console.WriteLine(" Make Random Numbers in Array"**);**

These few lines of code are to display the start of the next step of the algorithm and provide information and these few lines of code can be removed from the algorithm.

Random** rnd = **newRandom**();**

for** (**long** i = 0; i < Number_Random_Array.Length; i++)**

** {**

** Number_Random_Array[i] = rnd.Next(low, high).ToString();**

** sum += **long.Parse(Number_Random_Array[i]);

** }**

In the following, the rnd variable and the for loop will create negative and positive random numbers in the low and high variable range of the length of the array, and since the array is of string type, the arithmetic numbers will be converted into string numbers and They will be placed in an array of random numbers. The variable sum will perform the first sigma operation, because this algorithm is also a statistical algorithm and the frequency of numbers is included in it, but in such a way that the frequency of numbers and then the multiplication of the frequency by the numbers and the total time of the algorithm do not increase, So all the random numbers are added together and its frequency will be calculated in another process.

Console.WriteLine();

Console.WriteLine(" Print Random Numbers of Array"**);**

Console.ForegroundColor = ConsoleColor.Green;

These few lines of code are to display the start of the next step of the algorithm and provide information and these few lines of code can be removed from the algorithm. In the form of green text, random numbers generated based on the length of the array are displayed in the output, and according to the for loop, random string numbers are extracted from the Number_Random_Array array and their values will be displayed according to **(Figure 3)**.

for** (**int** i = 0; i < Number_Random_Array.Length; i++)**

** {**

Console.Write(" {0}"**, Number_Random_Array[i]);**

** }**

**2-4- X-axis simulation**

Based on the fact that the positive and negative random numbers are inspired by the x-axis, the array conversion is done on the x-axis, and since using the for loop to the length of the random number array Number_Random_Array, according to **(Figure 4)** when the Space key is pressed on the keyboard, the numbers in the array are displayed in the output according to the position function SetCursorPosition.

It should be noted that the SetCursorPosition position function should be disabled in the range of large numbers in the low and high variables, because the length and width of the monitor are limited and will show an error message, here the purpose of the SetCursorPosition function is only to display the numbers on the x axis of the simulation. is in the output and deleting or commenting the SetCursorPosition function does not harm the correct execution of the algorithm, but we have to because of the length and width of the monitor, if the low and high variables are small, the SetCursorPosition function can be used.

Random array of numbers Number_Random_Array, from index zero to the last index, for example, here it starts from the content of -12 and ends at 3, each number is displayed on the x-axis in the right place, here is the exciting part of the algorithm graphically and as you can see in **(Figure 4)**, the x-axis is simulated with generated random numbers, in addition to the image display, this act of placing numbers in the codes inside the X_Axis array is done as an array, and the X_Axis array is Paying attention to variable len and variable offset, calculations related to the position of each number in the x-axis and X_Axis array are performed.

Console.ForegroundColor = ConsoleColor.White;

Console.WriteLine();

Console.WriteLine();

Console.WriteLine(" Convert Array on the X-Axis"**);**

Console.WriteLine();

Console.WriteLine(" Please Press Space Key for Continue ..."**);**

Console.WriteLine();

Console.ReadKey();

Console.ForegroundColor = ConsoleColor.Green;

These are a few lines of code to show the start of the next step of the algorithm, and we enter the for loop by pressing the space key on the keyboard.

** len = X_Axis.Length / 2;**

** offset = 0;**

for** (**long** i = 0; i < Number_Random_Array.Length; i++)**

** {**

Based on the length of the random array Number_Random_Array, the length of the X_Axis array is halved to be able to implement the positive part and the negative part of the x axis in the X_Axis array, the next step we need an offset variable.

** offset = (**int**)len - **int.Parse(Number_Random_Array[i]);

The offset variable causes the correct order of random numbers to be addressed in the houses of the X_Axis array.

** X_Axis[offset] = Number_Random_Array[i];**

The random numbers in the Number_Random_Array array are placed in the right position in the X_Axis array.

** ++count_element_x_axis[offset];**

Console.SetCursorPosition(100-(int**)offset*3 , 23);**

As mentioned, for a random number that is generated two or more times, the frequency of each random number is stored in the count_element_x_axis array and helps to sort the same repeated random numbers. The SetCursorPosition function only helps to display the correct position of the numbers in the simulated X axis every time the Space button is pressed, I emphasize again that this function should be disabled in the array with a large range of low and high because Display of numbers on the monitor is done in limited coordinates.

if** (i % 4 == 0) **Console.ForegroundColor = ConsoleColor.Green;

if** (i % 4 == 1) **Console.ForegroundColor = ConsoleColor.Yellow;

if** (i % 4 == 2) **Console.ForegroundColor = ConsoleColor.Red;

if** (i % 4 == 3) **Console.ForegroundColor = ConsoleColor.Cyan;

Console.Write("{0}"**, Number_Random_Array[i]);**

Console.ReadKey();

**{**

As you can see in **(Figure 4)**, I have used four different colors to make the results more beautiful with a better graphic effect, although these lines can be removed from the algorithm and the collection of these few lines of code helps, it is better to It showed the process of sorting numbers on the X-axis simulated in the algorithm.

**2-5-** **Dump the x-axis array**

Console.WriteLine();

Console.ForegroundColor = ConsoleColor.Yellow;

Console.WriteLine();

** Number_Random_Array = **null**;**

GC.Collect();

Console.WriteLine(" Clear Number_Random_Array of Memory"**);**

These few lines of code cause empty lines with spaces from the text in the output, and the random number array is destroyed using the GC.Collect() code [2], to save memory and in yellow The message of clearing the Number_Random_Array array from the memory is announced.

Console.ForegroundColor = ConsoleColor.White;

Console.WriteLine();

Console.WriteLine(" Sort Memory LOT Another Array"**);**

Console.ForegroundColor = ConsoleColor.Green;

These are a few lines of code to show the start of the next step of the algorithm, and then we enter the for loop.

for** (**long** i = X_Axis.Length - 1; i >= 0; i--)**

if** (X_Axis[i] != **"X"**)**

** {**

** Result_Sort_Array[index] = X_Axis[i];**

** index++;**

**}**

The reason for using the string type X_Axis array is because of the zero number challenge, when I want to convert the array from large to small in terms of the number of houses and perform compression to reduce memory usage, and the X_Axis array which has the number of houses It is too much and becomes the number of fewer cells in the Result_Sort_Array array, it is necessary to compare that if the comparison against zero was inserted, the number zero would not be included in the sorting, what I mean is that we cannot use numbers for the empty cells of the arrays. show that the array house is empty or the array house is full, so if we consider the number zero as the empty houses of the array and the non-zero number means the houses of the array are full, then the zero number produced by the Random.Next function is considered in sorting It is not possible and as a result the number zero will not be shown in the algorithm, so instead of a numerical array, I have used a string array and in that content "X" means that the array is empty and non-"X" means that it is full. For this reason, I put the content of string arrays full of "X" content, the name of this algorithm is the x-axis algorithm, but the method used is also a weakness, and I have solved this weakness in the next algorithm.

You can see the output of the for loop according to **(Figure 5)** and the length of the x-axis array from the last index to the first index. It is placed under the name Result_Sort_Array and the sorting has been done in ascending order and if I were to code the array navigation from the first index to the end, the numbers from positive to negative would be displayed from left to right and would be placed in the Result_Sort_Array array and sorted It was done in descending order.

Console.ForegroundColor = ConsoleColor.White;

Console.WriteLine();

Console.ForegroundColor = ConsoleColor.Yellow;

Console.WriteLine();

** X_Axis = **null**;**

GC.Collect();

Console.WriteLine(" Clear X_Axis Array of Memory"**);**

Console.WriteLine();

Console.ForegroundColor = ConsoleColor.White;

Console.WriteLine();

These few lines of code create space between the lines to display the text in the output, and the X_Axis axis array is destroyed using the GC.Collect() code, And it saves memory and the message of clearing the X_Axis array from the memory is announced with yellow color.

**2-6- Array of sorted results**

The Result_Sort_Array array is compressed and of course sorted, and if each house of the array is opposite to "X", the string number is calculated using the int.Parse function, and as you can see **(Figure 6)**, the output is displayed given.

Console.WriteLine(" Sort Final Compact Array"**);**

Console.ForegroundColor = ConsoleColor.Green;

for** (**long** i = 0; i < Result_Sort_Array.Length; i++)**

** {**

if** (Result_Sort_Array[i] != **"X"**)**

** {**

** sum2 += **long.Parse(Result_Sort_Array[i]);

Console.Write(" {0} "**, Result_Sort_Array[i]);**

** }**

** }**

It was already stated that this sorting algorithm is also statistical and in this part of the for loop, it only calculates the sum of the sorted numbers based on the array houses that are opposite to the content of "X", meaning the opposite of "X". That is, there is a number in the array house that needs to be added, of course, the number addition part can be removed from the algorithm and only summed up according to the frequency of the numbers.

**2-7- Difference of sigmas**

There are three formulas here, which include two sigma formulas (sum) and sum difference formula, the first sigma formula includes the sum of a randomly generated number multiplied by its frequency, which includes the left and right sides of the image of numbers and frequency, the second sigma formula It only adds the numbers together and the frequency ratio is not taken into account, and the third formula is the distance between the two sums, well, maybe you can tell me what these formulas are!

Console.WriteLine(" Left & Right | Segma i=0 -> n (Number * Count) Total Sum = {0}"**, sum);**

Console.WriteLine(" Only Left | Segma i=0 -> n (Number) Total Sum2 = {0}"**, sum2);**

Console.WriteLine(" | Final Sum - Sum2 = {0} "**, sum - sum2);**

Well, the first sigma formula is all the random numbers that exist in the Number_Random_Array according to their count and frequency and are added together to form the first sigma. The first sigma, which adds all the random numbers together, and the count_element_x_axis array stores the frequency ratio and displays the randomly generated repeated numbers as a result. It is clear that this frequency ratio in the first sigma is only calculated by adding the numbers, although according to **(Figure 7)**, the result of this sigma can be seen in the list of ordered numbers on the left and its frequency on the right.

The second sigma is related to the sorted array named Result_Sort_Array and since the numbers are placed once in the x-axis and of course the frequency of the numbers is also stored in the count_element_x_axis array, then the second sigma is due to the sorted and non-repeating numbers in the x-axis, its frequency will be one unit for all numbers, which means that by using the frequency of numbers, the result of the x-axis sorting algorithm will be displayed.

The subtraction of the sums shows the result that numbers ordered with a frequency of one unit will show the result of zero subtraction, and for each randomly generated number whose frequency is greater than one, the first and second sigma difference and the result of random numbers repeated on the x-axis will be accompanied by the difference of sigmas. If the sigma difference is zero, it means that all the generated random numbers are unique and there are no duplicate random numbers in the array.

**2-8-** **Frequency array of random numbers**

As you can see according to **(Figure 7)**, the left side of the generated random number and the right side shows the frequency of the random number. In the for loop, to match the frequency with the random numbers, the algorithm has been traversed from the end to the beginning, because the algorithm from left to right, it sorts the numbers in ascending order, and the x-axis is inherently ascending, so it is necessary to do the frequency of the numbers according to the x-axis.

Console.WriteLine(" Count Elements of Sort Array "**);**

Console.ForegroundColor = ConsoleColor.Green;

for** (**long** i = count_element_x_axis.Length - 1; i >= 0; i--)**

if** (count_element_x_axis[i] != 0)**

** {**

In the count_element_x_axis frequency array for the sorting result, no house can be zero, unless the house is empty, that is, its frequency number is zero, so if the frequency in the count_element_x_axis array for each house is equal to two or more than two, its number and frequency is shown in red color, otherwise its number and frequency is the same and it will be shown in green color, these codes are better only for visual separation and can be removed from the algorithm, but a better effect for statistical display Shows sorted numbers.

if** (count_element_x_axis[i] >= 2)**

Console.ForegroundColor = ConsoleColor.Red;

else

Console.ForegroundColor = ConsoleColor.Green;

According to the WriteLine function, from top to bottom, small to large numbers are written and displayed, here the Result_Sort_Array array is scrolled from zero to the last index using the index2 variable, but as you can see, the count_element_x_axis frequency array of the i variable for scrolling uses, so the direction of movement and scrolling of the two variables i and index2 are opposite to each other so that the ratio of numbers to its frequency is correctly calculated and displayed.

Console.WriteLine(" Number {0} : Count {1} "**, **long.Parse(Result_Sort_Array[index2]), count_element_x_axis[i]);

**index2++;**

** }**

**2-9-** **End of algorithm**

In the last few lines, the arrays that are left in the memory and occupying the memory space, are cleared using the GC.Collect() code, and are displayed with a message in yellow, a few lines apart from each other, emphasizing that This algorithm is linearly O(n) and the algorithm ends by pressing a key from the keyboard.

Console.ForegroundColor = ConsoleColor.Yellow;

Console.WriteLine();

** Result_Sort_Array = **null**;**

GC.Collect();

Console.WriteLine(" Clear Result_Sort_Array of Memory"**);**

Console.WriteLine();

Console.ForegroundColor = ConsoleColor.Yellow;

Console.WriteLine();

** count_element_x_axis = **null**;**

GC.Collect();

Console.WriteLine(" Clear count_element_x_axis Array of Memory"**);**

Console.WriteLine();

Console.ForegroundColor = ConsoleColor.White;

Console.Write(" This is the X-Axis sorting algorithm of time order and its memory => "**);**

Console.ForegroundColor = ConsoleColor.Yellow;

Console.Write("O(n)"**);**

Console.ReadKey();