Posted By

mohamedhassan on 12/02/13


Tagged


Versions (?)

bubble


 / Published in: C#
 

bubble

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication2
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12.  
  13. Console.WriteLine("\n Bubble Sort\n");
  14.  
  15. Console.WriteLine(" -------");
  16. Console.Write("plz enter no of elements ");
  17.  
  18. int num = Convert.ToInt32(Console.ReadLine());
  19. Console.WriteLine();
  20. int[] ar = new int[num];
  21. Console.WriteLine("\n--> plz enter the elements \n");
  22.  
  23.  
  24.  
  25.  
  26. for (int i = 0; i < ar.Length; i++)
  27. {
  28. int u = i + 1;
  29. Console.Write(" -plz enter ele no ({0}) ",u );
  30. ar[i] = Convert.ToInt32(Console.ReadLine());
  31.  
  32. }
  33.  
  34. Console.WriteLine("\n-->the way it work");
  35. //bubble sorting-------------------------------------------------------
  36. int co = 1;
  37. foreach (int item in ar) //
  38. { //
  39. for (int y = 0; y < ar.Length -1; y++) //
  40. { //
  41. if (ar[y] > ar[y + 1]) //
  42. { int t = ar[y]; ar[y] = ar[y + 1]; ar[y + 1] = t; } //
  43. }
  44.  
  45.  
  46.  
  47.  
  48. Console.Write(" -step no ({0}) " ,co);
  49. Console.Write(" {");
  50. for (int j = 0; j < ar.Length; j++)
  51. {
  52.  
  53. Console.Write(" {0} ,", ar[j]);
  54.  
  55. }
  56. Console.Write(" } ");
  57. Console.WriteLine();
  58. co++;
  59.  
  60. } //
  61. //--------------------------------------------------------------------//
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. Console.WriteLine("\n--> the answer ");
  70.  
  71. Console.Write(" { ");
  72. for (int j = 0; j < ar.Length ; j++)
  73. {
  74.  
  75. Console.Write("{0} ,",ar[j]);
  76.  
  77. }
  78. Console.Write(" } ");
  79.  
  80. Console.ReadKey();
  81. }
  82. }
  83. }

Report this snippet  

You need to login to post a comment.