Advertisement
Guest User

BubbleSort

a guest
Sep 15th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _01.SortArrayBubbleSort
  5. {
  6.     class SortArrayBubbleSort
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] arr = Console.ReadLine().Trim().Split(' ')
  11.                 .Select(int.Parse).ToArray();
  12.  
  13.             BubbleSort(arr);
  14.             Console.WriteLine(string.Join(", ", arr));
  15.         } // end static void Main(string[] args)
  16.  
  17.         static void BubbleSort(int[] array)
  18.         {
  19.             bool swapped = true;
  20.             while (swapped)
  21.             {
  22.                 swapped = false;
  23.                 for (int i = 0; i < array.Length-1; i++)
  24.                 {
  25.                     if (array[i] > array[i+1])
  26.                     {
  27.                         Swap(ref array[i], ref array[i + 1]);
  28.                         swapped = true;
  29.                     }
  30.                 }
  31.             }
  32.         } // end static void BubbleSort(int[] array)
  33.  
  34.         static void Swap(ref int a, ref int b)
  35.         {
  36.             int temp = a;
  37.             a = b;
  38.             b = temp;
  39.         } // end static void Swap(int a, int b)
  40.  
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement