Advertisement
sashomaga

Selection sort

Jan 6th, 2013
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using System;
  2. //Array - Selection sort
  3.     class Program
  4.     {
  5.         static void Main()
  6.         {
  7.             Console.Write("Enter the length of the array: ");
  8.             int arrayLength = int.Parse(Console.ReadLine());
  9.             int[] arr = new int[arrayLength];
  10.             Console.WriteLine("Enter the array members:");
  11.             for (int i = 0; i < arrayLength; i++)
  12.             {
  13.                 arr[i] = int.Parse(Console.ReadLine());
  14.             }
  15.            
  16.             for (int i = 0; i < arr.Length; i++)
  17.             {
  18.                 for (int j = i + 1; j < arr.Length; j++)
  19.                 {
  20.                     if (arr[i] > arr[j])
  21.                     {
  22.                         arr[i] ^= arr[j];
  23.                         arr[j] ^= arr[i];
  24.                         arr[i] ^= arr[j];
  25.                     }              
  26.                 }
  27.             }
  28.             Console.WriteLine("Sorted array:");
  29.             foreach (var item in arr)
  30.             {
  31.                 Console.Write("{0} ",item);
  32.             }
  33.             Console.WriteLine();
  34.  
  35.            
  36.         }
  37.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement