Thursday, February 18, 2010

How to sort a comma separated array elements from a string using c# with Bubble sort?

static void BubbleSort(string inputString)
{
string[] inputStingArray = inputString.Split(' ');
int[] inputIntArr = inputStingArray.Select(x => int.Parse(x)).ToArray();
if (inputIntArr == null || inputIntArr.Length <= 0) { throw new ArgumentException("Inavlid array input"); } int temp; for (int i = inputIntArr.Length - 1; i >= 0; i--)
{
for (int j = 0; j < i; j++) { if (inputIntArr[j] > inputIntArr[j + 1])
{
temp = inputIntArr[j];
inputIntArr[j] = inputIntArr[j + 1];
inputIntArr[j + 1] = temp;
}
}
}

Console.WriteLine("Each individual Item in Array after sorting is");
foreach (int item in inputIntArr)
{
Console.WriteLine(item);
}
Console.ReadKey();
}

No comments:

Post a Comment