using System;
using System.Collections.Generic;
namespace SortStrings
{
class Program
{
static void Main(string[] args)
{
string[] inputArray = {"324", "1", "9001", "-23", "99", "324", "800", "801", "802", "888" };
Console.WriteLine("Input array is :");
DisplayArray(ref inputArray);
QuickSort(ref inputArray, 0, inputArray.Length-1);
Console.WriteLine("Sorted array is :");
DisplayArray(ref inputArray);
Console.ReadKey();
}
private static void DisplayArray(ref string[] inputArray)
{
foreach (string s in inputArray)
{
Console.WriteLine(s);
}
}
private static int Partition(ref string[] inputArray, int startIndex, int endIndex)
{
int loopIndex = startIndex;
int lastIndex = startIndex;
CompareNumberString c = new CompareNumberString();
IComparer<string> comparer = (IComparer<string>)c;
while (loopIndex < endIndex)
{
if (comparer.Compare(inputArray[loopIndex], inputArray[endIndex]) < 0)
{
swap(ref inputArray, loopIndex, lastIndex);
lastIndex++;
}
loopIndex++;
}
swap(ref inputArray, endIndex, lastIndex);
return lastIndex;
}
private static void QuickSort(ref string[] inputArray, int startIndex, int endIndex)
{
if (startIndex >= endIndex)
{
return;
}
int pivotIndex = Partition(ref inputArray, startIndex, endIndex);
QuickSort(ref inputArray, startIndex, pivotIndex - 1);
QuickSort(ref inputArray, pivotIndex + 1, endIndex);
return;
}
private static void swap(ref string[] inputArray, int index1, int index2)
{
string temp = inputArray[index1];
inputArray[index1] = inputArray[index2];
inputArray[index2] = temp;
}
}
}
using System.Collections.Generic;
namespace SortStrings
{
class Program
{
static void Main(string[] args)
{
string[] inputArray = {"324", "1", "9001", "-23", "99", "324", "800", "801", "802", "888" };
Console.WriteLine("Input array is :");
DisplayArray(ref inputArray);
QuickSort(ref inputArray, 0, inputArray.Length-1);
Console.WriteLine("Sorted array is :");
DisplayArray(ref inputArray);
Console.ReadKey();
}
private static void DisplayArray(ref string[] inputArray)
{
foreach (string s in inputArray)
{
Console.WriteLine(s);
}
}
private static int Partition(ref string[] inputArray, int startIndex, int endIndex)
{
int loopIndex = startIndex;
int lastIndex = startIndex;
CompareNumberString c = new CompareNumberString();
IComparer<string> comparer = (IComparer<string>)c;
while (loopIndex < endIndex)
{
if (comparer.Compare(inputArray[loopIndex], inputArray[endIndex]) < 0)
{
swap(ref inputArray, loopIndex, lastIndex);
lastIndex++;
}
loopIndex++;
}
swap(ref inputArray, endIndex, lastIndex);
return lastIndex;
}
private static void QuickSort(ref string[] inputArray, int startIndex, int endIndex)
{
if (startIndex >= endIndex)
{
return;
}
int pivotIndex = Partition(ref inputArray, startIndex, endIndex);
QuickSort(ref inputArray, startIndex, pivotIndex - 1);
QuickSort(ref inputArray, pivotIndex + 1, endIndex);
return;
}
private static void swap(ref string[] inputArray, int index1, int index2)
{
string temp = inputArray[index1];
inputArray[index1] = inputArray[index2];
inputArray[index2] = temp;
}
}
}
No comments:
Post a Comment