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


Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2. public class Stringexercise
  3. {
  4. public static void Main()
  5. {
  6. string[] arr1;
  7. string temp;
  8. int num, i, j, lenghth;
  9.  
  10. Console.Write("Input number of strings :");
  11. num = Convert.ToInt32(Console.ReadLine());
  12. arr1 = new string[num];
  13. Console.Write("Input {0} strings below :\n", num);
  14. for (i = 0; i < num; i++)
  15. {
  16. arr1[i] = Console.ReadLine();
  17. }
  18. lenghth = arr1.Length;
  19.  
  20. for (i = 0; i < lenghth; i++)
  21. {
  22. for (j = 0; j < lenghth - 1; j++)
  23. {
  24. if (arr1[j].CompareTo(arr1[j + 1]) > 0)
  25. {
  26. temp = arr1[j];
  27. arr1[j] = arr1[j + 1];
  28. arr1[j + 1] = temp;
  29. }
  30. }
  31. }
  32. Console.Write("\n\nAfter sorting the array appears like : \n");
  33. for (i = 0; i < lenghth; i++)
  34. {
  35. Console.WriteLine(arr1[i] + " ");
  36. }
  37.  
  38. Console.ReadLine();
  39. }
  40. }

Result

C# language techstudy

     





Related Topics




Popular Posts