Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- public class SelectionSort
- {
- private static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- int[] A = new int[n];
- for (int i = 0; i < A.Length; i++)
- {
- A[i] = int.Parse(Console.ReadLine());
- }
- for (int i = 0; i < A.Length; i++)
- {
- Swap(A, i, A.IndexOfMin(i));
- Console.WriteLine(A[i]);
- }
- }
- private static void Swap(IList<int> list, int first, int second)
- {
- int temp = list[first];
- list[first] = list[second];
- list[second] = temp;
- }
- }
- public static class CollectionExtensions
- {
- public static int IndexOfMin<T>(this IList<T> collection, int startIndex = 0)
- where T : IComparable<T>
- {
- if (0 > startIndex || startIndex >= collection.Count)
- {
- throw new IndexOutOfRangeException("Start index is outside the bounds of the collection");
- }
- int minIndex = startIndex;
- for (int i = startIndex; i < collection.Count; i++)
- {
- if (collection[i].CompareTo(collection[minIndex]) < 0)
- {
- minIndex = i;
- }
- }
- return minIndex;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment