Advertisement
logancberrypie

Insertion sort

Apr 24th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 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 Linear_Search
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             List<int> unsorted = new List<int>();
  14.             Random generator = new Random();
  15.             for (int i =0;i<20;i++)
  16.             {
  17.                 unsorted.Add(generator.Next()%100);
  18.             }
  19.             Sort(unsorted);
  20.         }
  21.         static void Sort(List<int> unsorted)
  22.         {
  23.             for (int sorted = 1; sorted < unsorted.Count();i++)
  24.             {
  25.                 int j = sorted;
  26.                 while (j>0 && unsorted[j] < unsorted[j - 1])
  27.                 {
  28.                     int temp = unsorted[j-1];
  29.                     unsorted[j - 1] = unsorted[j];
  30.                     unsorted[j] = temp;
  31.                     j--;
  32.                 }
  33.             }
  34.             unsorted.ForEach(Console.WriteLine);
  35.             Console.ReadLine();
  36.         }
  37.    
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement