Advertisement
simonses

SelectionSortArray

Jan 9th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 KB | None | 0 0
  1. using System;
  2.  
  3. class SelectionSortArray
  4. {
  5.     static void Main()
  6.     {
  7.         Console.WriteLine("Insert the length of the array:");
  8.         int n = int.Parse(Console.ReadLine());
  9.  
  10.         // Read the array
  11.         int[] array = new int[n];
  12.  
  13.         for (int i = 0; i < n; i++)
  14.         {
  15.             array[i] = int.Parse(Console.ReadLine());
  16.         }
  17.  
  18.         // Selection Sort
  19.         int currentMin, temp;
  20.                
  21.         for (int i = 0; i < array.Length - 1; i++)
  22.         {
  23.             currentMin = i;
  24.             for (int j = i + 1; j < array.Length; j++)
  25.             {
  26.                 if (array[j] < array[currentMin])
  27.                 {
  28.                     currentMin = j;
  29.                 }
  30.             }
  31.             temp = array[i];
  32.             array[i] = array[currentMin];
  33.             array[currentMin] = temp;
  34.         }
  35.        
  36.         // Print
  37.         for (int i = 0; i < array.Length; i++)
  38.         {
  39.             Console.Write(array[i] + " ");
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement