Aborigenius

BubbleSwap&InsertionSwap

Jul 1st, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.83 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
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int[] array = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  14.             //  Swap(ref array[0],ref array[1]);
  15.             InsertionSort(array);
  16.             Console.WriteLine(string.Join(" ", array));
  17.         }
  18.         static void InsertionSort(int [] array)
  19.         {
  20.             for (int firstIndex = 1; firstIndex < array.Length; firstIndex++)
  21.             {
  22.                 for (int secondIndex = firstIndex; secondIndex > 0; secondIndex--)
  23.                 {
  24.                     if (array[secondIndex - 1] > array[secondIndex])
  25.                     {
  26.                         Swap(ref array[secondIndex - 1], ref array[secondIndex]);
  27.                     }
  28.                     else
  29.                     {
  30.                         break;
  31.                     }
  32.                 }
  33.             }
  34.         }
  35.         static void BubbleSort(int[] array)
  36.         {
  37.             while (true)
  38.             {
  39.                 bool swap = false;
  40.                 for (int index = 0; index < array.Length - 1; index++)
  41.                 {
  42.                     //to switch to Descending
  43.                     if (array[index] > array[index + 1])
  44.                     {
  45.                         Swap(ref array[index], ref array[index + 1]);
  46.                         swap = true;
  47.                     }
  48.                 }
  49.                 if (!swap)
  50.                 {
  51.                     break;
  52.                 }
  53.             }
  54.             return;
  55.         }
  56.         static void Swap(ref int index1, ref int index2)
  57.         {
  58.             int temp = index1;
  59.             index1 = index2;
  60.             index2 = temp;
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment