Venciity

[Jumpstart C#] ArraysAndLists - bubbleSort

Feb 19th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace bubbleSort
  8. {
  9.     class bubbleSort
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var list = new List<int>();    //  List<int> list; по-кратък начин на изписване !
  14.  
  15.             int number;
  16.             do
  17.             {
  18.                 number = Int32.Parse(Console.ReadLine());
  19.                 if (number != 0)
  20.                 {
  21.                     list.Add(number);
  22.                 }
  23.             } while (number != 0);
  24.  
  25.             int n = list.Count;
  26.             bool swapped;
  27.             do
  28.             {
  29.                 int temp;
  30.                 swapped = false;
  31.                 for (int i = 0; i < n-1; i++)
  32.                 {
  33.                     if (list[i] > list[i+1])
  34.                     {
  35.                         swapped = true;
  36.                         temp = list[i + 1];
  37.                         list[i + 1] = list[i];
  38.                         list[i] = temp;
  39.                     }
  40.                 }
  41.  
  42.                 n--;
  43.             } while (swapped && n > 1); // обхождай ги докато се разменят или n > 1
  44.  
  45.             foreach (var element in list)
  46.             {
  47.                 Console.Write(element + " ");
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment