Advertisement
grubcho

Bubble Sort - Algorithms

Jun 29th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 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 buble_sort
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int[] array = Console.ReadLine().Split().Select(int.Parse).ToArray();
  14.  
  15.             BubbleSort(array);
  16.             Console.WriteLine(string.Join(" ", array));
  17.         }
  18.         static void BubbleSort(int[] array)
  19.         {
  20.             bool swapped;
  21.             do
  22.             {
  23.                 swapped = false;
  24.                 for (int index = 0; index < array.Length-1; index++)
  25.                 {
  26.                     if (array[index] > array[index+1])
  27.                     {
  28.                         Swap(array,index, index + 1);
  29.                         swapped = true;
  30.                     }
  31.                 }
  32.  
  33.             } while (swapped);
  34.         }
  35.         static void Swap(int[] array, int index1, int index2)
  36.         {
  37.             int temp = array[index1];
  38.             array[index1] = array[index2];
  39.             array[index2] = temp;
  40.  
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement