Write a program in C# Sharp to read a string through the keyboard and sort it using bubble sort

  • برمجة سي شارب
  • برمجة

Write a program in C# Sharp to read a string through the keyboard and sort it using bubble sort.
Test Data :
Input number of strings :3
Input 3 strings below :
abcd
zxcv
mnop
Expected Output :

After sorting the array appears like : 
abcd 
mnop 
zxcv

الأجوبة

using System;
public class exercise12
{
	public static void Main()
	{
		string[] arr1;
		string temp;
		int n,i,j,l;
       Console.Write("\n\nSorts the strings of an array using bubble sort :\n");
       Console.Write("-----------------------------------------------------\n");  
       Console.Write("Input number of strings :");
       n= Convert.ToInt32(Console.ReadLine()); 
       arr1=new string[n];      
       Console.Write("Input {0} strings below :\n",n);
  for(i=0;i<n;i++)
  {
      arr1[i] = Console.ReadLine();	
  }	
      l=arr1.Length;	
		for (i = 0; i < l; i++)
		{
			for (j = 0; j < l-1; j++)
			  {
				if (arr1[j].CompareTo(arr1[j + 1]) > 0)
				  {
                   temp = arr1[j];
                   arr1[j] = arr1[j + 1];
                   arr1[j + 1] = temp;
                  }
              }
        }
		 Console.Write("\n\nAfter sorting the array appears like : \n");
        for (i = 0; i < l; i++)
          {
            Console.WriteLine(arr1[i] + " ");
          }
	}
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...