C# Sorting algorithms implementation

Jack Herer 140 Reputation points
2023-04-26T17:48:34.1+00:00

Hey guys... this post will contain several sorting algorthm implementations in C#.

Follow for more content like this :)

Developer technologies | .NET | Other
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

1 answer

Sort by: Most helpful
  1. Александр Баскаков 0 Reputation points
    2025-09-13T21:01:07.95+00:00

    I just created couple algorithms for sorting with and w/out creating new array.

        public static T[] SortT<T>(T[] a) where T:IComparable<T>
    
        {
    
            T[] result = new T[a.Length];
    
            int current = 0;
    
            for (int i = 0; i < a.Length; i++)
    
            {
    
                current = 0;
    
                for (int j = 0; j < a.Length; j++)
    
                {
    
                    if (a[i].CompareTo(a[j])>0)
    
                        current++;
    
                }
    
                result[current] = a[i];
    
            }
    
            for (int i = 1; i < result.Length; i++)
    
                if (result[i].CompareTo(default(T))==0)
    
                    result[i] = result[i - 1];
    
            return result;
    
        }
    
        // for sorting big data class w/out creating new Tarray.
    
        public static void SortSwapT<T>(ref T[] a) where T:IComparable<T>
    
        {
    
            int[] indCurrent = new int[a.Length];
    
            Dictionary<int, int> currentInd = new Dictionary<int, int>();
    
            int current;
    
            for (int i = 0; i < a.Length; i++)
    
            {
    
                current = 0;
    
                for (int j = 0; j < a.Length; j++)
    
                {
    
                    if (a[i].CompareTo(a[j])>0)
    
                        current++;
    
                }
    
                indCurrent[i] = current;
    
            }
    
            for (int i = 0; i < indCurrent.Length; i++)
    
            {
    
                while (currentInd.ContainsKey(indCurrent[i]))
    
                    indCurrent[i] += 1;
    
                currentInd.Add(indCurrent[i], i);
    
            }
    
            T temp;
    
            for (int i = 0; i < a.Length - 1; i++)
    
            {
    
                temp = a[i];
    
                a[i] = a[currentInd[i]];
    
                a[currentInd[i]] = temp;
    
                currentInd[indCurrent[i]] = currentInd[i];
    
                indCurrent[currentInd[i]] = indCurrent[i];
    
            }
    
        }
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.