Download code

Jump to: navigation, search

Back to Selection_sort_(C_Sharp)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

SelectionSortExample.cs

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Selection_sort_(C_Sharp)?oldid=11637
 14 */
 15 
 16 using System;
 17 using System.Collections;
 18 
 19 class SelectionSortExample
 20 {
 21     static void selectionSortIntArray(int[] a)
 22     {
 23         for(int sortedSize=0; sortedSize < a.Length; sortedSize++)
 24         {
 25             int minElementIndex = sortedSize;
 26 	    int minElementValue = a[sortedSize];
 27 	    for(int i=sortedSize+1; i<a.Length; i++) 
 28 	    {
 29 	        if (a[i] < minElementValue)
 30 	        {
 31 	            minElementIndex = i;
 32 	            minElementValue = a[i];
 33 	        }
 34 	    }
 35             a[minElementIndex] = a[sortedSize];
 36 	    a[sortedSize] = minElementValue;
 37         }
 38     }
 39 
 40 
 41     static void selectionSortGeneric<T>(T[] a) where T : IComparable
 42     {
 43         for(int sortedSize=0; sortedSize < a.Length; sortedSize++)
 44         {
 45             int minElementIndex = sortedSize;
 46             T minElementValue = a[sortedSize];
 47             for(int i=sortedSize+1; i<a.Length; i++) 
 48             {
 49                 if (a[i].CompareTo(minElementValue) < 0)
 50                 {
 51                     minElementIndex = i;
 52                     minElementValue = a[i];
 53                 }
 54             }
 55             a[minElementIndex] = a[sortedSize];
 56             a[sortedSize] = minElementValue;
 57         }
 58     }
 59 
 60     static void selectionSortGeneric(object[] a, IComparer comparer)
 61     {
 62         for(int sortedSize=0; sortedSize < a.Length; sortedSize++)
 63         {
 64             int minElementIndex = sortedSize;
 65             object minElementValue = a[sortedSize];
 66             for(int i=sortedSize+1; i<a.Length; i++) 
 67             {
 68                 if (comparer.Compare(a[i], minElementValue) < 0)
 69                 {
 70                     minElementIndex = i;
 71                     minElementValue = a[i];
 72                 }
 73             }
 74             a[minElementIndex] = a[sortedSize];
 75             a[sortedSize] = minElementValue;
 76         }
 77     }
 78 
 79     public static void Main(String[] args)
 80     {
 81         int length = int.Parse(args[0]);
 82         int[] a = new int[length];
 83         Random r = new Random();
 84 
 85         for(int i=0; i<length; i++)
 86         {
 87             a[i] = r.Next();
 88         }
 89 
 90         DateTime start = DateTime.Now;
 91         selectionSortIntArray(a);
 92         Console.WriteLine("Sort time: " + (DateTime.Now - start));
 93 
 94         for (int i = 1; i < length; i++)
 95         {
 96             if (a[i] < a[i - 1])
 97             {
 98                 Console.WriteLine("ERROR");
 99                 return;
100             }
101         }
102     }
103 }


hijacker
hijacker
hijacker
hijacker