-8

How to sort list of words alphabetically in dot net without using any built in functions like Sort.

If I have a list of words : Hello,Awesome,Cool,Stuff output: Awesome,Cool,Hello,Stuff

I don't want to use LINQ too

5

1 Answer 1

1

You can use quicksort:

    static int partition(string[] arr, int start, int end) {
        int pivot = end;
        int i = start, j = end;
        string temp;
        while (i < j) {
            while (i < end && string.Compare(arr[i], arr[pivot]) < 0)
                i++;
            while (j > start && string.Compare(arr[j], arr[pivot]) > 0)
                j--;

            if (i < j) {
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
        temp = arr[pivot];
        arr[pivot] = arr[j];
        arr[j] = temp;
        return j;
    }

    static void quicksort(string[] arr, int start, int end) {
        if (start < end) {
            int pivotIndex = partition(arr, start, end);
            quicksort(arr, start, pivotIndex - 1);
            quicksort(arr, pivotIndex + 1, end);
        }
    }

    static void Main(string[] args) {
        string[] arr = { "Hello", "Awesome", "Cool", "Stuff" };
        quicksort(arr, 0, arr.Length - 1);
        foreach (string s in arr) {
            Console.Write(s + " ");
        }
        Console.ReadKey();
    }

Source: http://www.bluesharktutorials.com/2013/08/quick-sort-algorithm-with-c-sharp-program-code.html. I've changed int to string.

Sign up to request clarification or add additional context in comments.

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.