C
C#2y ago
Whiteboy

Counting Sort, display the array

I need to display the array which counted how many each numbers there are like in the screen
3 Replies
Whiteboy
Whiteboy2y ago
namespace LabNext2ASD
{
internal static class CountingSort
{
private static void CountSort(IList<int> arrayToSort, int arrSize)
{
int max = arrayToSort.Max();
int min = arrayToSort.Min();
int range = max - min + 1;
int[] count = new int[range];
int[] output = new int[arrSize];

for (int i = 0; i < arrayToSort.Count; i++)
{
count[arrayToSort[i] - min]++;
}

for (int i = 1; i < count.Length; i++)
{
count[i] += count[i - 1];
}

for (int i = arrayToSort.Count - 1; i >= 0; i--)
{
output[count[arrayToSort[i] - min] - 1] = arrayToSort[i];
count[arrayToSort[i] - min]--;
}

for (int i = 0; i < arrayToSort.Count; i++)
{
arrayToSort[i] = output[i];
}
}

private static void PrintArray(IReadOnlyList<int> arrayToSort)
{
if (arrayToSort == null) throw new ArgumentNullException(nameof(arrayToSort));
foreach (var t in arrayToSort)
{
//Console.Write(t + " ");
}
Console.WriteLine("");
}


public static void Main(string[] args)
{
string[] text = File.ReadAllLines(@"C:\Users\Admin\Desktop\Counting_Sort_0001.csv");
int arrSize = int.Parse(text[0]);
int[] arrayToSort = text.Skip(1).Take(arrSize).Select(int.Parse).ToArray();

CountSort(arrayToSort, arrSize);
PrintArray(arrayToSort);
}
}
}
namespace LabNext2ASD
{
internal static class CountingSort
{
private static void CountSort(IList<int> arrayToSort, int arrSize)
{
int max = arrayToSort.Max();
int min = arrayToSort.Min();
int range = max - min + 1;
int[] count = new int[range];
int[] output = new int[arrSize];

for (int i = 0; i < arrayToSort.Count; i++)
{
count[arrayToSort[i] - min]++;
}

for (int i = 1; i < count.Length; i++)
{
count[i] += count[i - 1];
}

for (int i = arrayToSort.Count - 1; i >= 0; i--)
{
output[count[arrayToSort[i] - min] - 1] = arrayToSort[i];
count[arrayToSort[i] - min]--;
}

for (int i = 0; i < arrayToSort.Count; i++)
{
arrayToSort[i] = output[i];
}
}

private static void PrintArray(IReadOnlyList<int> arrayToSort)
{
if (arrayToSort == null) throw new ArgumentNullException(nameof(arrayToSort));
foreach (var t in arrayToSort)
{
//Console.Write(t + " ");
}
Console.WriteLine("");
}


public static void Main(string[] args)
{
string[] text = File.ReadAllLines(@"C:\Users\Admin\Desktop\Counting_Sort_0001.csv");
int arrSize = int.Parse(text[0]);
int[] arrayToSort = text.Skip(1).Take(arrSize).Select(int.Parse).ToArray();

CountSort(arrayToSort, arrSize);
PrintArray(arrayToSort);
}
}
}
and i need something like this
Whiteboy
Whiteboy2y ago
Whiteboy
Whiteboy2y ago
can be just the numbers in blue Need it asap D: anyone pls D: