You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.4 KiB
C#
46 lines
1.4 KiB
C#
using System;
|
|
using System.Linq;
|
|
public class Counting_sort
|
|
{
|
|
public static void Main()
|
|
{
|
|
int[] array = new int[10]
|
|
{
|
|
2, 5, -4, 11, 0, 8, 22, 67, 51, 6
|
|
};
|
|
Console.WriteLine("\n"+"Original array :");
|
|
foreach (int aa in array)
|
|
Console.Write(aa + " ");
|
|
int[] sortedArray = new int[array.Length];
|
|
// find smallest and largest value
|
|
int minVal = array[0];
|
|
int maxVal = array[0];
|
|
for (int i = 1; i < array.Length; i++)
|
|
{
|
|
if (array[i] < minVal) minVal = array[i];
|
|
else if (array[i] > maxVal) maxVal = array[i];
|
|
}
|
|
// init array of frequencies
|
|
int[] counts = new int[maxVal - minVal + 1];
|
|
// init the frequencies
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
counts[array[i] - minVal]++;
|
|
}
|
|
// recalculate
|
|
counts[0]--;
|
|
for (int i = 1; i < counts.Length; i++)
|
|
{
|
|
counts[i] = counts[i] + counts[i - 1];
|
|
}
|
|
// Sort the array
|
|
for (int i = array.Length - 1; i >= 0; i--)
|
|
{
|
|
sortedArray[counts[array[i] - minVal]--] = array[i];
|
|
}
|
|
Console.WriteLine("\n"+"Sorted array :");
|
|
foreach (int aa in sortedArray)
|
|
Console.Write(aa + " ");
|
|
Console.Write("\n");
|
|
}
|
|
} |