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
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
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
.