aleahim

SelectionSortArray_problem07

Jan 25th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.77 KB | None | 0 0
  1. using System;
  2.  
  3. class SelectionSortAlg_07
  4. {
  5.     static void Main()
  6.     {
  7.         int n = int.Parse(Console.ReadLine());
  8.         int[] arr = new int[n];
  9.         int temp = 0;
  10.         int min = 0;
  11.  
  12.         for (int i = 0; i < n; i++)
  13.         {
  14.             arr[i] = int.Parse(Console.ReadLine());
  15.         }
  16.  
  17.         for (int j = 0; j < arr.Length-1; j++){
  18.             min = j;
  19.             for ( int m = j+1; m < arr.Length; m++) {
  20.                 if (arr[m] < arr[min])
  21.                 {
  22.                 min = m;
  23.                 }
  24.             }
  25.             if (j != min)
  26.             {
  27.             temp = arr[j];
  28.             arr[j] = arr[min];
  29.             arr[min] = temp;
  30.             }
  31.         }
  32.         for (int i = 0; i < arr.Length; i++)
  33.         {
  34.             Console.WriteLine("Element[{0}] = {1}", i, arr[i]);
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment