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 SelectionSortAlgoritam
- {
- class SelectionSortAlgoritam
- {
- static void Main(string[] args)
- {
- //Task 7: Sorting an array means to arrange its elements in increasing order.
- //Write a program to sort an array. Use the "selection sort" algorithm:
- //Find the smallest element, move it at the first position, find
- //the smallest from the rest, move it at the second position, etc.
- Console.WriteLine("Please insert 'N' integer number: ");
- int N = int.Parse(Console.ReadLine());
- int[] numbers = new int[N];
- Console.WriteLine("Please insert 'N' elements in the array: ");
- for (int i = 0; i < N; i++)
- {
- numbers[i] = int.Parse(Console.ReadLine());
- }
- int temp = 0;
- for (int j = 0; j < N; j++) //"Selection sort" solution.
- {
- for (int i = j + 1; i < N; i++)
- {
- if (numbers[j] > numbers[i])
- {
- temp = numbers[i];
- numbers[i] = numbers[j];
- numbers[j] = temp;
- }
- }
- }
- //int temp = 0; //"Bubble sort" solution follows.
- //int flag = 0;
- //for (int j = 0; j < N; j++)
- //{
- // for (int i = 0; i < N - 1; i++)
- // {
- // if (numbers[i] > numbers[i + 1])
- // {
- // temp = numbers[i];
- // numbers[i] = numbers[i + 1];
- // numbers[i + 1] = temp;
- // flag++;
- // }
- // }
- // if (flag == 0)
- // {
- // break;
- // }
- // flag = 0;
- //}
- Console.WriteLine("The elements of array in increasing order: ");
- for (int i = 0; i < numbers.Length; i++)
- {
- Console.WriteLine(numbers[i]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment