Ritika Roy

Ritika Roy

  • NA
  • 2
  • 2.6k

Problem in String Sorting in C#

Apr 17 2015 2:58 AM
Sir plzz help me to solve this error, The below code is work fine but when i replace
 
for(i=0; i<size; i++) to
 
for(i=1; i<=size; i++) 
 
it shows exception .why? 
 
using System;
public class ArrName
{
public static void Main()
{
string min,temp;
Console.Write("Enter the no. of Application: ");
int size = Convert.ToInt32(Console.ReadLine());
int loc,i,j;
string[] arr = new string[size];
Console.WriteLine("Enter proper name of each Student: ");
for(i = 0; i<size; i++)
{
Console.Write("{0}. ",i);
arr[i] = Console.ReadLine();
}
Console.WriteLine();
Console.ReadLine();
Console.WriteLine("Name After Arrange them in Alphabetical order: ");
for(i = 0; i<size; i++)
{
min = arr[i];
loc = i;
for(j = i+1; j<size; j++)
{
if((arr[j].CompareTo(min))<0 )
{
min = arr[j];
loc = j;
}
if(loc != i)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(i = 0; i<size; i++)
{
Console.WriteLine(" {0}", arr[i]);
}
Console.ReadKey();
}
}

Answers (2)