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;
- //Read a list of integers on the first line of the console. On the next line, you will receive an integer N. After that, find and print //the largest N elements the array, sorted in descending order.
- //namespace LargestNElements
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] array = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- int n = int.Parse(Console.ReadLine());
- //BubbleSort(array);
- InsertionSort(array);
- List<int> largestN = new List<int>();
- for (int i = 0; i < n; i++)
- {
- largestN.Add(array[i]);
- }
- Console.WriteLine(string.Join(" ", largestN));
- }
- //static void BubbleSort(int[] array)
- //{
- //bool swapped;
- //do
- //{
- //swapped = false;
- //for (int i = 0; i < array.Length-1; i++)
- //{
- //if (array[i] < array[i+1])
- //{
- //Swap(ref array[i], ref array[i+1]);
- //swapped = true;
- //}
- //}
- //} while (swapped);
- //}
- static void InsertionSort(int[] array)
- {
- for (int firstIndex = 0; firstIndex < array.Length; firstIndex++)
- {
- for (int secondIndex = firstIndex; secondIndex > 0; secondIndex--)
- {
- if (array[secondIndex-1] < array[secondIndex])
- {
- Swap(ref array[secondIndex - 1], ref array[secondIndex]);
- }
- else
- {
- break;
- }
- }
- }
- }
- static void Swap(ref int num1, ref int num2)
- {
- int temp = num1;
- num1 = num2;
- num2 = temp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement