2

i wrote an application which is a custom console that allows execution of various commands. One of the commands allows sorting an array of integers. The input data is a string, which is a list of comma separated numbers.

My question is - how to minimize the sorting code runtime complexity as much as possible?

using System;
using System.Collections.Generic;
using CustomConsole.Common;
namespace Shell_Commander.Commands
{
class SortCommand : ICommand
{

    private string _sortCommandName = "sort";
    public string Name { get { return this._sortCommandName; } set { _sortCommandName = value; } }


    public string Execute(string parameters)
    {
        var splittedParameters = parameters.Split(",",StringSplitOptions.RemoveEmptyEntries);
        var arr = new List<int>();

        int number;
        int parameterIndex = 0;

        foreach (var item in splittedParameters)
        {
            parameterIndex++;

            if (int.TryParse(item, out number))
            {
                arr.Add(number);
            }
            else
            {
                Console.WriteLine($"The {parameterIndex}'st parameter isn't a number. Value: '{item}'.");
            }
        }

        int temp = 0;

        for (int write = 0; write < arr.Count; write++)
        {
            for (int sort = 0; sort < arr.Count - 1; sort++)
            {
                if (arr[sort] > arr[sort + 1])
                {
                    temp = arr[sort + 1];
                    arr[sort + 1] = arr[sort];
                    arr[sort] = temp;
                }
            }
        }
        var result = "";

        Console.Write("The sort result is: ");
        for (int i = 0; i < arr.Count; i++)
        {
            result += arr[i] + " ";
            Console.Write(arr[i] + " ");
        }

        Console.WriteLine();
        return result;
    }
}

}

Example - for the '4,1,2' input, the output will be:

"The sort result is: 1 2 4".
2
  • 1
    Are you talking about the little swap sort routine that you have in the code? Commented May 4, 2019 at 19:20
  • Use efficient sorting algorithm, like QuickSort? Use sorting from standard library? Commented May 4, 2019 at 19:23

2 Answers 2

2

You need to read this and that. In fact, you can simply use LINQ's OrderBy method, which is internally using QuickSort.

So, here's the solution, instead of double for, use this:

var sortedArray = arr.OrderBy(x => x).ToArray();
Sign up to request clarification or add additional context in comments.

2 Comments

Why not just Array.Sort?
@dyukha it's up to you, choose any of the already existing in C# sort functions.
1

Why not just replace your little sorting routine with calling the Sort method on array variable.

arr.Sort();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.