Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- /*
- * 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.
- */
- class SelectionSort
- {
- static void Main()
- {
- int[] array = { 3, 2, 7, 4, 5, 18, 4, 54, 32 };
- int minKeyElement = 0;
- int tmpElement = int.MinValue;
- for (int j = 0; j < array.Length - 1; j++)
- {
- minKeyElement = j;
- for (int p = j + 1; p < array.Length; p++)
- {
- if (array[p] < array[minKeyElement])
- {
- minKeyElement = p;
- }
- }
- tmpElement = array[minKeyElement];
- array[minKeyElement] = array[j];
- array[j] = tmpElement;
- }
- // Sorted array
- for (int i = 0; i < array.Length; i++)
- {
- Console.WriteLine(array[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement