Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace insertion_sort_with_list
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<int> list = Console.ReadLine().Split(' ').Select(int.Parse).ToList();
- List<int> resultList = InsertionSort(list);
- Console.WriteLine(string.Join(" ", resultList));
- }
- static List<int> InsertionSort(List<int> list)
- {
- List<int> resultList = new List<int>();
- for (int firstIndex = 0; firstIndex < list.Count; firstIndex++)
- {
- bool isInserted = false;
- int currentElementToInsert = list[firstIndex];
- for (int secondIndex = 0; secondIndex < resultList.Count; secondIndex++)
- {
- int currentResultListElement = resultList[secondIndex];
- if (currentElementToInsert < currentResultListElement)
- {
- resultList.Insert(secondIndex, currentElementToInsert);
- isInserted = true;
- break;
- }
- }
- if (!isInserted)
- {
- resultList.Add(currentElementToInsert);
- }
- }
- return resultList;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement