svetliovd

Bubble Sort

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