Advertisement
Aborigenius

SortArrayOfStrings

Jul 2nd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 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 SortArrayOfSrtings
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string [] array = Console.ReadLine().Split(' ').ToArray();
  14.  
  15.  
  16.             BubbleSort(array);
  17.  
  18.             Console.WriteLine(string.Join(" ", array));
  19.  
  20.         }
  21.         static void BubbleSort(string[] array)
  22.         {
  23.             while (true)
  24.             {
  25.  
  26.                 bool swap = false;
  27.                 for (int index = 0; index < array.Length - 1; index++)
  28.                 {
  29.                     if (string.Compare( array[index],  array[index + 1]) > 0) //if first number is greater then second then swap
  30.                     {
  31.                         var temp = array[index];
  32.                         array[index] = array[index + 1];
  33.                         array[index + 1] = temp;
  34.                         swap = true;
  35.                     }
  36.                 }
  37.                 if (!swap)
  38.                 {
  39.                     break;
  40.                 }
  41.             }
  42.             return;
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement