Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class SelectionSortOfArray
- {
- static void Main()
- {
- //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.
- int[] myArray = {2,5,4,7,8,12,5};
- int minElement = 0,
- minIndex = 0,
- sortBuffer = 0;
- for (int i = 0; i < myArray.Length; i++)
- {
- minElement = myArray[i];
- for (int j = i; j < myArray.Length; j++)
- {
- if (myArray[j] < minElement)
- {
- minElement = myArray[j];
- minIndex = j;
- }
- }
- sortBuffer = myArray[i];
- myArray[i] = minElement;
- myArray[minIndex] = sortBuffer;
- }
- for (int index = 0; index < myArray.Length; index++)
- {
- Console.Write("{0} ", myArray[index]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement