Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. #region License
  2.  
  3. // Copyright (c) 2017, Vira
  4. // All rights reserved.
  5. // Solution: OneLiner
  6. // Project: OneLiner
  7. // Filename: Program.cs
  8. // Date - created:2017.04.26
  9. // Date - current: 2017.04.26
  10.  
  11. #endregion
  12.  
  13. #region Usings
  14.  
  15. using System;
  16. using System.Linq;
  17.  
  18. #endregion
  19. namespace OneLiner
  20. {
  21. internal class Program
  22. {
  23. public static void Main(string[] args)
  24. {
  25. // Generate items (not sorted)
  26. var a = Gen(100);
  27.  
  28. // Sorting the array using the "Selection-Sort"-algorithm
  29. Enumerable.Range(0, a.Length - 1)
  30. .ToList()
  31. .ForEach(i => Swap(ref a, i,
  32. a.ToList().FindIndex(i, a.Length - i, x => x.Equals(a.ToList().GetRange(i, a.Length - i).Min()))));
  33.  
  34. // Printing the sorted array:
  35. a.ToList().ForEach(Console.WriteLine);
  36. }
  37.  
  38. private static int[] Gen(int count, int min = 0, int max = 101)
  39. {
  40. var rand = new Random(DateTime.Now.Millisecond);
  41. return Enumerable
  42. .Repeat(0, count)
  43. .Select(i => rand.Next(min, max))
  44. .ToArray();
  45. }
  46.  
  47. private static void Swap(ref int[] src, int i1, int i2)
  48. {
  49. // Does not work :(
  50. //src[i1] ^= src[i2] ^= src[i1] ^= src[i2];
  51.  
  52. var temp = src[i1];
  53. src[i1] = src[i2];
  54. src[i2] = temp;
  55. }
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement