Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- /*
- * Reverse Selection Sort
- ***************************************
- * By Starbeamrainbowlabs <https://starbeamrinbowlabs.com>
- *
- * Posted at https://starbeamrainbowlabs.com/blog/article.php?posts%2F039-Selection-Sort.html
- * A binary can be found at https://starbeamrainbowlabs.com/binaries/reverseselection.exe
- */
- public class SelectionSort
- {
- /// <summary>
- /// Swaps the values in an array of ints at the posistions a and b.
- /// </summary>
- /// <param name="array">A reference to the array to operate on.</param>
- /// <param name="a">The index of the first value.</param>
- /// <param name="b">The index of the second value.</param>
- static void swap_places(ref int[] array, int a, int b)
- {
- int temp = array[a];
- array[a] = array[b];
- array[b] = temp;
- }
- /// <summary>
- /// Generates a array of ints to a given size.
- /// </summary>
- /// <param name="size">The size of the array to generate</param>
- /// <param name="min">The minimum number that should appear in the array.</param>
- /// <param name="max">The maxmimum number that should appear in the array.</param>
- /// <returns>An array of random ints that is `size` in length.</returns>
- static int[]generate_array(int size, int min = 0, int max = 25)
- {
- if (min >= max)
- throw new Exception("The min was bigger than max.");
- int[]newarray = new int[size];
- Random generator = new Random();
- for (int i = size - 1; i >= 0; i--) {
- newarray[i] = generator.Next(min, max);
- }
- return newarray;
- }
- /// <summary>
- /// Performs a selection sort on an array of ints.
- /// </summary>
- /// <param name="array">The array to sort.</param>
- static void selection_sort(ref int[] array)
- {
- /*
- * pass #0
- * 0 1 2 3 4
- * [3,1,5,3,2]
- * |---*-|
- * ^ limit
- *
- * pass #1
- * 0 1 2 3 4
- * [3,1,2,3,5]
- * *---^
- * ^ limit
- *
- * pass #2
- * 0 1 2 3 4
- * [3,1,2,3,5]
- * *-^
- * ^ limit
- *
- * pass #3
- * 0 1 2 3 4
- * [2,1,3,3,5]
- * *
- * ^ limit
- *
- * * done *
- * [1,2,3,3,5]
- */
- int limit = array.Length - 1;
- while(limit > 0)
- {
- //find the index with the maximum value
- int max_index = 0; //set the max to the first element in the array
- //don't search the first element in the array, we have already done that on the line above
- for(int i = limit - 1; i > 0; i--)
- {
- if(array[i] > array[max_index])
- max_index = i;
- }
- if(array[max_index] > array[limit])
- {
- //we have found an index with a high value than the current limit
- swap_places(ref array, max_index, limit);
- }
- Console.Write("limit: {0} ", limit);
- print_array(ref array);
- limit--;
- }
- }
- /// <summary>
- /// Prints an array of ints to the console.
- /// </summary>
- /// <param name="array">The array to print.</param>
- static void print_array(ref int[] array, bool new_line = true)
- {
- Console.Write("[ {0} ]", String.Join(", ", array));
- if(new_line)
- Console.WriteLine();
- }
- static void Main()
- {
- int[] array = generate_array(25);
- print_array(ref array);
- selection_sort(ref array);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement