Advertisement
desislava_topuzakova

01.Bubble Sort

Jun 14th, 2020
1,001
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Sortign
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int[] numbers = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
  12.             bubbleSort(numbers);
  13.             printArray(numbers);          
  14.  
  15.         }
  16.  
  17.         static void bubbleSort(int[] arr)
  18.         {
  19.             int n = arr.Length;
  20.             for (int i = 0; i < n - 1; i++)
  21.                 for (int j = 0; j < n - i - 1; j++)
  22.                     if (arr[j] > arr[j + 1])
  23.                     {
  24.                         // swap temp and arr[i]
  25.                         int temp = arr[j];
  26.                         arr[j] = arr[j + 1];
  27.                         arr[j + 1] = temp;
  28.                     }
  29.         }
  30.  
  31.         static void printArray(int[] arr)
  32.         {
  33.             int n = arr.Length;
  34.             for (int i = 0; i < n; ++i)
  35.                 Console.Write(arr[i] + " ");
  36.             Console.WriteLine();
  37.         }
  38.  
  39.  
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement