Advertisement
Guest User

SelectionSort With Generics

a guest
Jan 6th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace task7_SelectionSort
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             //Sorting an array means to arrange its elements in increasing order. Write a program to sort an array.
  11.             //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.
  12.  
  13.             //Example Arrays
  14.             int[] ints = { -9 ,9, -1, 6, 2, -7, 4, -1, 6, 5, 8, -8 };
  15.             char[] chars = { 'z','c','e', 'b', 'd', 'a' };
  16.             string[] names = { "Ivan", "Blagoi", "Blaga", "Anton" };
  17.             string[] city = { "Sofia", "Dupnica", "Varna", "Burgas", "Teteven", "Aitos", "AitosZ" };
  18.  
  19.             //sorting
  20.             ints = SelectionSort(ints);
  21.             chars = SelectionSort(chars);
  22.             names = SelectionSort(names);
  23.             city = SelectionSort(city);
  24.  
  25.             //printing
  26.             Print(names);
  27.             Print(chars);
  28.             Print(city);
  29.             Print(ints);
  30.         }
  31.  
  32.         static T[] SelectionSort<T>(T[] array)
  33.         {
  34.             T itemForCompare;
  35.             int itemForSwapIndex;
  36.  
  37.             for (int i = 0; i < array.Length - 1; i++)
  38.             {
  39.                 itemForCompare = array[i];
  40.                 itemForSwapIndex = -1;
  41.  
  42.                 for (int j = i + 1; j < array.Length; j++)
  43.                 {
  44.                     var cmpResult = Comparer<T>.Default.Compare(itemForCompare, array[j]);
  45.                     if(cmpResult > 0)
  46.                     //if (itemForCompare > array[j])
  47.                     {
  48.                         itemForCompare = array[j];
  49.                         itemForSwapIndex = j;
  50.                     }
  51.                 }
  52.  
  53.                 if (itemForSwapIndex > 0)
  54.                 {
  55.                     T temp = array[i];
  56.                     array[i] = array[itemForSwapIndex];
  57.                     array[itemForSwapIndex] = temp;
  58.                 }
  59.             }
  60.  
  61.             return array;
  62.         }
  63.  
  64.         static void Print<T>(IEnumerable<T> g)
  65.         {
  66.             var enumerator = g.GetEnumerator();
  67.  
  68.             var item = enumerator.MoveNext();
  69.             while (item)
  70.             {
  71.                 Console.Write(enumerator.Current);
  72.                 item = enumerator.MoveNext();
  73.                 if (item)
  74.                 {
  75.                     Console.Write(" ");
  76.                 }
  77.             }
  78.             Console.WriteLine();
  79.  
  80.  
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement